public class JsonMarshaller extends Object
Modifier and Type | Field and Description |
---|---|
protected com.google.gson.GsonBuilder |
builder
GsonBuilder that can be used to append interfaces so the parser knows how
to handle them.
|
protected com.google.gson.Gson |
gson
GSON parser instance to convert JSON to Java objects and back.
|
Constructor and Description |
---|
JsonMarshaller()
Default constructor.
|
JsonMarshaller(Class... interfaces)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
Object |
fromJson(Class objectType,
File jsonFile)
Convert from JSON to object.
|
Object |
fromJson(Class objectType,
String jsonString)
Convert from JSON to object.
|
Object |
fromJson(Class objectType,
URL jsonURL)
Convert from JSON to object.
|
Object |
fromJson(Type objectType,
File jsonFile)
Convert from JSON to object.
|
Object |
fromJson(Type objectType,
String jsonString)
Convert from JSON to object.
|
Object |
fromJson(Type objectType,
URL jsonURL)
Convert from JSON to object.
|
protected String |
getJsonStringFromFile(File jsonFile)
Convert JSON string from file.
|
protected void |
init()
Initializes the marshaller with (custom) type adapters and date format
|
void |
saveObjectToJson(Object anObject,
File jsonFile)
Save an object to JSON.
|
String |
toJson(Object anObject)
Convert an object to JSON.
|
protected com.google.gson.Gson gson
protected final com.google.gson.GsonBuilder builder
public JsonMarshaller()
public JsonMarshaller(Class... interfaces)
interfaces
- comma separated list of interfaces that this class is
using. They will automatically be added using a custom InterfaceAdapterprotected void init()
public String toJson(Object anObject)
anObject
- the input objectpublic void saveObjectToJson(Object anObject, File jsonFile) throws IOException
anObject
- the input objectjsonFile
- the target file to which the JSON will be saved.IOException
- if the object cannot be successfully saved into a
JSON filepublic Object fromJson(Class objectType, String jsonString)
objectType
- the class the object belongs tojsonString
- the string representation of the JSON objectpublic Object fromJson(Class objectType, File jsonFile) throws IOException
objectType
- the class the object belongs tojsonFile
- a JSON fileIOException
- if the object cannot be successfully read from a JSON
filepublic Object fromJson(Class objectType, URL jsonURL) throws IOException
objectType
- the class the object belongs tojsonURL
- a JSON URLIOException
- if the object cannot be successfully read from a JSON
filepublic Object fromJson(Type objectType, String jsonString)
objectType
- the typetoken the object belongs tojsonString
- the string representation of the JSON objectpublic Object fromJson(Type objectType, File jsonFile) throws IOException
objectType
- the typetoken the object belongs tojsonFile
- a JSON fileIOException
- if the object cannot be successfully read from a JSON
filepublic Object fromJson(Type objectType, URL jsonURL) throws IOException
objectType
- the typetoken the object belongs tojsonURL
- a JSON URLIOException
- if the object cannot be successfully read from a JSON
fileprotected String getJsonStringFromFile(File jsonFile) throws FileNotFoundException, IOException
jsonFile
- the input JSON fileFileNotFoundException
- if the JSON file can not be reachedIOException
- if the object cannot be successfully read from a JSON
fileCopyright © 2016. All rights reserved.