Class 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);
     
    • Constructor Detail

      • JsonGenerator

        public JsonGenerator()
    • 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
      • 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)
      • getFieldName

        protected String getFieldName​(Method method)
      • transformName

        protected String transformName​(String propertyName)
      • 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, calling updatePatch("/person", o) will return a patch with [ {op: "add", path: "/person/firstName", value: .... }, {op: "add", path: "/person/lastName", value: .... } ]