Interface SimpleMapper


public interface SimpleMapper
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.

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

Example



   static final SimpleMapper simpleMapper = SimpleMapper.builder().build();

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

   String asJson = simpleMapper.toJson(map);

 
  • Method Details

    • builder

      static SimpleMapper.Builder builder()
      Create a new builder for SimpleMapper.
    • 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 SimpleMapper.Type.

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

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

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

      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.

    • 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> SimpleMapper.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.