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

Java: Jackson polymorphic JSON deserialization of an object with an interface property?

You should use JsonTypeInfo.As.EXTERNAL_PROPERTY instead of JsonTypeInfo.As.PROPERTY. In this scenario your Asset class should look like this: class Asset { @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = “type”) @JsonSubTypes({ @JsonSubTypes.Type(value = ImageAssetProperties.class, name = “image”), @JsonSubTypes.Type(value = DocumentAssetProperties.class, name = “document”) }) private AssetProperties properties; public AssetProperties getProperties() { return properties; } public … Read more

Right way to write JSON deserializer in Spring or extend it

I’ve searched a lot and the best way I’ve found so far is on this article: Class to serialize package net.sghill.example; import net.sghill.example.UserDeserializer import net.sghill.example.UserSerializer import org.codehaus.jackson.map.annotate.JsonDeserialize; import org.codehaus.jackson.map.annotate.JsonSerialize; @JsonDeserialize(using = UserDeserializer.class) public class User { private ObjectId id; private String username; private String password; public User(ObjectId id, String username, String password) { this.id = … Read more

Java serialization – java.io.InvalidClassException local class incompatible [duplicate]

@DanielChapman gives a good explanation of serialVersionUID, but no solution. the solution is this: run the serialver program on all your old classes. put these serialVersionUID values in your current versions of the classes. as long as the current classes are serial compatible with the old versions, you should be fine. (note for future code: … Read more

How can I serialize and deserialize a type with a string member that contains “raw” JSON, without escaping the JSON in the process

Your question is, How can I serialize and deserialize a type with a string member that contains “raw” JSON, without escaping the JSON in the process? This can be done via a custom JsonConverter that reads and writes raw JSON using JsonWriter.WriteRawValue() and JRaw.Create(): public class RawConverter : JsonConverter { public override bool CanConvert(Type objectType) … Read more

Serialization – readObject writeObject overrides

You have to do it like this: import java.io.IOException; class Student implements java.io.Serializable { String name; String DOB; int id; Student(String naam, int idno, String dob) { name = naam; id = idno; DOB = dob; } private void writeObject(java.io.ObjectOutputStream stream) throws IOException { stream.writeObject(name); stream.writeInt(id); stream.writeObject(DOB); } private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException … Read more