Class JSONConvert
Helper class for serializing objects and deserializing json files. Essentially wraps around the ObjectMapper of the jackson library.
Inheritance
Inherited Members
Namespace:
Assembly: .dll
Syntax
public final class JSONConvert
Constructors
JSONConvert()
Declaration
public JSONConvert()
Fields
ForgivingMapper
As opposed to UnforgivingMapper, does NOT fail on 1. JSON contains more properties than there are setters in the POJO. 2. POJO contains more setters, than there are properties in the JSON.
Declaration
public static final ObjectMapper ForgivingMapper
Field Value
Type | Description |
---|---|
com.fasterxml.jackson.databind.ObjectMapper |
UnforgivingMapper
Fails deserialization on 1. JSON contains more properties than there are setters in the POJO. 2. POJO contains more setters, than there are properties in the JSON.
Declaration
public static final ObjectMapper UnforgivingMapper
Field Value
Type | Description |
---|---|
com.fasterxml.jackson.databind.ObjectMapper |
Methods
<T>deserializeJson(String json, TypeReference<T> type, ObjectMapper mapper)
Deserializes a Json string into an object of the given type. This method can be used if you want to deserialize into a generic type. E.g. List.
Declaration
public static final T <T>deserializeJson(String json, TypeReference<T> type, ObjectMapper mapper)
Parameters
Type | Name | Description |
---|---|---|
java.lang.String | json | The json to deserialize |
com.fasterxml.jackson.core.type.TypeReference<T> | type | The type reference to the generic type. |
com.fasterxml.jackson.databind.ObjectMapper | mapper |
Returns
Type | Description |
---|---|
T | The deserialized object, null if deserialization failed |
<T>deserializeJson(String json, Class<T> outClass, ObjectMapper mapper)
Deserializes a json string into an object of given class type.
Declaration
public static final T <T>deserializeJson(String json, Class<T> outClass, ObjectMapper mapper)
Parameters
Type | Name | Description |
---|---|---|
java.lang.String | json | The json to deserialize |
java.lang.Class<T> | outClass | The class to deserialize into |
com.fasterxml.jackson.databind.ObjectMapper | mapper |
Returns
Type | Description |
---|---|
T | The deserialized object, null if deserialization failed |
deserializeJsonUnknown(String json)
Deserializes a json string of unknown format.
Declaration
public static final JsonNode deserializeJsonUnknown(String json)
Parameters
Type | Name | Description |
---|---|---|
java.lang.String | json | The json string |
Returns
Type | Description |
---|---|
com.fasterxml.jackson.databind.JsonNode | The JsonNode created from the json |
serializeObject(Object o)
Serializes a given object into a JSON string.
Declaration
public static final String serializeObject(Object o)
Parameters
Type | Name | Description |
---|---|---|
java.lang.Object | o | the object to serialize |
Returns
Type | Description |
---|---|
java.lang.String | A json string representation of the object, empty string if serialization failed |