Package net.tangly.gleam.model
Record Class JsonProperty<T,U>
java.lang.Object
java.lang.Record
net.tangly.gleam.model.JsonProperty<T,U>
- Type Parameters:
T
- type of the entity owning the fieldU
- type of the property- Record Components:
key
- name of the property containing the JSON arraygetter
- getter of the property - returns an item of type U from an object of type Tsetter
- setter of the property of type U to the object of type Textracts
- conversion function between a JSON object and a Java object of type Uinserts
- conversion function between a Java object of type U and a JSON object type
- All Implemented Interfaces:
JsonField<T,
U>
public record JsonProperty<T,U> (@NotNull String key, @NotNull Function<T,U> getter, @NotNull BiConsumer<T,U> setter, @NotNull Function<org.json.JSONObject,U> extracts, @NotNull BiFunction<U,org.json.JSONObject,org.json.JSONObject> inserts)
extends Record
implements JsonField<T,U>
Defines a JSON property mapping a Java class to a JSON entity property or mapping a simple property to a simple JSON property.
The API defines two sets of services
- the first protocol defines how the property of type U is converted to and from a JSON representation. The JSON key under which the
JSON representation is stored in the JSON structure is also accessible. This protocol is defined in
JsonField
- the second protocol defines how the JSON representation can be written to or read from the Java instance.
-
Constructor Summary
ConstructorDescriptionJsonProperty
(@NotNull String key, @NotNull Function<T, U> getter, @NotNull BiConsumer<T, U> setter, @NotNull Function<org.json.JSONObject, U> extracts, @NotNull BiFunction<U, org.json.JSONObject, org.json.JSONObject> inserts) Creates an instance of aJsonProperty
record class. -
Method Summary
Modifier and TypeMethodDescriptionconvertFromJson
(@NotNull org.json.JSONObject object) Import the JSON value and transform it to the type of the associated property after an optional conversion.org.json.JSONObject
convertToJson
(U property, @NotNull org.json.JSONObject object) Export the JSON value from the associated property after an optional conversion.final boolean
Indicates whether some other object is "equal to" this one.void
Export the JSON value from the associated property after an optional conversion.extracts()
Returns the value of theextracts
record component.getter()
Returns the value of thegetter
record component.final int
hashCode()
Returns a hash code value for this object.void
Import the JSON value and set the associated property after an optional conversion.@NotNull BiFunction
<U, org.json.JSONObject, org.json.JSONObject> inserts()
Returns the value of theinserts
record component.@NotNull String
key()
Returns the value of thekey
record component.static <T,
U> JsonProperty <T, U> of
(@NotNull String key, @NotNull Function<T, U> getter, BiConsumer<T, U> setter, @NotNull Function<org.json.JSONObject, U> extracts, @NotNull BiFunction<U, org.json.JSONObject, org.json.JSONObject> inserts) static <T> JsonProperty
<T, BigDecimal> ofBigDecimal
(@NotNull String key, Function<T, BigDecimal> getter, BiConsumer<T, BigDecimal> setter) static <T> JsonProperty
<T, Currency> ofCurrency
(@NotNull String key, Function<T, Currency> getter, BiConsumer<T, Currency> setter) static <T> JsonProperty
<T, Integer> static <T> JsonProperty
<T, LocalDate> ofLocalDate
(@NotNull String key, Function<T, LocalDate> getter, BiConsumer<T, LocalDate> setter) static <T> JsonProperty
<T, Locale> static <T> JsonProperty
<T, Long> static <T> JsonProperty
<T, String> static <T,
U> JsonProperty <T, U> ofType
(@NotNull String property, Function<T, U> getter, BiConsumer<T, U> setter, JsonEntity<U> entity) Create a JSON property of type U@NotNull BiConsumer
<T, U> setter()
Returns the value of thesetter
record component.final String
toString()
Returns a string representation of this record class.
-
Constructor Details
-
JsonProperty
public JsonProperty(@NotNull @NotNull String key, @NotNull @NotNull Function<T, U> getter, @NotNull @NotNull BiConsumer<T, U> setter, @NotNull @NotNull Function<org.json.JSONObject, U> extracts, @NotNull @NotNull BiFunction<U, org.json.JSONObject, org.json.JSONObject> inserts) Creates an instance of aJsonProperty
record class.
-
-
Method Details
-
ofString
public static <T> JsonProperty<T,String> ofString(@NotNull @NotNull String key, Function<T, String> getter, BiConsumer<T, String> setter) -
ofInt
public static <T> JsonProperty<T,Integer> ofInt(@NotNull @NotNull String key, Function<T, Integer> getter, BiConsumer<T, Integer> setter) -
ofLong
public static <T> JsonProperty<T,Long> ofLong(@NotNull @NotNull String key, Function<T, Long> getter, BiConsumer<T, Long> setter) -
ofBigDecimal
public static <T> JsonProperty<T,BigDecimal> ofBigDecimal(@NotNull @NotNull String key, Function<T, BigDecimal> getter, BiConsumer<T, BigDecimal> setter) -
ofLocalDate
public static <T> JsonProperty<T,LocalDate> ofLocalDate(@NotNull @NotNull String key, Function<T, LocalDate> getter, BiConsumer<T, LocalDate> setter) -
ofCurrency
public static <T> JsonProperty<T,Currency> ofCurrency(@NotNull @NotNull String key, Function<T, Currency> getter, BiConsumer<T, Currency> setter) -
ofLocale
public static <T> JsonProperty<T,Locale> ofLocale(@NotNull @NotNull String key, Function<T, Locale> getter, BiConsumer<T, Locale> setter) -
ofType
public static <T,U> JsonProperty<T,U> ofType(@NotNull @NotNull String property, Function<T, U> getter, BiConsumer<T, U> setter, JsonEntity<U> entity) Create a JSON property of type U- Type Parameters:
T
- type of the entity owning the propertyU
- type of the Java property- Parameters:
property
- name of the propertygetter
- getter of the propertysetter
- setter of the propertyentity
- JSON entity describing the JSON representation of type U- Returns:
- new JSON property
-
of
public static <T,U> JsonProperty<T,U> of(@NotNull @NotNull String key, @NotNull @NotNull Function<T, U> getter, BiConsumer<T, U> setter, @NotNull @NotNull Function<org.json.JSONObject, U> extracts, @NotNull @NotNull BiFunction<U, org.json.JSONObject, org.json.JSONObject> inserts) -
convertFromJson
Import the JSON value and transform it to the type of the associated property after an optional conversion. The implementation has the knowledge how to extract the values from the JSON object based of the value type used.- Parameters:
object
- JSON object containing the values to convert- Returns:
- Java property value associated with the JSON object
- See Also:
-
convertToJson
public org.json.JSONObject convertToJson(@NotNull U property, @NotNull @NotNull org.json.JSONObject object) Export the JSON value from the associated property after an optional conversion. Multiple values or whole JSON object structures can be written if the object is a complex one. The implementation has the knowledge how to write the values from the JSON object based of the value type used.- Parameters:
property
- to transform into a JSON objectobject
- JSON object containing the values to convert- Returns:
- JSON object containing the converted values
- See Also:
-
imports
Description copied from interface:JsonField
Import the JSON value and set the associated property after an optional conversion. Multiple JSON values can be used if the object is complex. -
exports
Description copied from interface:JsonField
Export the JSON value from the associated property after an optional conversion. Multiple JSON values can be written if the object is a complex one. -
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)
. -
key
-
getter
-
setter
-
extracts
-
inserts
-