Package net.tangly.gleam.model
Record Class JsonEntity<T>
java.lang.Object
java.lang.Record
net.tangly.gleam.model.JsonEntity<T>
- Type Parameters:
T
- type of Java entity to map with a JSON representation- Record Components:
properties
- the fields of the JSON entityfactory
- factory to create a new Java object based on the JSON entityimports
- optional import function to translate a JSON object into a Java objectexports
- optional export function to translate a Java object into a JSON representation
public record JsonEntity<T>(List<JsonField<T,?>> properties, Supplier<T> factory, BiFunction<org.json.JSONObject,Object,T> imports, BiFunction<T,Object,org.json.JSONObject> exports)
extends Record
Defines a mapping between JSON entity and a Java entity. We have an isomorphic structure of two graphs, meaning Java entities can contains other entities and
JSON entities can contains the associated JSON entities mapping these Java entities.
- A regular Java class is mapped. The property declarations are used to map between Java instance and JSON object.
- A Java class is inferred from some JSON fields and lookup in the data model. Therefore the import function is specific to the business logic of the domain entity. The export function uses the regular approach.
- A Java record is used as Java domain model representation. Therefore the import function is record specific because no setter are available. The export function uses the regular approach.
The BiFunction lambdas for imports and exports transformations are needed when the transformation process needs the context of the owning entity to perform semantic checks or mappings. If no context is required simply use the factory method providing Function lambdas.
-
Constructor Summary
ConstructorDescriptionJsonEntity
(List<JsonField<T, ?>> properties, Supplier<T> factory, BiFunction<org.json.JSONObject, Object, T> imports, BiFunction<T, Object, org.json.JSONObject> exports) Creates an instance of aJsonEntity
record class. -
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
Indicates whether some other object is "equal to" this one.BiFunction
<T, Object, org.json.JSONObject> exports()
Returns the value of theexports
record component.org.json.JSONObject
Export an entity as a JSON entity.org.json.JSONObject
Export an entity as a JSON entity.factory()
Returns the value of thefactory
record component.final int
hashCode()
Returns a hash code value for this object.BiFunction
<org.json.JSONObject, Object, T> imports()
Returns the value of theimports
record component.imports
(@NotNull org.json.JSONObject object) Imports an entity from a JSON object.Imports an entity from a JSON object.static <T> JsonEntity
<T> Defines a factory method for a Java record mapped to a JSON type.static <T> JsonEntity
<T> Defines a factory method for a regular Java class mapped to a JSON type.static <T> JsonEntity
<T> of
(BiFunction<org.json.JSONObject, Object, T> imports, BiFunction<T, Object, org.json.JSONObject> exports) static <T> JsonEntity
<T> of
(List<JsonField<T, ?>> properties, Supplier<T> factory, BiFunction<org.json.JSONObject, Object, T> imports, BiFunction<T, Object, org.json.JSONObject> exports) Defines a factory method for transformation process with the context of the owning entitystatic <T> JsonEntity
<T> of
(List<JsonField<T, ?>> properties, Supplier<T> factory, Function<org.json.JSONObject, T> imports, Function<T, org.json.JSONObject> exports) Defines a factory method for a context less transformation process.Returns the value of theproperties
record component.final String
toString()
Returns a string representation of this record class.
-
Constructor Details
-
JsonEntity
public JsonEntity(List<JsonField<T, ?>> properties, Supplier<T> factory, BiFunction<org.json.JSONObject, Object, T> imports, BiFunction<T, Object, org.json.JSONObject> exports) Creates an instance of aJsonEntity
record class.- Parameters:
properties
- the value for theproperties
record componentfactory
- the value for thefactory
record componentimports
- the value for theimports
record componentexports
- the value for theexports
record component
-
-
Method Details
-
of
public static <T> JsonEntity<T> of(@NotNull @NotNull List<JsonField<T, ?>> properties, @NotNull @NotNull Supplier<T> factory) Defines a factory method for a regular Java class mapped to a JSON type.- Type Parameters:
T
- type to be converted to JSON representation- Parameters:
properties
- fields of the Java class to be exported to JSON and imported From JSON.factory
- factory method to create a new Java class instance, often the default constructor- Returns:
- JSON entity defining the transformation process
-
of
public static <T> JsonEntity<T> of(@NotNull @NotNull List<JsonField<T, ?>> properties, @NotNull @NotNull Function<org.json.JSONObject, T> imports) Defines a factory method for a Java record mapped to a JSON type. The imports block is custom to handle the construction of a record, the export uses regular mechanisms through the declaration of the fields for the Java class.- Type Parameters:
T
- record type to be converted to JSON representation- Parameters:
properties
- fields of the Java class to be exported to JSON and imported From JSON.imports
- import function transforming a JSON object into a Java record- Returns:
- JSON entity defining the transformation process
-
of
public static <T> JsonEntity<T> of(BiFunction<org.json.JSONObject, Object, T> imports, BiFunction<T, Object, org.json.JSONObject> exports) -
of
public static <T> JsonEntity<T> of(List<JsonField<T, ?>> properties, Supplier<T> factory, Function<org.json.JSONObject, T> imports, Function<T, org.json.JSONObject> exports) Defines a factory method for a context less transformation process.- Type Parameters:
T
- type of the Java instances- Parameters:
properties
- fields of the Java class to be exported to JSON and imported From JSON.factory
- factory method to create a new Java class instance, often the default constructorimports
- import function transforming a JSON object into a Java instanceexports
- export function transforming a Java instance into a JSON object- Returns:
- JSON entity defining the transformation process
-
of
public static <T> JsonEntity<T> of(List<JsonField<T, ?>> properties, Supplier<T> factory, BiFunction<org.json.JSONObject, Object, T> imports, BiFunction<T, Object, org.json.JSONObject> exports) Defines a factory method for transformation process with the context of the owning entity- Type Parameters:
T
- type of the Java instances- Parameters:
properties
- fields of the Java class to be exported to JSON and imported From JSON.factory
- factory method to create a new Java class instance, often the default constructorimports
- import function transforming a JSON object into a Java instance, parameters are the JSON object, the object owning the transformed Java entity and the Java object as return value.exports
- export function transforming a Java instance into a JSON object, parameters are the Java entity to be transformed in a JSON representation, the object owning the the Java entity and the JSON object of the entity JSON representation as return value.- Returns:
- JSON entity defining the transformation process
-
exports
Export an entity as a JSON entity.- Parameters:
entity
- entity to be exported as JSON object- Returns:
- JSON representation of the entity
-
exports
-
imports
Imports an entity from a JSON object.- Parameters:
object
- JSON representation of the entity- Returns:
- entity created based on the JSON object
-
imports
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object)
. -
properties
Returns the value of theproperties
record component.- Returns:
- the value of the
properties
record component
-
factory
-
imports
-
exports
-