Package com.gengoai.json
Class Json
- java.lang.Object
-
- com.gengoai.json.Json
-
public final class Json extends Object
Convenience methods for serializing and deserializing objects to and from json and creating json reader and writers.
- Author:
- David B. Bracewell
-
-
Field Summary
Fields Modifier and Type Field Description static com.fasterxml.jackson.databind.ObjectMapper
MAPPER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static JsonEntry
asJsonEntry(Object o)
static Resource
dump(Object object, @NonNull Resource resource)
Dumps the given object to the given output location in json format.static Resource
dumpPretty(Object object, @NonNull Resource resource)
Dumps the given object to the given output location in json format.static String
dumps(Object object)
Dumps the given object to a string in json format.static String
dumpsPretty(Object object)
Dumps the given object to a string in json format.static JsonEntry
parse(@NonNull Resource json)
Parses the given resource as json entrystatic <T> T
parse(@NonNull Resource resource, @NonNull Type type)
Parses the json in the given resource creating an object of the given class type.static <T> T
parse(@NonNull String json, @NonNull Type type)
Parses the json in the given resource creating an object of the given class type.static JsonEntry
parse(String json)
Parses the json string.static List<JsonEntry>
parseArray(@NonNull Resource resource)
Loads an array of objects from the given resource in json format.static List<JsonEntry>
parseArray(@NonNull String json)
Parses a json string into a list ofJsonEntry
static Map<String,JsonEntry>
parseObject(Resource json)
Quicker method for parsing a json string into aMap
ofString
keys andObject
values.static Map<String,JsonEntry>
parseObject(String json)
Quicker method for parsing a json string into aMap
ofString
keys andObject
values.static com.fasterxml.jackson.databind.JavaType
typeToJavaType(Type type)
-
-
-
Method Detail
-
dump
public static Resource dump(Object object, @NonNull @NonNull Resource resource) throws IOException
Dumps the given object to the given output location in json format.- Parameters:
object
- the object to dumpresource
- the resource to write the dumped object in json format to.- Returns:
- the resource
- Throws:
IOException
- Something went wrong writing to the given resource
-
dumpPretty
public static Resource dumpPretty(Object object, @NonNull @NonNull Resource resource) throws IOException
Dumps the given object to the given output location in json format.- Parameters:
object
- the object to dumpresource
- the resource to write the dumped object in json format to.- Returns:
- the resource
- Throws:
IOException
- Something went wrong writing to the given resource
-
dumps
public static String dumps(Object object)
Dumps the given object to a string in json format.- Parameters:
object
- the object to dump- Returns:
- the object as a json string
-
dumpsPretty
public static String dumpsPretty(Object object)
Dumps the given object to a string in json format.- Parameters:
object
- the object to dump- Returns:
- the object as a json string
-
parse
public static JsonEntry parse(String json) throws IOException
Parses the json string.- Parameters:
json
- the json string to parse- Returns:
- the parsed string as a json entry
- Throws:
IOException
- something went wrong parsing the json.
-
parse
public static <T> T parse(@NonNull @NonNull String json, @NonNull @NonNull Type type) throws IOException
Parses the json in the given resource creating an object of the given class type.- Type Parameters:
T
- the class type parameter- Parameters:
json
- the json to parsetype
- the class information for the object to deserialized- Returns:
- the deserialized object
- Throws:
IOException
- something went wrong reading the json
-
parse
public static JsonEntry parse(@NonNull @NonNull Resource json) throws IOException
Parses the given resource as json entry- Parameters:
json
- the resource to read from- Returns:
- the parsed resource as a json entry
- Throws:
IOException
- Something went wrong parsing the resource
-
parse
public static <T> T parse(@NonNull @NonNull Resource resource, @NonNull @NonNull Type type) throws IOException
Parses the json in the given resource creating an object of the given class type.- Type Parameters:
T
- the class type parameter- Parameters:
resource
- the resource to read fromtype
- the class information for the object to deserialized- Returns:
- the deserialized object
- Throws:
IOException
- something went wrong reading the json
-
parseArray
public static List<JsonEntry> parseArray(@NonNull @NonNull Resource resource) throws IOException
Loads an array of objects from the given resource in json format.- Parameters:
resource
- the resource to read from- Returns:
- the list of objects read in from the resource
- Throws:
IOException
- Something went wrong reading from the resource
-
parseArray
public static List<JsonEntry> parseArray(@NonNull @NonNull String json) throws IOException
Parses a json string into a list ofJsonEntry
- Parameters:
json
- the json string to load the array from- Returns:
- the list of objects parsed from the string
- Throws:
IOException
- Something went wrong parsing the json string
-
parseObject
public static Map<String,JsonEntry> parseObject(Resource json) throws IOException
Quicker method for parsing a json string into aMap
ofString
keys andObject
values.- Parameters:
json
- the json to load from- Returns:
- the map of representing the json object in the string
- Throws:
IOException
- Something went wrong parsing the json in the resource
-
parseObject
public static Map<String,JsonEntry> parseObject(String json) throws IOException
Quicker method for parsing a json string into aMap
ofString
keys andObject
values.- Parameters:
json
- the json to load from- Returns:
- the map of representing the json object in the string
- Throws:
IOException
- Something went wrong parsing the json string
-
typeToJavaType
public static com.fasterxml.jackson.databind.JavaType typeToJavaType(Type type)
-
-