Serialization of struct

Following example shows a simplest way to serialize struct into char array and de-serialize it. #include <iostream> #include <cstring> #define BUFSIZE 512 #define PACKETSIZE sizeof(MSG) using namespace std; typedef struct MSG { int type; int priority; int sender; char message[BUFSIZE]; }MSG; void serialize(MSG* msgPacket, char *data); void deserialize(char *data, MSG* msgPacket); void printMsg(MSG* msgPacket); int … Read more

System.Text.Json – Deserialize nested object as string

Found a right way how to correctly read the nested JSON object inside the JsonConverter. The complete solution is the following: public class SomeModel { public int Id { get; set; } public string Name { get; set; } [JsonConverter(typeof(InfoToStringConverter))] public string Info { get; set; } } public class InfoToStringConverter : JsonConverter<string> { public … Read more

Deserializing heterogenous JSON array into covariant List using Json.NET

Here is an example using CustomCreationConverter. public class JsonItemConverter : Newtonsoft.Json.Converters.CustomCreationConverter<Item> { public override Item Create(Type objectType) { throw new NotImplementedException(); } public Item Create(Type objectType, JObject jObject) { var type = (string)jObject.Property(“valueType”); switch (type) { case “int”: return new IntItem(); case “string”: return new StringItem(); } throw new ApplicationException(String.Format(“The given vehicle type {0} is … Read more

Error Deserializing Xml to Object – xmlns=” was not expected

Simply take off the Namespace =: [XmlRoot(“register-account”), XmlType(“register-account”)] public class RegisterAccountResponse {…} since your xml doesn’t seem to be in an xml-namespace. Also, [Serializable] isn’t used by XmlSerializer. If your xml was using a namespace it would have an xmlns at the root. Also, to help with callers you could add where T : class, … Read more

Serializing/deserializing with memory stream

This code works for me: public void Run() { Dog myDog = new Dog(); myDog.Name= “Foo”; myDog.Color = DogColor.Brown; System.Console.WriteLine(“{0}”, myDog.ToString()); MemoryStream stream = SerializeToStream(myDog); Dog newDog = (Dog)DeserializeFromStream(stream); System.Console.WriteLine(“{0}”, newDog.ToString()); } Where the types are like this: [Serializable] public enum DogColor { Brown, Black, Mottled } [Serializable] public class Dog { public String Name … Read more

Newtonsoft Json Deserialize Dictionary as Key/Value list from DataContractJsonSerializer

Extending Andrew Whitaker’s answer, here’s a completely generic version that works on any type of writable dictionary: public class JsonGenericDictionaryOrArrayConverter: JsonConverter { public override bool CanConvert(Type objectType) { return objectType.GetDictionaryKeyValueTypes().Count() == 1; } public override bool CanWrite { get { return false; } } object ReadJsonGeneric<TKey, TValue>(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { … Read more

How to serialize an object that includes BufferedImages

make your ArrayList<BufferedImage> transient, and implement a custom writeObject() method. In this, write the regular data for your ImageCanvas, then manually write out the byte data for the images, using PNG format. class ImageCanvas implements Serializable { transient List<BufferedImage> images; private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(images.size()); // how many images are serialized? … Read more

Jackson Change JsonIgnore Dynamically

For serialization, “filtering properties” blog entry should help. Deserialization side has less support, since it is more common to want to filter out stuff that is written. One possible approach is to sub-class JacksonAnnotationIntrospector, override method(s) that introspect ignorability of methods (and/or fields) to use whatever logic you want. It might also help if you … Read more

Handling decimal values in Newtonsoft.Json

You can handle both formats (the JSON number representation and the masked string format) using a custom JsonConverter class like this. class DecimalConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token … Read more

tech