Package com.codingchili.core.protocol
Class Serializer
java.lang.Object
com.codingchili.core.protocol.Serializer
Serializes objects to JSON or YAML and back. Utility methods for gzip and class definition generation.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic io.vertx.core.buffer.Buffer
Converts any object into a buffer in json format.Serializes the non static member fields of the given class.static <T> Collection<T>
getValueByPath(io.vertx.core.json.JsonObject json, String path)
Extract the value at the given path from the given jsonobject.static <T> Collection<T>
getValueByPath(Object object, String path)
Gets a value by the given path for an object.static byte[]
gzip(byte[] data)
Compresses a byte array using gzip.static io.vertx.core.json.JsonObject
Converts an object into a json object.static <T> T
Execute with a pooled kryo instance.static String
Serializes an object as JSON.static void
skipTransient(com.esotericsoftware.kryo.Kryo kryo, Class theClass)
Configures the current kryo instance to skip copying and serializing of transient fields.static byte[]
ungzip(byte[] data)
Decompress a byte array using gzip.static <T> T
Dematerializes a json-string into a typed object.static <T> T
Dematerializes a json-string into a typed object.static <T> T
Dematerializes a yaml-string into a typed object.static String
Serializes an object as YAML.
-
Field Details
-
json
public static com.fasterxml.jackson.databind.ObjectMapper json -
yaml
public static com.fasterxml.jackson.databind.ObjectMapper yaml
-
-
Constructor Details
-
Serializer
public Serializer()
-
-
Method Details
-
kryo
Execute with a pooled kryo instance.- Type Parameters:
T
- the type of value that is returned by the given kryo operation.- Parameters:
kryo
- a pooled kryo instance.- Returns:
- the value that is returned by the kryo invocation.
-
skipTransient
Configures the current kryo instance to skip copying and serializing of transient fields. If the serializer configuration is already up to date then no changes will be committed.- Parameters:
kryo
- the kryo instance to apply the configuration to.theClass
- the class for which the serializer configuration is to be changed.
-
pack
Serializes an object as JSON.- Parameters:
object
- containing JSON transformable types.- Returns:
- a JSON string representing the object.
-
yaml
Serializes an object as YAML.- Parameters:
object
- the object to serialize- Returns:
- a YAML string representing the object.
-
buffer
Converts any object into a buffer in json format.- Parameters:
object
- the object to serialize.- Returns:
- a Buffer of the json encoded object.
-
unpack
Dematerializes a json-string into a typed object.- Type Parameters:
T
- must be bound to the clazz parameter- Parameters:
data
- json-encoded string.clazz
- the class to instantiate.- Returns:
- an object specified by the type parameter.
-
unyaml
Dematerializes a yaml-string into a typed object.- Type Parameters:
T
- must be bound to the class parameter.- Parameters:
data
- the yaml-encoded string.clazz
- the class to instantiate.- Returns:
- an object specified by the type parameters.
-
unpack
Dematerializes a json-string into a typed object.- Type Parameters:
T
- must be bound to the clazz parameter- Parameters:
json
- json object to be unpacked.clazz
- the class to instantiate.- Returns:
- an object specified by the type parameter.
-
json
Converts an object into a json object.- Parameters:
object
- object to be converted.- Returns:
- JsonObject
-
getValueByPath
Extract the value at the given path from the given jsonobject. The path is formatted by delimiting fields with a dot.- Type Parameters:
T
- free class cast.- Parameters:
json
- the json object to extract a value frompath
- the path of the value, for example "person.name.last", last may be an array or regular field. when extracting values from arrays only plain fields are allowed, no objects in arrays.- Returns:
- the extracted value type casted to the type parameter.
-
getValueByPath
Gets a value by the given path for an object.- Type Parameters:
T
- the type of the field to retrieve.- Parameters:
object
- the object to get the path value of.path
- the path to the field to retrieve the value of, may be an object or collection.- Returns:
- a list of values matching the path.
-
gzip
public static byte[] gzip(byte[] data)Compresses a byte array using gzip.- Parameters:
data
- data to be compressed.- Returns:
- data compressed with gzip.
-
ungzip
public static byte[] ungzip(byte[] data)Decompress a byte array using gzip.- Parameters:
data
- to be decompressed.- Returns:
- data decompressed with gzip.
-
describe
Serializes the non static member fields of the given class.- Parameters:
template
- the class of which members should be described.- Returns:
- a map that can be serialized to json with field name mapped to type.
-