Ignore a property during xml serialization but not during deserialization

This is the solution outlined by Manoj: If you want to suppress serialization of a specific property Foo, but still be able to deserialize it, you can add a method public bool ShouldSerializeFoo() that always returns false. Example: public class Circle2 { public double Diameter { get; set; } public double Radius { get { … Read more

NewtonSoft.Json Serialize and Deserialize class with property of type IEnumerable

You don’t need to use JsonConverterAttribute, just keep your model clean and use CustomCreationConverter instead, the code is simpler: public class SampleConverter : CustomCreationConverter<ISample> { public override ISample Create(Type objectType) { return new Sample(); } } Then: var sz = JsonConvert.SerializeObject( sampleGroupInstance ); JsonConvert.DeserializeObject<SampleGroup>( sz, new SampleConverter()); Documentation: Deserialize with CustomCreationConverter

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

How to serialize & deserialize JavaScript objects?

In general, there’s no way (in a browser) to serialize objects with functions attached to them: Every function has a reference to its outer scope, that scope won’t exist when you deserialize it, so serialized references to that scope will be invalid. What I would do is use the built-in (or json2.js) JSON.stringify and JSON.parse … 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

tech