Class EntitySerializationImpl

java.lang.Object
io.jmix.core.impl.serialization.EntitySerializationImpl
All Implemented Interfaces:
EntitySerialization

@Component("core_EntitySerialization") public class EntitySerializationImpl extends Object implements EntitySerialization
  • Field Details

  • Constructor Details

    • EntitySerializationImpl

      public EntitySerializationImpl()
  • Method Details

    • toJson

      public String toJson(Object entity)
      Description copied from interface: EntitySerialization
      An overloaded version of the EntitySerialization.toJson(Object, FetchPlan, EntitySerializationOption...) method with a null fetchPlan parameter and with no serialization options.
      Specified by:
      toJson in interface EntitySerialization
      Parameters:
      entity - an entity to be serialized
      Returns:
      a string that represents a JSON object
    • toJson

      public String toJson(Object entity, @Nullable FetchPlan fetchPlan, EntitySerializationOption... options)
      Description copied from interface: EntitySerialization
      Serializes a single entity to the JSON object graph.

      If the fetchPlan parameter is null then all loaded entity properties will be presented in JSON, otherwise only loaded properties that are in the fetchPlan will be in the JSON object.

      The options parameter specify some additional options for the serialization process. For example, repeated entities may be replaced with the object with the only "id" property, making the result JSON more compact. See EntitySerializationOption for details.

      Additionally, an "_entityName" property is added to the JSON objects that represent an entity.

      Specified by:
      toJson in interface EntitySerialization
      Parameters:
      entity - an entity to be serialized
      fetchPlan - a fetch plan that defines which entity properties should be added to the result JSON object
      options - options specifying how an entity should be serialized
      Returns:
      a string that represents a JSON object
    • toJson

      public String toJson(Collection<?> entities)
      Description copied from interface: EntitySerialization
      An overloaded version of the EntitySerialization.toJson(Collection, FetchPlan, EntitySerializationOption...) method with a null fetchPlan parameter and with no serialization options.
      Specified by:
      toJson in interface EntitySerialization
      Parameters:
      entities - a collection of entities to be serialized
      Returns:
      a string that represent a JSON array which contains entity objects
    • toJson

      public String toJson(Collection<?> entities, @Nullable FetchPlan fetchPlan, EntitySerializationOption... options)
      Description copied from interface: EntitySerialization
      Serializes a collection of entities to the JSON array. Method works like the EntitySerialization.toJson(Object, FetchPlan, EntitySerializationOption...), but return a JSON array as a result.
      Specified by:
      toJson in interface EntitySerialization
      Parameters:
      entities - a list of entities to be serialized
      fetchPlan - a fetch plan that defines which entity properties should be added to the result JSON object
      options - options specifying how an entity should be serialized
      Returns:
      a string that represents a JSON array of objects.
    • objectToJson

      public String objectToJson(Object object, EntitySerializationOption... options)
      Description copied from interface: EntitySerialization
      Serializes any custom POJO or collection of POJOs or JSON. If some field in POJO is an entity then this field will be serialized according to entity serialization rules. Date fields are serialized according to the format of the DateTimeDatatype.
      Specified by:
      objectToJson in interface EntitySerialization
      Parameters:
      object - any POJO or collection of POJOs
      options - options specifying how a JSON object graph for fields with type 'Entity' will be serialized
      Returns:
      a string that represents a JSON object or JSON array
    • entityFromJson

      public <T> T entityFromJson(String json, @Nullable MetaClass metaClass, EntitySerializationOption... options)
      Description copied from interface: EntitySerialization
      Deserializes a JSON object to the entity.

      The metaClass parameter defines a result entity metaClass. It is optional. It must be defined if the JSON object doesn't contain an "_entityName" property.

      An entity may be serialized to the JSON in slightly different formats. The format is defined by the options parameter. See EntitySerializationOption for details.

      Specified by:
      entityFromJson in interface EntitySerialization
      Parameters:
      json - a string that represents a JSON object
      metaClass - a metaClass of the entity that will be created
      options - options specifying how a JSON object graph was serialized
      Returns:
      an entity
    • entitiesCollectionFromJson

      public <T> Collection<T> entitiesCollectionFromJson(String json, @Nullable MetaClass metaClass, EntitySerializationOption... options)
      Description copied from interface: EntitySerialization
      Deserializes a JSON array of objects to entities collection
      Specified by:
      entitiesCollectionFromJson in interface EntitySerialization
      Parameters:
      json - a string that represents a JSON array of objects
      metaClass - a metaClass of the entities that will be created
      options - options specifying how a JSON object graph was serialized
      Returns:
      an entities collection
    • objectFromJson

      public <T> T objectFromJson(String json, Type type, EntitySerializationOption... options)
      Description copied from interface: EntitySerialization
      Deserializes an object of any class from JSON. Date fields are deserialized according to the DateTimeDatatype format, entities object and fields of Entity type are deserialized like in the EntitySerialization.entityFromJson(String, MetaClass, EntitySerializationOption...) method
      Specified by:
      objectFromJson in interface EntitySerialization
      Parameters:
      json - a string that represents an object
      type - type of the object
      options - options specifying how a JSON object graph was serialized
      Returns:
      deserialized object
    • createGsonForSerialization

      protected com.google.gson.Gson createGsonForSerialization(@Nullable FetchPlan fetchPlan, EntitySerializationOption... options)
    • createGsonForDeserialization

      protected com.google.gson.Gson createGsonForDeserialization(@Nullable MetaClass metaClass, EntitySerializationOption... options)
    • registerDatatypeAdaptersForSerialization

      protected void registerDatatypeAdaptersForSerialization(com.google.gson.GsonBuilder gsonBuilder)
      Registers serializers for the types that Gson cannot handle by itself, e.g. the java.time types. The values are written using the corresponding datatypes.
    • registerDatatypeAdaptersForDeserialization

      protected void registerDatatypeAdaptersForDeserialization(com.google.gson.GsonBuilder gsonBuilder)
      Registers deserializers for the types that Gson cannot handle by itself, e.g. the java.time types. The values are read using the corresponding datatypes.
    • getField

      protected @Nullable Field getField(@Nullable Class clazz, String fieldName)
    • makeFieldAccessible

      protected void makeFieldAccessible(Field field)