All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.avaje.json.simple.SimpleMapper Maven / Gradle / Ivy

There is a newer version: 3.0-RC5
Show newest version
package io.avaje.json.simple;

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.json.PropertyNames;
import io.avaje.json.stream.JsonStream;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.List;
import java.util.Map;

/**
 * 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

*
{@code
 *
 *   static final SimpleMapper simpleMapper = SimpleMapper.builder().build();
 *
 *   Map map = new LinkedHashMap<>();
 *   map.put("one", 45L);
 *   map.put("two", 93L);
 *
 *   String asJson = simpleMapper.toJson(map);
 *
 * }
*/ public interface SimpleMapper { /** * Create a new builder for SimpleMapper. */ static Builder builder() { return new DSimpleMapperBuilder(); } /** * Return a mapper for any json content. */ Type object(); /** * Return a mapper for json OBJECT content with more reading/writing options. */ Type> map(); /** * Return a mapper for json ARRAY content with more reading/writing options. */ Type> list(); /** * Write the object to JSON string. *

* For options to write json content to OutputStream, Writer etc * use {@link Type}. * *

{@code
   *
   * var list = List.of(42, "hello");
   *
   * var asJson = mapper.toJson(list);
   * }
*/ String toJson(Object object); /** * Read the object from JSON string. */ Object fromJson(String json); /** * Read a Map from JSON OBJECT string. *

* Use {@link #map()} for more reading options. */ Map fromJsonObject(String json); /** * Read a List from JSON ARRAY string. *

* Use {@link #list()} for more reading options. */ List fromJsonArray(String json); /** * 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. */ PropertyNames properties(String... names); /** * Return a Type specific mapper for the given JsonAdapter. * * @param customAdapter The custom adapter to use. * @param The type of the class to map to/from json. * @return The Type specific mapper. */ Type type(JsonAdapter customAdapter); /** * Build the JsonNodeMapper. */ interface Builder { /** * Set the default JsonStream to use. *

* When not set this defaults to {@code JsonStream.builder().build()}. * * @see JsonStream#builder() */ Builder jsonStream(JsonStream jsonStream); /** * Build and return the JsonNodeMapper. */ SimpleMapper build(); } /** * Reading and writing with all options such and InputStream, Reader etc. */ interface Type { /** * Create a list type for this type. */ Type> list(); /** * Create a map type with string keys and this type as the value type. */ Type> map(); /** * Read the return the value from the json content. */ T fromJson(String content); /** * Read the return the value from the reader. */ T fromJson(JsonReader reader); /** * Read the return the value from the json content. */ T fromJson(byte[] content); /** * Read the return the value from the reader. */ T fromJson(Reader reader); /** * Read the return the value from the inputStream. */ T fromJson(InputStream inputStream); /** * Return as json string. */ String toJson(T value); /** * Return as json string in pretty format. */ String toJsonPretty(T value); /** * Return the value as json content in bytes form. */ byte[] toJsonBytes(T value); /** * Write to the given writer. */ void toJson(T value, JsonWriter writer); /** * Write to the given writer. */ void toJson(T value, Writer writer); /** * Write to the given outputStream. */ void toJson(T value, OutputStream outputStream); } }