001package io.avaje.json.simple; 002 003import io.avaje.json.JsonAdapter; 004import io.avaje.json.JsonReader; 005import io.avaje.json.JsonWriter; 006import io.avaje.json.PropertyNames; 007import io.avaje.json.stream.JsonStream; 008 009import java.io.InputStream; 010import java.io.OutputStream; 011import java.io.Reader; 012import java.io.Writer; 013import java.util.List; 014import java.util.Map; 015 016/** 017 * A mapper for mapping to basic Java types. 018 * <p> 019 * This supports the basic Java types of String, Boolean, Integer, Long, Double and 020 * Maps and List of these. 021 * <p> 022 * For full support with more types and binding to custom types use avaje-jsonb instead. 023 * 024 * <h3>Example</h3> 025 * <pre>{@code 026 * 027 * static final SimpleMapper simpleMapper = SimpleMapper.builder().build(); 028 * 029 * Map<String, Long> map = new LinkedHashMap<>(); 030 * map.put("one", 45L); 031 * map.put("two", 93L); 032 * 033 * String asJson = simpleMapper.toJson(map); 034 * 035 * }</pre> 036 */ 037public interface SimpleMapper { 038 039 /** 040 * Create a new builder for SimpleMapper. 041 */ 042 static Builder builder() { 043 return new DSimpleMapperBuilder(); 044 } 045 046 /** 047 * Return a mapper for any json content. 048 */ 049 Type<Object> object(); 050 051 /** 052 * Return a mapper for json OBJECT content with more reading/writing options. 053 */ 054 Type<Map<String, Object>> map(); 055 056 /** 057 * Return a mapper for json ARRAY content with more reading/writing options. 058 */ 059 Type<List<Object>> list(); 060 061 /** 062 * Write the object to JSON string. 063 * <p> 064 * For options to write json content to OutputStream, Writer etc 065 * use {@link Type}. 066 * 067 * <pre>{@code 068 * 069 * var list = List.of(42, "hello"); 070 * 071 * var asJson = mapper.toJson(list); 072 * }</pre> 073 */ 074 String toJson(Object object); 075 076 /** 077 * Write the object to JsonWriter. 078 * <p> 079 * For options to write json content to OutputStream, Writer etc 080 * use {@link Type}. 081 */ 082 void toJson(Object object, JsonWriter jsonWriter); 083 084 /** 085 * Read the object from JSON string. 086 */ 087 Object fromJson(String json); 088 089 /** 090 * Read the object from JSON. 091 */ 092 Object fromJson(JsonReader jsonReader); 093 094 /** 095 * Read a Map from JSON OBJECT string. 096 * <p> 097 * Use {@link #map()} for more reading options. 098 */ 099 Map<String, Object> fromJsonObject(String json); 100 101 /** 102 * Read a Map from JSON OBJECT. 103 * <p> 104 * Use {@link #map()} for more reading options. 105 */ 106 Map<String, Object> fromJsonObject(JsonReader jsonReader); 107 108 /** 109 * Read a List from JSON ARRAY string. 110 * <p> 111 * Use {@link #list()} for more reading options. 112 */ 113 List<Object> fromJsonArray(String json); 114 115 /** 116 * Read a List from JSON ARRAY. 117 * <p> 118 * Use {@link #list()} for more reading options. 119 */ 120 List<Object> fromJsonArray(JsonReader jsonReader); 121 122 /** 123 * Return the property names as PropertyNames. 124 * <p> 125 * Provides the option of optimising the writing of json for property names 126 * by having them already escaped and encoded rather than as plain strings. 127 */ 128 PropertyNames properties(String... names); 129 130 /** 131 * Return a Type specific mapper for the given JsonAdapter. 132 * 133 * @param customAdapter The custom adapter to use. 134 * @param <T> The type of the class to map to/from json. 135 * @return The Type specific mapper. 136 */ 137 <T> Type<T> type(JsonAdapter<T> customAdapter); 138 139 default JsonExtract extract(Map<String, Object> map) { 140 return new DExtract(map); 141 } 142 143 /** 144 * Build the JsonNodeMapper. 145 */ 146 interface Builder { 147 148 /** 149 * Set the default JsonStream to use. 150 * <p> 151 * When not set this defaults to {@code JsonStream.builder().build()}. 152 * 153 * @see JsonStream#builder() 154 */ 155 Builder jsonStream(JsonStream jsonStream); 156 157 /** 158 * Build and return the JsonNodeMapper. 159 */ 160 SimpleMapper build(); 161 } 162 163 /** 164 * Reading and writing with all options such and InputStream, Reader etc. 165 */ 166 interface Type<T> { 167 168 /** 169 * Create a list type for this type. 170 */ 171 Type<List<T>> list(); 172 173 /** 174 * Create a map type with string keys and this type as the value type. 175 */ 176 Type<Map<String, T>> map(); 177 178 /** 179 * Read the return the value from the json content. 180 */ 181 T fromJson(String content); 182 183 /** 184 * Read the return the value from the reader. 185 */ 186 T fromJson(JsonReader reader); 187 188 /** 189 * Read the return the value from the json content. 190 */ 191 T fromJson(byte[] content); 192 193 /** 194 * Read the return the value from the reader. 195 */ 196 T fromJson(Reader reader); 197 198 /** 199 * Read the return the value from the inputStream. 200 */ 201 T fromJson(InputStream inputStream); 202 203 /** 204 * Return as json string. 205 */ 206 String toJson(T value); 207 208 /** 209 * Return as json string in pretty format. 210 */ 211 String toJsonPretty(T value); 212 213 /** 214 * Return the value as json content in bytes form. 215 */ 216 byte[] toJsonBytes(T value); 217 218 /** 219 * Write to the given writer. 220 */ 221 void toJson(T value, JsonWriter writer); 222 223 /** 224 * Write to the given writer. 225 */ 226 void toJson(T value, Writer writer); 227 228 /** 229 * Write to the given outputStream. 230 */ 231 void toJson(T value, OutputStream outputStream); 232 233 } 234}