001package com.avaje.ebean.text.json; 002 003import com.avaje.ebean.text.PathProperties; 004import com.fasterxml.jackson.core.JsonGenerator; 005import com.fasterxml.jackson.core.JsonParser; 006 007import java.io.Reader; 008import java.io.Writer; 009import java.lang.reflect.Type; 010import java.util.List; 011 012/** 013 * Converts objects to and from JSON format. 014 */ 015public interface JsonContext { 016 017 /** 018 * Convert json string input into a Bean of a specific type. 019 * 020 * @throws JsonIOException When IOException occurs 021 */ 022 <T> T toBean(Class<T> rootType, String json) throws JsonIOException; 023 024 /** 025 * Convert json string input into a Bean of a specific type additionally using JsonReadOptions. 026 * 027 * @throws JsonIOException When IOException occurs 028 */ 029 <T> T toBean(Class<T> rootType, String json, JsonReadOptions options) throws JsonIOException; 030 031 /** 032 * Convert json reader input into a Bean of a specific type. 033 * 034 * @throws JsonIOException When IOException occurs 035 */ 036 <T> T toBean(Class<T> rootType, Reader json) throws JsonIOException; 037 038 /** 039 * Convert json reader input into a Bean of a specific type additionally using JsonReadOptions. 040 * 041 * @throws JsonIOException When IOException occurs 042 */ 043 <T> T toBean(Class<T> rootType, Reader json, JsonReadOptions options) throws JsonIOException; 044 045 /** 046 * Convert json parser input into a Bean of a specific type. 047 * 048 * @throws JsonIOException When IOException occurs 049 */ 050 <T> T toBean(Class<T> cls, JsonParser parser) throws JsonIOException; 051 052 /** 053 * Convert json parser input into a Bean of a specific type additionally using JsonReadOptions.. 054 * 055 * @throws JsonIOException When IOException occurs 056 */ 057 <T> T toBean(Class<T> cls, JsonParser parser, JsonReadOptions options) throws JsonIOException; 058 059 /** 060 * Convert json string input into a list of beans of a specific type. 061 * 062 * @throws JsonIOException When IOException occurs 063 */ 064 <T> List<T> toList(Class<T> rootType, String json) throws JsonIOException; 065 066 /** 067 * Convert json string input into a list of beans of a specific type additionally using JsonReadOptions. 068 * 069 * @throws JsonIOException When IOException occurs 070 */ 071 <T> List<T> toList(Class<T> rootType, String json, JsonReadOptions options) throws JsonIOException; 072 073 /** 074 * Convert json reader input into a list of beans of a specific type. 075 * 076 * @throws JsonIOException When IOException occurs 077 */ 078 <T> List<T> toList(Class<T> rootType, Reader json) throws JsonIOException; 079 080 /** 081 * Convert json reader input into a list of beans of a specific type additionally using JsonReadOptions. 082 * 083 * @throws JsonIOException When IOException occurs 084 */ 085 <T> List<T> toList(Class<T> rootType, Reader json, JsonReadOptions options) throws JsonIOException; 086 087 /** 088 * Convert json parser input into a list of beans of a specific type. 089 * 090 * @throws JsonIOException When IOException occurs 091 */ 092 <T> List<T> toList(Class<T> cls, JsonParser json) throws JsonIOException; 093 094 /** 095 * Convert json parser input into a list of beans of a specific type additionally using JsonReadOptions. 096 * 097 * @throws JsonIOException When IOException occurs 098 */ 099 <T> List<T> toList(Class<T> cls, JsonParser json, JsonReadOptions options) throws JsonIOException; 100 101 /** 102 * Use the genericType to determine if this should be converted into a List or 103 * bean. 104 * 105 * @throws JsonIOException When IOException occurs 106 */ 107 Object toObject(Type genericType, Reader json) throws JsonIOException; 108 109 /** 110 * Use the genericType to determine if this should be converted into a List or 111 * bean. 112 * 113 * @throws JsonIOException When IOException occurs 114 */ 115 Object toObject(Type genericType, String json) throws JsonIOException; 116 117 /** 118 * Use the genericType to determine if this should be converted into a List or 119 * bean. 120 * 121 * @throws JsonIOException When IOException occurs 122 */ 123 Object toObject(Type genericType, JsonParser jsonParser) throws JsonIOException; 124 125 /** 126 * Return the bean or collection as JSON string. 127 * 128 * @throws JsonIOException When IOException occurs 129 */ 130 String toJson(Object value) throws JsonIOException; 131 132 /** 133 * Write the bean or collection in JSON format to the writer. 134 * 135 * @throws JsonIOException When IOException occurs 136 */ 137 void toJson(Object value, Writer writer) throws JsonIOException; 138 139 /** 140 * Write the bean or collection to the JsonGenerator. 141 * 142 * @throws JsonIOException When IOException occurs 143 */ 144 void toJson(Object value, JsonGenerator generator) throws JsonIOException; 145 146 /** 147 * Return the bean or collection as JSON string using PathProperties. 148 * 149 * @throws JsonIOException When IOException occurs 150 */ 151 String toJson(Object value, PathProperties pathProperties) throws JsonIOException; 152 153 /** 154 * Write the bean or collection as json to the writer using the PathProperties. 155 */ 156 void toJson(Object value, Writer writer, PathProperties pathProperties) throws JsonIOException; 157 158 /** 159 * Write the bean or collection to the JsonGenerator using the PathProperties. 160 */ 161 void toJson(Object value, JsonGenerator generator, PathProperties pathProperties) throws JsonIOException; 162 163 /** 164 * Deprecated in favour of using PathProperties by itself. 165 * Write json to the JsonGenerator using the JsonWriteOptions. 166 */ 167 void toJson(Object value, JsonGenerator generator, JsonWriteOptions options) throws JsonIOException; 168 169 /** 170 * Deprecated in favour of using PathProperties by itself. 171 * With additional options. 172 * 173 * @throws JsonIOException When IOException occurs 174 */ 175 void toJson(Object value, Writer writer, JsonWriteOptions options) throws JsonIOException; 176 177 /** 178 * Deprecated in favour of using PathProperties by itself. 179 * Convert a bean or collection to json string. 180 * 181 * @throws JsonIOException When IOException occurs 182 */ 183 String toJson(Object value, JsonWriteOptions options) throws JsonIOException; 184 185 /** 186 * Return true if the type is known as an Entity bean or a List Set or 187 * Map of entity beans. 188 */ 189 boolean isSupportedType(Type genericType); 190 191 /** 192 * Create and return a new JsonGenerator for the given writer. 193 * 194 * @throws JsonIOException When IOException occurs 195 */ 196 JsonGenerator createGenerator(Writer writer) throws JsonIOException; 197 198 /** 199 * Create and return a new JsonParser for the given reader. 200 * 201 * @throws JsonIOException When IOException occurs 202 */ 203 JsonParser createParser(Reader reader) throws JsonIOException; 204 205 /** 206 * Return a helper that can write scalar types known to Ebean to Jackson. 207 * <p> 208 * Ebean has built in support for java8 and Joda types as well as the other 209 * standard JDK types like URI, URL, UUID etc. This is a fast simple way to 210 * write any of those types to Jackson. 211 * </p> 212 */ 213 JsonScalar getScalar(JsonGenerator generator); 214 215}