net.opentsdb.utils
public final class JSON extends Object
The class also provides some simple wrappers around commonly used serialization and deserialization methods for POJOs as well as a JSONP wrapper. These work wonderfully for smaller objects and you can use JAVA annotations to control the de/serialization for your POJO class.
For streaming of large objects, access the mapper directly via getMapper()
or getFactory()
Unfortunately since Jackson provides typed exceptions, most of these methods will pass them along so you'll have to handle them where you are making a call.
Troubleshooting POJO de/serialization:
If you get mapping errors, check some of these
@JsonAutoDetect
annotation
Useful Class Annotations:
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
- will serialize
any, public or private values
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
- will
automatically ignore any fields set to NULL, otherwise they are serialized
with a literal null value
Useful Method Annotations:
@JsonIgnore
- Ignores the method for de/serialization purposes.
CRITICAL for any methods that could cause a de/serialization infinite loop
Modifier and Type | Class and Description |
---|---|
static class |
JSON.SearchTypeDeserializer
Helper class for deserializing Search type enum from human readable
strings
|
static class |
JSON.TreeRuleTypeDeserializer
Helper class for deserializing Tree Rule type enum from human readable
strings
|
static class |
JSON.UniqueIdTypeDeserializer
Helper class for deserializing UID type enum from human readable strings
|
Constructor and Description |
---|
JSON() |
Modifier and Type | Method and Description |
---|---|
static com.fasterxml.jackson.core.JsonFactory |
getFactory()
Returns a reference to the JsonFactory for streaming creation
|
static com.fasterxml.jackson.databind.ObjectMapper |
getMapper()
Returns a reference to the static ObjectMapper
|
static <T> T |
parseToObject(byte[] json,
Class<T> pojo)
Deserializes a JSON formatted byte array to a specific class type
Note: If you get mapping exceptions you may need to provide a
TypeReference
|
static <T> T |
parseToObject(byte[] json,
com.fasterxml.jackson.core.type.TypeReference<T> type)
Deserializes a JSON formatted byte array to a specific class type
|
static <T> T |
parseToObject(String json,
Class<T> pojo)
Deserializes a JSON formatted string to a specific class type
Note: If you get mapping exceptions you may need to provide a
TypeReference
|
static <T> T |
parseToObject(String json,
com.fasterxml.jackson.core.type.TypeReference<T> type)
Deserializes a JSON formatted string to a specific class type
|
static com.fasterxml.jackson.core.JsonParser |
parseToStream(byte[] json)
Parses a JSON formatted byte array into raw tokens for streaming or tree
iteration
Warning: This method can parse an invalid JSON object without
throwing an error until you start processing the data
|
static com.fasterxml.jackson.core.JsonParser |
parseToStream(InputStream json)
Parses a JSON formatted inputs stream into raw tokens for streaming or tree
iteration
Warning: This method can parse an invalid JSON object without
throwing an error until you start processing the data
|
static com.fasterxml.jackson.core.JsonParser |
parseToStream(String json)
Parses a JSON formatted string into raw tokens for streaming or tree
iteration
Warning: This method can parse an invalid JSON object without
throwing an error until you start processing the data
|
static byte[] |
serializeToBytes(Object object)
Serializes the given object to a JSON byte array
|
static byte[] |
serializeToJSONPBytes(String callback,
Object object)
Serializes the given object and wraps it in a callback function
i.e.
|
static String |
serializeToJSONPString(String callback,
Object object)
Serializes the given object and wraps it in a callback function
i.e.
|
static String |
serializeToString(Object object)
Serializes the given object to a JSON string
|
public static final <T> T parseToObject(String json, Class<T> pojo)
json
- The string to deserializepojo
- The class type of the object used for deserializationpojo
typeIllegalArgumentException
- if the data or class was null or parsing
failedJSONException
- if the data could not be parsedpublic static final <T> T parseToObject(byte[] json, Class<T> pojo)
json
- The byte array to deserializepojo
- The class type of the object used for deserializationpojo
typeIllegalArgumentException
- if the data or class was null or parsing
failedJSONException
- if the data could not be parsedpublic static final <T> T parseToObject(String json, com.fasterxml.jackson.core.type.TypeReference<T> type)
json
- The string to deserializetype
- A type definition for a complex objectpojo
typeIllegalArgumentException
- if the data or type was null or parsing
failedJSONException
- if the data could not be parsedpublic static final <T> T parseToObject(byte[] json, com.fasterxml.jackson.core.type.TypeReference<T> type)
json
- The byte array to deserializetype
- A type definition for a complex objectpojo
typeIllegalArgumentException
- if the data or type was null or parsing
failedJSONException
- if the data could not be parsedpublic static final com.fasterxml.jackson.core.JsonParser parseToStream(String json)
json
- The string to parseIllegalArgumentException
- if the data was null or parsing failedJSONException
- if the data could not be parsedpublic static final com.fasterxml.jackson.core.JsonParser parseToStream(byte[] json)
json
- The byte array to parseIllegalArgumentException
- if the data was null or parsing failedJSONException
- if the data could not be parsedpublic static final com.fasterxml.jackson.core.JsonParser parseToStream(InputStream json)
json
- The input stream to parseIllegalArgumentException
- if the data was null or parsing failedJSONException
- if the data could not be parsedpublic static final String serializeToString(Object object)
object
- The object to serializeIllegalArgumentException
- if the object was nullJSONException
- if the object could not be serializedIOException
- Thrown when there was an issue reading the objectpublic static final byte[] serializeToBytes(Object object)
object
- The object to serializeIllegalArgumentException
- if the object was nullJSONException
- if the object could not be serializedIOException
- Thrown when there was an issue reading the objectpublic static final String serializeToJSONPString(String callback, Object object)
callback
- The name of the Javascript callback to prependobject
- The object to serializeIllegalArgumentException
- if the callback method name was missing
or object was nullJSONException
- if the object could not be serializedIOException
- Thrown when there was an issue reading the objectpublic static final byte[] serializeToJSONPBytes(String callback, Object object)
callback
- The name of the Javascript callback to prependobject
- The object to serializeIllegalArgumentException
- if the callback method name was missing
or object was nullJSONException
- if the object could not be serializedIOException
- Thrown when there was an issue reading the objectpublic static final com.fasterxml.jackson.databind.ObjectMapper getMapper()
public static final com.fasterxml.jackson.core.JsonFactory getFactory()