public final class MarshallerMethods extends Object
A TORO utility class that uses Jackson/JAXB marshaller to convert object to and from Json/Xml String. JSON (JavaScript Object Notation) is a lightweight data-interchange format. XML (eXtensible Markup Language) is a markup language much like HTML.
Modifier and Type | Method and Description |
---|---|
static String |
asJSON(Object o)
Use the ESB pre-configured Jackson/JAXB marshaller to convert an object to a json string
|
static String |
asXML(Object o)
Use the ESB pre-configured Jackson/JAXB marshaller to convert an object to a json string
|
static <T> T |
fromJSON(File jsonFile,
Class<T> returnType)
Converts a JSON file to the specified return type.
|
static <T> T |
fromJSON(InputStream jsonInputStream,
Class<T> returnType)
Converts a JSON from an InputStream to a specified class.
|
static <T> T |
fromJSON(Reader jsonReader,
Class<T> returnType)
Converts a JSON from a reader to a specified class.
|
static <T> T |
fromJSON(String jsonString,
Class<T> returnType)
Convert a JSON String to the specified return type.
|
static <T> T |
fromJSON(URL jsonURL,
Class<T> returnType)
Converts a JSON data returned by the given URL to a class.
|
static <T> T |
fromXML(File xmlFile,
Class<T> returnType)
Converts an XML file to the specified return type.
|
static <T> T |
fromXML(InputStream xmlInputStream,
Class<T> returnType)
Converts an XML from an InputStream to a specified class.
|
static <T> T |
fromXML(Reader xmlReader,
Class<T> returnType)
Converts an XML from a reader to a specified class.
|
static <T> T |
fromXML(String xmlString,
Class<T> returnType)
Convert an XML string to the specified return type.
|
static <T> T |
fromXML(URL xmlURL,
Class<T> returnType)
Converts an XML data returned by the given URL will be unmarshalled.
|
public static String asJSON(Object o) throws ToroException
Use the ESB pre-configured Jackson/JAXB marshaller to convert an object to a json string
Example usage:
Dummy dummy = new Dummy( firstname: 'John', lastname: 'Doe' ) def json = dummy.asJSON()
o
- The object to be marshalled as JSON string.ToroException
public static String asXML(Object o) throws ToroException
Use the ESB pre-configured Jackson/JAXB marshaller to convert an object to a json string
Example usage:
Dummy dummy = new Dummy( firstname: 'John', lastname: 'Doe' ) def xml = dummy.asXML()
o
- The object to be marshalled as XMLToroException
public static <T> T fromXML(String xmlString, Class<T> returnType) throws ToroException
Convert an XML string to the specified return type.
Example usage:
xml.fromXML( Dummy.class )
xmlString
- The XML string to be unmarshalled into a class.returnType
- The class to be returned after unmarshalling the XML string.ToroException
public static <T> T fromXML(File xmlFile, Class<T> returnType) throws ToroException
Converts an XML file to the specified return type.
Example usage:
File xmlFile = new File( dummyXMLPath ) xmlFile.fromXML( Dummy.class )
xmlFile
- XML filereturnType
- The class where the XML file will be unmarshalled.ToroException
public static <T> T fromXML(InputStream xmlInputStream, Class<T> returnType) throws ToroException
Converts an XML from an InputStream to a specified class.
Example usage:
InputStream input = new ByteArrayInputStream( json.getBytes() ) def jsonFromInputStream = input.fromJSON( Dummy.class ) input.close()
xmlInputStream
- XML data from an InputStream.returnType
- The class where the InputStream will be unmarshalled.ToroException
public static <T> T fromXML(Reader xmlReader, Class<T> returnType) throws ToroException
Converts an XML from a reader to a specified class.
Example usage:
StringReader reader = new StringReader( json ) def xmlFromReader = reader.fromXML( Dummy.class )
xmlReader
- XML from a Reader or its subclassesreturnType
- The class where the xmlReader will be unmarshalled.ToroException
public static <T> T fromXML(URL xmlURL, Class<T> returnType) throws ToroException
Converts an XML data returned by the given URL will be unmarshalled.
Example usage:
URL url = new URL( 'https://www.w3schools.com/xml/plant_catalog.xml' ) Catalog plantCatalog = url.fromXML( Catalog.class )
xmlURL
- The URL for the XML data.returnType
- The class where the returned data by the URL will be unmarshalled.ToroException
public static <T> T fromJSON(String jsonString, Class<T> returnType) throws ToroException
Convert a JSON String to the specified return type.
Example usage:
json.fromJSON( Dummy.class )
jsonString
- The JSON string to be unmarshalled into a class.returnType
- The class to be returned after unmarshalling the JSON string.ToroException
public static <T> T fromJSON(File jsonFile, Class<T> returnType) throws ToroException
Converts a JSON file to the specified return type.
Example usage:
File jsonFile = new File( dummyJSONPath ) jsonFile.fromJSON( Dummy.class )
jsonFile
- JSON file.returnType
- The class where the JSON file will be unmarshalled.ToroException
public static <T> T fromJSON(InputStream jsonInputStream, Class<T> returnType) throws ToroException
Converts a JSON from an InputStream to a specified class.
Example usage:
InputStream input = new ByteArrayInputStream( json.getBytes() ) def jsonFromInputStream = input.fromJSON( Dummy.class ) input.close()
jsonInputStream
- JSON data from an InputStreamreturnType
- The class where the InputStream will be unmarshalled.ToroException
public static <T> T fromJSON(Reader jsonReader, Class<T> returnType) throws ToroException
Converts a JSON from a reader to a specified class.
Example usage:
StringReader reader = new StringReader( json ) def jsonReader = reader.jsonReader( Dummy.class ) input.close()
jsonReader
- JSON from a Reader or its subclassesreturnType
- The class where the jsonReader will be unmarshalled.ToroException
public static <T> T fromJSON(URL jsonURL, Class<T> returnType) throws ToroException
Converts a JSON data returned by the given URL to a class.
Example usage:
URL url = new URL( 'http://echo.jsontest.com/firstname/John/lastname/Doe' ) url.fromJSON( Dummy.class )
jsonURL
- The URL for the JSON data.returnType
- The class where the returned data by the URL will be unmarshalled.ToroException
Copyright © 2019. All rights reserved.