Interface JsonMapper


public interface JsonMapper
A mapper for mapping to basic Java types.

This supports the basic Java types of String, Boolean, Integer, Long, Double and Maps and List of these.

If avaje-jsonb is available then you will use that and NOT use this JsonMapper at all. This JsonMapper is intended to be used by code that ONLY wants to depend on avaje-json-core and process the basic Java types or provide its own JsonAdapters.

For full support with more types and binding to custom types use avaje-jsonb instead.

Example



   static final JsonMapper jsonMapper = JsonMapper.builder().build();

   Map<String, Long> map = new LinkedHashMap<>();
   map.put("one", 45L);
   map.put("two", 93L);

   String asJson = jsonMapper.toJson(map);

 
  • Method Details

    • builder

      static JsonMapper.Builder builder()
      Create a new builder for JsonMapper.
    • object

      Return a mapper for any json content.
    • map

      Return a mapper for json OBJECT content with more reading/writing options.
    • list

      Return a mapper for json ARRAY content with more reading/writing options.
    • toJson

      String toJson(Object object)
      Write the object to JSON string.

      For options to write json content to OutputStream, Writer etc use JsonMapper.Type.

      
      
       var list = List.of(42, "hello");
      
       var asJson = mapper.toJson(list);
       
    • toJson

      void toJson(Object object, JsonWriter jsonWriter)
      Write the object to JsonWriter.

      For options to write json content to OutputStream, Writer etc use JsonMapper.Type.

    • fromJson

      Object fromJson(String json)
      Read the object from JSON string.
    • fromJson

      Object fromJson(JsonReader jsonReader)
      Read the object from JSON.
    • fromJsonObject

      Map<String,Object> fromJsonObject(String json)
      Read a Map from JSON OBJECT string.

      Use map() for more reading options.

    • fromJsonObject

      Map<String,Object> fromJsonObject(JsonReader jsonReader)
      Read a Map from JSON OBJECT.

      Use map() for more reading options.

    • fromJsonArray

      List<Object> fromJsonArray(String json)
      Read a List from JSON ARRAY string.

      Use list() for more reading options.

    • fromJsonArray

      List<Object> fromJsonArray(JsonReader jsonReader)
      Read a List from JSON ARRAY.

      Use list() for more reading options.

    • properties

      PropertyNames properties(String... names)
      Return the property names as PropertyNames.

      Provides the option of optimising the writing of json for property names by having them already escaped and encoded rather than as plain strings.

    • type

      <T> JsonMapper.Type<T> type(JsonAdapter<T> customAdapter)
      Return a Type specific mapper for the given JsonAdapter.
      Type Parameters:
      T - The type of the class to map to/from json.
      Parameters:
      customAdapter - The custom adapter to use.
      Returns:
      The Type specific mapper.
    • type

      <T> JsonMapper.Type<T> type(Function<JsonMapper,JsonAdapter<T>> adapterFunction)
      Return a Type specific mapper using a function that creates a JsonAdapter.

      Often the adapterFunction is the constructor of the custom JsonAdapter where the constructor takes JsonMapper as the only argument.

      Type Parameters:
      T - The type of the class to map to/from json.
      Parameters:
      adapterFunction - The function that creates a JsonAdapter.
      Returns:
      The Type specific mapper.
    • extract

      default JsonExtract extract(Map<String,Object> map)
      Return the map wrapped via JsonExtract to make extracting values easier.