Package com.compomics.util.io.json
Class JsonMarshaller
java.lang.Object
com.compomics.util.experiment.personalization.ExperimentObject
com.compomics.util.io.json.JsonMarshaller
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
IdentificationParametersMarshaller,PrideMarshaller,PTMFactoryMarshaller
This class converts non-specific objects to the JSON format and vice versa.
- Author:
- Kenneth Verheggen, Marc Vaudel, Harald Barsnes
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final com.google.gson.GsonBuilderGsonBuilder that can be used to append interfaces so the parser knows how to handle them.protected com.google.gson.GsonGSON parser instance to convert JSON to Java objects and back.Fields inherited from class com.compomics.util.experiment.personalization.ExperimentObject
NO_KEY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConvert from JSON to object.Convert from JSON to object.Convert from JSON to object.Convert from JSON to object.Convert from JSON to object.Convert from JSON to object.protected StringgetJsonStringFromFile(File jsonFile) Convert JSON string from file.protected voidinit()Initializes the marshaller with (custom) type adapters.voidsaveObjectToJson(Object anObject, File jsonFile) Save an object to JSON.Convert an object to JSON.Methods inherited from class com.compomics.util.experiment.personalization.ExperimentObject
addUrParam, asLong, clearParametersMap, getId, getUrParam, getUrParams, removeUrParam, setId, setUrParams
-
Field Details
-
gson
protected com.google.gson.Gson gsonGSON parser instance to convert JSON to Java objects and back. -
builder
protected final com.google.gson.GsonBuilder builderGsonBuilder that can be used to append interfaces so the parser knows how to handle them.
-
-
Constructor Details
-
JsonMarshaller
public JsonMarshaller()Default constructor. -
JsonMarshaller
Constructor.- Parameters:
interfaces- comma separated list of interfaces that this class is using. They will automatically be added using a custom InterfaceAdapter.
-
-
Method Details
-
init
protected void init()Initializes the marshaller with (custom) type adapters. -
toJson
Convert an object to JSON.- Parameters:
anObject- the input object- Returns:
- the JSON representation of an object
-
saveObjectToJson
Save an object to JSON.- Parameters:
anObject- the input objectjsonFile- the target file to which the JSON will be saved- Throws:
IOException- if the object cannot be successfully saved into a JSON file
-
fromJson
Convert from JSON to object.- Parameters:
objectType- the class the object belongs tojsonString- the string representation of the JSON object- Returns:
- an instance of the objectType containing the JSON information
-
fromJson
Convert from JSON to object.- Parameters:
objectType- the class the object belongs tojsonFile- a JSON file- Returns:
- an instance of the objectType containing the JSON information
- Throws:
IOException- if the object cannot be successfully read from a JSON file
-
fromJson
Convert from JSON to object.- Parameters:
objectType- the class the object belongs tojsonURL- a JSON URL- Returns:
- an instance of the objectType containing the JSON information
- Throws:
IOException- if the object cannot be successfully read from a JSON file
-
fromJson
Convert from JSON to object.- Parameters:
objectType- the typetoken the object belongs tojsonString- the string representation of the JSON object- Returns:
- an instance of the objectType containing the JSON information
-
fromJson
Convert from JSON to object.- Parameters:
objectType- the typetoken the object belongs tojsonFile- a JSON file- Returns:
- an instance of the objectType containing the JSON information
- Throws:
IOException- if the object cannot be successfully read from a JSON file
-
fromJson
Convert from JSON to object.- Parameters:
objectType- the typetoken the object belongs tojsonURL- a JSON URL- Returns:
- an instance of the objectType containing the JSON information
- Throws:
IOException- if the object cannot be successfully read from a JSON file
-
getJsonStringFromFile
Convert JSON string from file.- Parameters:
jsonFile- the input JSON file- Returns:
- the string representation of the JSON content
- Throws:
FileNotFoundException- if the JSON file can not be reachedIOException- if the object cannot be successfully read from a JSON file
-