Class Serializer

java.lang.Object
com.codingchili.core.protocol.Serializer

public class Serializer extends Object
Serializes objects to JSON or YAML and back. Utility methods for gzip and class definition generation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static com.fasterxml.jackson.databind.ObjectMapper
     
    static com.fasterxml.jackson.databind.ObjectMapper
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static io.vertx.core.buffer.Buffer
    buffer​(Object object)
    Converts any object into a buffer in json format.
    static Map<String,​String>
    describe​(Class<?> template)
    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
    json​(Object object)
    Converts an object into a json object.
    static <T> T
    kryo​(Function<com.esotericsoftware.kryo.Kryo,​T> kryo)
    Execute with a pooled kryo instance.
    static String
    pack​(Object object)
    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
    unpack​(io.vertx.core.json.JsonObject json, Class<T> clazz)
    Dematerializes a json-string into a typed object.
    static <T> T
    unpack​(String data, Class<T> clazz)
    Dematerializes a json-string into a typed object.
    static <T> T
    unyaml​(String data, Class<T> clazz)
    Dematerializes a yaml-string into a typed object.
    static String
    yaml​(Object object)
    Serializes an object as YAML.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static <T> T kryo(Function<com.esotericsoftware.kryo.Kryo,​T> 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

      public static void skipTransient(com.esotericsoftware.kryo.Kryo kryo, Class theClass)
      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

      public static String pack(Object object)
      Serializes an object as JSON.
      Parameters:
      object - containing JSON transformable types.
      Returns:
      a JSON string representing the object.
    • yaml

      public static String yaml(Object object)
      Serializes an object as YAML.
      Parameters:
      object - the object to serialize
      Returns:
      a YAML string representing the object.
    • buffer

      public static io.vertx.core.buffer.Buffer buffer(Object object)
      Converts any object into a buffer in json format.
      Parameters:
      object - the object to serialize.
      Returns:
      a Buffer of the json encoded object.
    • unpack

      public static <T> T unpack(String data, Class<T> clazz)
      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

      public static <T> T unyaml(String data, Class<T> clazz)
      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

      public static <T> T unpack(io.vertx.core.json.JsonObject json, Class<T> clazz)
      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

      public static io.vertx.core.json.JsonObject json(Object object)
      Converts an object into a json object.
      Parameters:
      object - object to be converted.
      Returns:
      JsonObject
    • getValueByPath

      public static <T> Collection<T> getValueByPath(io.vertx.core.json.JsonObject json, String path)
      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 from
      path - 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

      public static <T> Collection<T> getValueByPath(Object object, String path)
      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

      public static Map<String,​String> describe(Class<?> template)
      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.