Why does Jackson polymorphic serialization not work in lists?

The various reasons for why this happens are discussed here and here. I don’t necessarily agree with the reasons, but Jackson, because of type erasure, doesn’t off the bat know the type of elements the List (or Collection or Map) contains. It chooses to use a simple serializer that doesn’t interpret your annotations. You have … Read more

What’s the use of Moshi’s Kotlin codegen?

This is sort of three questions Why is code gen useful Code gen is useful as a compile-time alternative to the reflective moshi-kotlin. Both of them are useful because they natively understand Kotlin code and its language features. Without them, Moshi would not be able to understand Kotlin nullability, default values, and much more. There … Read more

How to write a custom JSON decoder for a complex object?

The encoder/decoder example you reference (here) could be easily extended to allow different types of objects in the JSON input/output. However, if you just want a simple decoder to match your encoder (only having Edge objects encoded in your JSON file), use this decoder: class EdgeDecoder(json.JSONDecoder): def __init__(self, *args, **kwargs): json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs) def … Read more

How to use generics and list of generics with json serialization in Dart?

json_serializable json_serializable has a several strategies1 to handle generic types as single objects T or List<T> (as of v. 5.0.2+) : Helper Class: JsonConverter Helper Methods: @JsonKey(fromJson:, toJson:) Generic Argument Factories @JsonSerializable(genericArgumentFactories: true) 1 Of which I’m aware. There’s likely other ways to do this. Helper Class: JsonConverter Basic idea: write a custom JsonConverter class … Read more

Jackson Annotations: Difference Between JsonIgnoreProperties(ignoreUnknown=true) and JsonInclude(Include.NON_EMPTY)

Short answer: @JsonIgnoreProperties(ignoreUnknown=true) is applicable at deserialization of JSON to Java object (POJO) only. If your POJO does not contain certain properties that JSON does contain, they are ignored and no error is thrown. On the other hand @JsonInclude(Include.NON_EMPTY) is used at serialization of POJO to JSON and it says, skip POJO properties that are: … Read more

TypeNameHandling caution in Newtonsoft Json

When deserialize with TypeNameHandling.All and without a SerializationBinder checks json.net will try to create a instace of the type that comes as metadata in the JSON. public class Car { public string Maker { get; set; } public string Model { get; set; } } { “$type”: “Car”, “Maker”: “Ford”, “Model”: “Explorer” } //create a … Read more

How can I write structured data to a file and then read it back into the same structure later?

Why not use python pickle? Python has a great serializing module called pickle it is very easy to use. import cPickle cPickle.dump(obj, open(‘save.p’, ‘wb’)) obj = cPickle.load(open(‘save.p’, ‘rb’)) There are two disadvantages with pickle: It’s not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source. The format … Read more