de.hpi.fgis.dude.util.data.json
Class JsonUtil

java.lang.Object
  extended by de.hpi.fgis.dude.util.data.json.JsonUtil

public class JsonUtil
extends Object

Convenience methods for dealing with Jsonables.

Author:
Arvid Heise

Constructor Summary
JsonUtil()
           
 
Method Summary
static
<T extends AutoJsonable>
T
fromJson(String json, Class<T> type)
          Parses the given json string to an instance of the given Jsonable type.
static void readFields(DuDeJsonParser<?> jsonParser, AutoJsonable jsonable)
          Reads all non-transient fields of the jsonable to the DuDeJsonParser.
static String toJson(AutoJsonable jsonable)
          Serializes a Jsonable and returns the resulting json string.
static String toJson(AutoJsonable jsonable, boolean formatted)
          Serializes a Jsonable and returns the resulting json string.
static void writeFields(DuDeJsonGenerator jsonGenerator, AutoJsonable jsonable)
          Writes all non-transient fields of the jsonable to the DuDeJsonGenerator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonUtil

public JsonUtil()
Method Detail

toJson

public static String toJson(AutoJsonable jsonable,
                            boolean formatted)
Serializes a Jsonable and returns the resulting json string.

Parameters:
jsonable - the Jsonable to serialize.
formatted - if the output should be formatted
Returns:
the string representing the given Jsonable

toJson

public static String toJson(AutoJsonable jsonable)
Serializes a Jsonable and returns the resulting json string.

Parameters:
jsonable - the Jsonable to serialize.
Returns:
the string representing the given Jsonable

fromJson

public static <T extends AutoJsonable> T fromJson(String json,
                                                  Class<T> type)
Parses the given json string to an instance of the given Jsonable type.

Type Parameters:
T - the specific Jsonable type
Parameters:
json - the json string containing the representation of the new instance
type - the specific Jsonable type
Returns:
a new instance of the type initialized with the given json string

readFields

public static void readFields(DuDeJsonParser<?> jsonParser,
                              AutoJsonable jsonable)
                       throws org.codehaus.jackson.JsonParseException,
                              IOException
Reads all non-transient fields of the jsonable to the DuDeJsonParser. It expects the given jsonParser to currently have an opened record and it leaves it open.

Parameters:
jsonParser - the DuDeJsonParser to read from
jsonable - the AutoJsonable to initialize
Throws:
org.codehaus.jackson.JsonParseException - If an error occurred while parsing the data.
IOException - If an error occurred while reading the data.

writeFields

public static void writeFields(DuDeJsonGenerator jsonGenerator,
                               AutoJsonable jsonable)
                        throws org.codehaus.jackson.JsonGenerationException,
                               IOException
Writes all non-transient fields of the jsonable to the DuDeJsonGenerator. It expects the given jsonGenerator to currently have an opened record and it leaves it open.

Parameters:
jsonGenerator - the DuDeJsonGenerator to write to
jsonable - the AutoJsonable to write
Throws:
org.codehaus.jackson.JsonGenerationException - If an error occurs while generating the Json syntax.
IOException - If an error occurs while accessing the underlying stream.


Copyright © 2011 Hasso Plattner Institute - Chair of Information Systems. All Rights Reserved.