Class JsonGenerator
- java.lang.Object
-
- org.actioncontroller.optional.json.JsonGenerator
-
public class JsonGenerator extends Object
Convert from regular Java objects to JSON structures. Supports primitive numbers (byte, short, int, long, float, double, BigDecimal, BigInteger), simple values (for String, character, Instant, LocalDate, ZonedDateTime, OffsetDateTime, UUID, URL, URI and InetAddress), Enums, Collections, Maps, Streams and Java beans. You can even add your own converters.Usage:
JsonGenerator jsonGenerator = new JsonGenerator(); jsonGenerator.addMapper(MyClass.class, o -> new JsonObject()); JsonValue json = jsonGenerator.toJson(o);
-
-
Field Summary
Fields Modifier and Type Field Description static Function<String,String>UNDERSCORE
-
Constructor Summary
Constructors Constructor Description JsonGenerator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> voidaddMapper(Class<T> type, Function<T,javax.json.JsonValue> mapper)Register a mapper for the specified typevoidaddMapper(Type type, Function<Object,javax.json.JsonValue> mapper)Register a mapper for the specified typejavax.json.JsonPatchaddPatch(String path, Object o)Create a JsonPatch with a single operation that will add the specified object as JSON to the specified json pathjavax.json.JsonObjectBuilderaddPatchOperation(String path, Object o)Create a JsonPatch operation that will add the specified object as JSON to the specified json pathprotected StringgetFieldName(Method method)javax.json.JsonObjectobjectToJson(Object pojo)javax.json.JsonValuetoJson(Object pojo)Used to map a Java object to a Json structureprotected StringtransformName(String propertyName)javax.json.JsonPatchupdatePatch(String path, Object o)Create a JsonPatch that set all non-null properties of the object at corresponding json paths.JsonGeneratorwithNameTransformer(Function<String,String> transformer)
-
-
-
Method Detail
-
addMapper
public <T> void addMapper(Class<T> type, Function<T,javax.json.JsonValue> mapper)
Register a mapper for the specified type
-
addMapper
public void addMapper(Type type, Function<Object,javax.json.JsonValue> mapper)
Register a mapper for the specified type
-
withNameTransformer
public JsonGenerator withNameTransformer(Function<String,String> transformer)
-
toJson
public javax.json.JsonValue toJson(Object pojo)
Used to map a Java object to a Json structure
-
objectToJson
public javax.json.JsonObject objectToJson(Object pojo)
-
addPatch
public javax.json.JsonPatch addPatch(String path, Object o)
Create a JsonPatch with a single operation that will add the specified object as JSON to the specified json path
-
addPatchOperation
public javax.json.JsonObjectBuilder addPatchOperation(String path, Object o)
Create a JsonPatch operation that will add the specified object as JSON to the specified json path
-
updatePatch
public javax.json.JsonPatch updatePatch(String path, Object o)
Create a JsonPatch that set all non-null properties of the object at corresponding json paths. E.g. if the object has properties firstName and lastName, callingupdatePatch("/person", o)will return a patch with[ {op: "add", path: "/person/firstName", value: .... }, {op: "add", path: "/person/lastName", value: .... } ]
-
-