Interface JsonType<T>

  • All Superinterfaces:
    JsonView<T>

    public interface JsonType<T>
    extends JsonView<T>
    Provides API to serialise a type to and from JSON.

    JsonType provides the main API used to read and write json.

    fromJson

    Read json content from: String, byte[], Reader, InputStream, JsonReader

    
    
      JsonType<Customer> customerType = jsonb.type(Customer.class);
    
      Customer customer = customerType.fromJson(content);
    
     

    toJson

    Write json content to: String, byte[], Writer, OutputStream, JsonWriter

    
    
      JsonType<Customer> customerType = jsonb.type(Customer.class);
    
      String asJson = customerType.toJson(customer);
    
     

    Moshi note: JsonType does not exist in Moshi and has been added to provide a slightly nicer API to use than JsonAdapter.

    • Method Detail

      • view

        JsonView<T> view​(String dsl)
        Build and return the view given the DSL that specifies the properties to include.

        Examples of json view DSL:

        
        
           // only include the id and name properties
           (id, name)
        
           // include billAddress which is a nested type
           (id, name, billingAddress(street, suburb))
        
           // include billAddress with all it's properties
           (id, name, billingAddress(*))
        
           (id, name, billingAddress(street, suburb), shippingAddress(*), contacts(email,lastName, firstName))
        
         
      • list

        JsonType<List<T>> list()
        Return the list type for this JsonType.
      • stream

        JsonType<Stream<T>> stream()
        Prefer use of stream(JsonReader) rather than using this stream type directly.

        Generally, we should not use this type directly but instead create a JsonReader and use stream(JsonReader) instead. Then we just use a try-with-resources on the JsonReader and can additionally specify JsonReader.streamArray(boolean) option.

        When using this Stream type directly, use a try-with-resources block with the Stream to ensure that any underlying resources are closed.

        
        
          JsonType<Stream<MyBean>> type =  jsonb.type(MyBean.class).stream();
        
          try (Stream<MyBean> asStream = type.fromJson(content)) {
            // use the stream
            ...
          }
        
         
        Returns:
        The stream type for this base JsonType.
        See Also:
        stream(JsonReader)
      • set

        JsonType<Set<T>> set()
        Return the set type for this JsonType.
      • map

        JsonType<Map<String,​T>> map()
        Return the map with this type as the value type and string keys.
      • fromJson

        T fromJson​(JsonReader reader)
        Read the return the value from the reader.
      • fromJson

        T fromJson​(String content)
        Read the return the value from the json content.
      • fromJson

        T fromJson​(byte[] content)
        Read the return the value from the json content.
      • fromJson

        T fromJson​(Reader reader)
        Read the return the value from the reader.
      • fromJson

        T fromJson​(InputStream inputStream)
        Read the return the value from the inputStream.
      • fromObject

        T fromObject​(Object value)
        Convert from 'object form' expecting Map<String,Object> for 'json object' and expecting Collection<?> for 'json array'.
        Parameters:
        value - The json value that will be converted into T.
        Returns:
        The value converted from 'object form'.
      • stream

        Stream<T> stream​(JsonReader reader)
        Return as a Stream that will read the content as the stream is processed.

        Use a try-with-resources block with the JsonReader to ensure the underlying resources are closed.

        
        
          JsonType<MyBean> type =  jsonb.type(MyBean.class);
        
          try (JsonReader reader = jsonb.reader(content)) {
        
            Stream<MyBean> asStream = type.stream(reader);
            ...
          }
        
         

        When using Jackson

        When using Jackson-core as the underlying parser we should explicitly state that the content is either an ARRAY (with '[' and ']' tokens or not (x-json-stream new line delimited, there are no '[' and ']' tokens).

        When using the builtin avaje-jsonb parser, it automatically detects and handles both cases (with or without the '[' and ']' tokens). With the Jackson-core parser we need to explicitly state if we are processing

        
        
          JsonType<MyBean> type =  jsonb.type(MyBean.class);
        
          try (JsonReader reader = jsonb.reader(content)) {
            // when the content contains the ARRAY '[', ']' tokens set streamArray(true)
            Stream<MyBean> asStream = type.stream(reader.streamArray(true));
            ...
          }