How do I implement TypeAdapterFactory in Gson?

When you register a regular type adapter (GsonBuilder.registerTypeAdapter), it only generates a type adapter for THAT specific class. For example: public abstract class Animal { abstract void speak(); } public class Dog extends Animal { private final String speech = “woof”; public void speak() { System.out.println(speech); } } // in some gson related method gsonBuilder.registerTypeAdapter(Animal.class, … Read more

Deserialize array of key value pairs using Json.NET

The simplest way is deserialize array of key-value pairs to IDictionary<string, string>: public class SomeData { public string Id { get; set; } public IEnumerable<IDictionary<string, string>> Data { get; set; } } private static void Main(string[] args) { var json = “{ \”id\”: \”123\”, \”data\”: [ { \”key1\”: \”val1\” }, { \”key2\” : \”val2\” } … Read more

Jackson JSON library: how to instantiate a class that contains abstract fields

There are multiple ways; before version 1.8, simplest way is probably to do: @JsonDeserialize(as=Cat.class) public abstract class AbstractAnimal { … } as to deciding based on attribute, that is best done using @JsonTypeInfo, which does automatic embeddeding (when writing) and use of type information. There are multiple kinds of type info (class name, logical type … Read more

Convert an int to bool with Json.Net [duplicate]

Ended up creating a converter: public class BoolConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(((bool)value) ? 1 : 0); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return reader.Value.ToString() == “1”; } public override bool CanConvert(Type objectType) { return objectType == typeof(bool); } … Read more

property type or class using reflection

After searching through Apples Documentation about objc Runtime and according to this SO answer I finally got it working. I just want to share my results. unsigned int count; objc_property_t* props = class_copyPropertyList([MyObject class], &count); for (int i = 0; i < count; i++) { objc_property_t property = props[i]; const char * name = property_getName(property); … Read more

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