How to handle a NumberFormatException with Gson in deserialization a JSON response

Here is an example that I made for Long type. This is a better option: public class LongTypeAdapter extends TypeAdapter<Long> { @Override public Long read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } String stringValue = reader.nextString(); try { Long value = Long.valueOf(stringValue); return value; } catch (NumberFormatException e) { … 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

GSON does not deserialize reference to outer class

As Gson documentation says: Gson can serialize static nested classes quite easily. Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization. You can address this problem … Read more

Make Jackson use a custom deserializer everywhere (for a type which isn’t mine)

LongJsonDeserializer deserializer = new LongJsonDeserializer(); SimpleModule module = new SimpleModule(“LongDeserializerModule”, new Version(1, 0, 0, null, null, null)); module.addDeserializer(Long.class, deserializer); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(module); Here’s a full demo application. This works with the latest release of Jackson, and probably also with Jackson versions going back to 1.7. import java.io.IOException; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonProcessingException; import … Read more

How to deserialize JSON to objects of the correct type, without having to define the type before hand?

Json.NET has the ability to record the .Net object type of polymorphic types during serialization, by using the setting TypeNameHandling = TypeNameHandling.Auto. When the setting is enabled the .Net type of polymorphic objects will appear as a synthetic property called “$type”, for instance: “$type”: “Newtonsoft.Json.Samples.Stockholder, Newtonsoft.Json.Tests” However, the “$type” property is never emitted for the … Read more

Jackson JSON Deserialization: array elements in each line

If you don’t want to extend DefaultPrettyPrinter you can also just set the indentArraysWith property externally: ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE); String json = objectMapper.writer(prettyPrinter).writeValueAsString(object);

Issue with JSON.stringify adding a extra \ and “” to my Json object

It looks like you are placing a string as the value in your map. You should do something like: var objMap = {“JObject” : ValuesArray}; var json = JSON.stringify(objMap) What’s happening is you are double json encoding your values array – note that your “invalid” JSON value is actually a JSON string rather than the … Read more

What’s the Jackson deserialization equivalent of @JsonUnwrapped?

You can use @JsonCreator with @JsonProperty for each field: @JsonCreator public Parent(@JsonProperty(“age”) Integer age, @JsonProperty(“firstName”) String firstName, @JsonProperty(“lastName”) String lastName) { this.age = age; this.name = new Name(firstName, lastName); } Jackson does type checking and unknown field checking for you in this case.