Class MarshallerMethods
- java.lang.Object
-
- io.toro.martini.MarshallerMethods
-
public final class MarshallerMethods extends Object
-
-
Constructor Summary
Constructors Constructor Description MarshallerMethods(io.toro.martini.xml.XMLObjectConverter xmlObjectConverter)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
asJSON(Object o)
Marshal an object into a JSON string.static String
asXML(Object o)
Marshal an object into a JSON string.static <T> T
fromJSON(File jsonFile, Class<T> returnType)
Unmarshal the JSON content of a file to an instance of the provided type.static <T> T
fromJSON(InputStream jsonInputStream, Class<T> returnType)
Unmarshal the JSON content of a stream to an instance of the provided type.static <T> T
fromJSON(Reader jsonReader, Class<T> returnType)
Unmarshal the JSON content of a reader to an instance of the provided type.static <T> T
fromJSON(String jsonString, Class<T> returnType)
Unmarshal a JSON string to an instance of the provided type.static <T> T
fromJSON(URL jsonURL, Class<T> returnType)
Unmarshal the JSON document located in the provided URL, to an instance of the provided type.static <T> T
fromXML(File xmlFile, Class<T> returnType)
Unmarshal the XML content of a file to an instance of the provided type.static <T> T
fromXML(InputStream xmlInputStream, Class<T> returnType)
Unmarshal the XML content of a stream to an instance of the provided type.static <T> T
fromXML(Reader xmlReader, Class<T> returnType)
Unmarshal the XML content of a reader to an instance of the provided type.static <T> T
fromXML(String xmlString, Class<T> returnType)
Unmarshal an XML string to an instance of the provided type.static <T> T
fromXML(URL xmlURL, Class<T> returnType)
Unmarshal the XML document located in the provided URL, to an instance of the provided type.
-
-
-
Constructor Detail
-
MarshallerMethods
@Autowired public MarshallerMethods(io.toro.martini.xml.XMLObjectConverter xmlObjectConverter)
-
-
Method Detail
-
asJSON
public static String asJSON(Object o)
Marshal an object into a JSON string.
Example usage:
Person person = new Person( firstname: 'John', lastname: 'Doe' ) String json = person.asJSON()
- Parameters:
o
- the object to be marshalled- Returns:
- the pretty-printed JSON string representation of the provided object
- Since:
- 1.0
-
asXML
public static String asXML(Object o)
Marshal an object into a JSON string.
Example usage:
Person person = new Person( firstname: 'John', lastname: 'Doe' ) String json = person.asXML()
- Parameters:
o
- the object to be marshalled- Returns:
- the pretty-printed XML string representation of the provided object
- Since:
- 1.0
-
fromXML
public static <T> T fromXML(String xmlString, Class<T> returnType)
Unmarshal an XML string to an instance of the provided type.
Example usage:
String xml = ... Person person = xml.fromXML( Person.class )
- Parameters:
xmlString
- the XML string to unmarshalreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the provided XML string
- Since:
- 1.0
-
fromXML
public static <T> T fromXML(File xmlFile, Class<T> returnType)
Unmarshal the XML content of a file to an instance of the provided type.
Example usage:
File xml = new File( 'path/to/person.xml' ) Person person = xml.fromXML( Person.class )
- Parameters:
xmlFile
- the file containing the XML string to unmarshalreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the content of the provided XML file
- Since:
- 1.0
-
fromXML
public static <T> T fromXML(InputStream xmlInputStream, Class<T> returnType)
Unmarshal the XML content of a stream to an instance of the provided type.
Example usage:
try ( InputStream xml = ... ) { Person person = xml.fromXML( Person.class ) }
- Parameters:
xmlInputStream
- the stream containing the XML string to unmarshalreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the content of the provided XML input stream
- Since:
- 1.0
-
fromXML
public static <T> T fromXML(Reader xmlReader, Class<T> returnType)
Unmarshal the XML content of a reader to an instance of the provided type.
Example usage:
Reader xml = ... Person person = xml.fromXML( Person.class )
- Parameters:
xmlReader
- the reader containing the XML string to unmarshalreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the content of the provided XML reader
- Since:
- 1.0
-
fromXML
public static <T> T fromXML(URL xmlURL, Class<T> returnType)
Unmarshal the XML document located in the provided URL, to an instance of the provided type.
Example usage:
URL xml = new URL( 'https://www.w3schools.com/xml/plant_catalog.xml' ) Person person = xml.fromXML( Person.class )
- Parameters:
xmlURL
- the URL pointing to the XML document which will be unmarshalledreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the content of the provided XML document
- Since:
- 1.0
-
fromJSON
public static <T> T fromJSON(String jsonString, Class<T> returnType)
Unmarshal a JSON string to an instance of the provided type.
Example usage:
String json = ... Person person = json.fromJSON( Person.class )
- Parameters:
jsonString
- the JSON string to unmarshalreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the provided JSON string
- Since:
- 1.0
-
fromJSON
public static <T> T fromJSON(File jsonFile, Class<T> returnType)
Unmarshal the JSON content of a file to an instance of the provided type.
Example usage:
File json = new File( 'path/to/person.json' ) Person person = json.fromJSON( Person.class )
- Parameters:
jsonFile
- the file containing the JSON string to unmarshalreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the content of the provided JSON file
- Since:
- 1.0
-
fromJSON
public static <T> T fromJSON(InputStream jsonInputStream, Class<T> returnType)
Unmarshal the JSON content of a stream to an instance of the provided type.
Example usage:
try ( InputStream json = ... ) { Person person = json.fromJSON( Person.class ) }
- Parameters:
jsonInputStream
- the stream containing the JSON string to unmarshalreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the content of the provided JSON input stream
- Since:
- 1.0
-
fromJSON
public static <T> T fromJSON(Reader jsonReader, Class<T> returnType)
Unmarshal the JSON content of a reader to an instance of the provided type.
Example usage:
Reader json = ... Person person = json.fromJSON( Person.class )
- Parameters:
jsonReader
- the reader containing the JSON string to unmarshalreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the content of the provided JSON reader
- Since:
- 1.0
-
fromJSON
public static <T> T fromJSON(URL jsonURL, Class<T> returnType)
Unmarshal the JSON document located in the provided URL, to an instance of the provided type.
Example usage:
URL json = new URL( 'https://api.github.com/users/me/repos' ) Person person = json.fromJSON( Person.class )
- Parameters:
jsonURL
- the URL pointing to the JSON document which will be unmarshalledreturnType
- the type of object where unmarshalled data shall be put into- Returns:
- an instance of the specified class, whose data is based on the content of the provided JSON document
- Since:
- 1.0
-
-