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

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<String,JsonValue>
          extended by de.hpi.fgis.dude.util.data.json.JsonRecord
All Implemented Interfaces:
AutoJsonable, JsonValue, Jsonable, Serializable, Cloneable, Comparable<JsonValue>, Map<String,JsonValue>

public class JsonRecord
extends HashMap<String,JsonValue>
implements JsonValue

JsonRecord represents a Json record.

Author:
Matthias Pohl
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface de.hpi.fgis.dude.util.data.json.JsonValue
JsonValue.JsonType
 
Constructor Summary
JsonRecord()
          Initializes a JsonRecord.
JsonRecord(int initialCapacity)
          Initializes a JsonRecord with the passed initial capacity.
JsonRecord(int initialCapacity, float loadFactor)
          Initializes a JsonRecord with the passed initial capacity and its load factor.
JsonRecord(Map<? extends String,? extends JsonValue> initialData)
          Initializes a new JsonRecord with the passed data.
 
Method Summary
 int compareTo(JsonValue other)
          Compares the current instance with the passed JsonValue.
static JsonRecord createJsonRecord(String jsonCode)
          Creates a JsonRecord instance based on the passed Json code.
 void fromJson(DuDeJsonParser<?> jsonParser)
          Initializes the current instance using the passed DuDeJsonParser.
 String generateString()
          Generates a concatenated String out of all values stored in this JsonRecord.
 JsonValue getJsonValue(String... path)
          Returns the JsonValue specified by the passed path or null if the specified attribute does not exist.
(package private)  JsonValue getJsonValue(String currentAttributeName, Iterator<String> path)
          Returns the value that corresponds to the passed path.
 JsonValue.JsonType getType()
          Returns JsonType.Record.
 JsonValue put(String key, boolean value)
          Puts the passed key and the corresponding JsonBoolean value into the JsonRecord.
 JsonValue put(String key, double value)
          Puts the passed key and the corresponding JsonNumber value into the JsonRecord.
 JsonValue put(String key, int value)
          Puts the passed key and the corresponding JsonNumber value into the JsonRecord.
 JsonValue put(String key, JsonValue value)
          Puts the passed key and JsonValue into the JsonRecord.
 JsonValue put(String key, long value)
          Puts the passed key and the corresponding JsonNumber value into the JsonRecord.
 JsonValue put(String key, String str)
          Puts the passed key and the corresponding JsonString value into the JsonRecord.
 JsonValue putCollection(String key, Collection<? extends JsonValue> collection)
          Puts a JsonArray generated out of the passed Collection and its key to this JsonRecord.
 JsonValue putMap(String key, Map<String,? extends JsonValue> map)
          Puts a JsonRecord generated out of the passed Map and its key to this JsonRecord.
 JsonValue putNull(String key)
          Puts the passed key and JsonNull into the JsonRecord .
 JsonValue searchFor(String attributeName)
          Returns the first occurrence of the passed attribute (using a recursive look-up).
 void toJson(DuDeJsonGenerator jsonGenerator)
          Generates the Json code using the passed DuDeJsonGenerator.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface de.hpi.fgis.dude.util.data.json.JsonValue
size
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

JsonRecord

public JsonRecord()
Initializes a JsonRecord.


JsonRecord

public JsonRecord(int initialCapacity)
Initializes a JsonRecord with the passed initial capacity.

Parameters:
initialCapacity - The initial capacity of the record.

JsonRecord

public JsonRecord(Map<? extends String,? extends JsonValue> initialData)
Initializes a new JsonRecord with the passed data.

Parameters:
initialData - The data that will be added during the initialization.

JsonRecord

public JsonRecord(int initialCapacity,
                  float loadFactor)
Initializes a JsonRecord with the passed initial capacity and its load factor.

Parameters:
initialCapacity - The initial capacity of the record.
loadFactor - The load factor of the record.
See Also:
HashMap.HashMap(int, float)
Method Detail

createJsonRecord

public static JsonRecord createJsonRecord(String jsonCode)
                                   throws ParseException
Creates a JsonRecord instance based on the passed Json code.

Parameters:
jsonCode - The code that shall be converted into a JsonRecord
Returns:
The newly created JsonRecord.
Throws:
ParseException - If the passed Json code cannot be converted into a JsonRecord.

getType

public JsonValue.JsonType getType()
Returns JsonType.Record.

Specified by:
getType in interface JsonValue
Returns:
The type of the current instance.
See Also:
JsonValue.getType()

compareTo

public int compareTo(JsonValue other)
Description copied from interface: JsonValue
Compares the current instance with the passed JsonValue. Instances of different JsonValue.JsonTypes are ordered lexicographically except for JsonValue.JsonType.Null. The above mentioned requirements define the following order: null == JsonValue.JsonType.Null < JsonValue.JsonType.Array < JsonValue.JsonType.Boolean < JsonValue.JsonType.Number < JsonValue.JsonType.Record < JsonValue.JsonType.String.

Specified by:
compareTo in interface JsonValue
Specified by:
compareTo in interface Comparable<JsonValue>
See Also:
Comparable.compareTo(Object)

searchFor

public JsonValue searchFor(String attributeName)
Returns the first occurrence of the passed attribute (using a recursive look-up).

Parameters:
attributeName - The name of the attribute whose value is requested.
Returns:
The value of the attribute or null if such an attribute was not found.

getJsonValue

public JsonValue getJsonValue(String... path)
Returns the JsonValue specified by the passed path or null if the specified attribute does not exist.

Parameters:
path - The path to the attribute.
Returns:
The corresponding JsonValue.

getJsonValue

JsonValue getJsonValue(String currentAttributeName,
                       Iterator<String> path)
Returns the value that corresponds to the passed path.

Parameters:
currentAttributeName - The currently observed attribute within the record. If this attribute does not exist, null will be returned.
path - The rest of the path.
Returns:
The corresponding value or null, if the path is invalid.

generateString

public String generateString()
Generates a concatenated String out of all values stored in this JsonRecord. This method should be used instead of AbstractMap.toString() if the enclosed values should be concatenated without any separators and key information.

Returns:
A concatenation of a all values stored in this JsonRecord.

put

public JsonValue put(String key,
                     JsonValue value)
Puts the passed key and JsonValue into the JsonRecord. If the passed value is null, JsonNull will be inserted instead.

Specified by:
put in interface Map<String,JsonValue>
Overrides:
put in class HashMap<String,JsonValue>
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

putNull

public JsonValue putNull(String key)
Puts the passed key and JsonNull into the JsonRecord .

Parameters:
key - The key which shall be added with JsonNull.
Returns:
The previous value associated with the passed key, or null if there was no mapping for this key.
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

put

public JsonValue put(String key,
                     boolean value)
Puts the passed key and the corresponding JsonBoolean value into the JsonRecord. If the passed value is null, JsonNull will be inserted instead.

Parameters:
key - The key that shall be added.
value - The boolean value whose Json representation shall be put into the JsonRecord.
Returns:
The previous value associated with the passed key, or null if there was no mapping for this key.
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

putCollection

public JsonValue putCollection(String key,
                               Collection<? extends JsonValue> collection)
Puts a JsonArray generated out of the passed Collection and its key to this JsonRecord. If the passed value is null, JsonNull will be used instead.

Parameters:
key - The key that shall be put into the JsonRecord.
collection - The Collection that shall be converted into a JsonArray and put into the JsonRecord.
Returns:
The previous value associated with the passed key, or null if there was no mapping for this key.
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

put

public JsonValue put(String key,
                     int value)
Puts the passed key and the corresponding JsonNumber value into the JsonRecord. If the passed value is null, JsonNull will be inserted instead.

Parameters:
key - The key that shall be added.
value - The integer value whose Json representation shall be put into the JsonRecord.
Returns:
The previous value associated with the passed key, or null if there was no mapping for this key.
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

put

public JsonValue put(String key,
                     long value)
Puts the passed key and the corresponding JsonNumber value into the JsonRecord. If the passed value is null, JsonNull will be inserted instead.

Parameters:
key - The key that shall be added.
value - The long value whose Json representation shall be put into the JsonRecord.
Returns:
The previous value associated with the passed key, or null if there was no mapping for this key.
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

put

public JsonValue put(String key,
                     double value)
Puts the passed key and the corresponding JsonNumber value into the JsonRecord. If the passed value is null, JsonNull will be inserted instead.

Parameters:
key - The key that shall be added.
value - The double value whose Json representation shall be put into the JsonRecord.
Returns:
The previous value associated with the passed key, or null if there was no mapping for this key.
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

putMap

public JsonValue putMap(String key,
                        Map<String,? extends JsonValue> map)
Puts a JsonRecord generated out of the passed Map and its key to this JsonRecord. If the passed value is null, JsonNull will be used instead.

Parameters:
key - The key that shall be put into the JsonRecord.
map - The Map that shall be converted into a JsonRecord and put into this JsonRecord.
Returns:
The previous value associated with the passed key, or null if there was no mapping for this key.
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

put

public JsonValue put(String key,
                     String str)
Puts the passed key and the corresponding JsonString value into the JsonRecord. If the passed value is null, JsonNull will be inserted instead.

Parameters:
key - The key that shall be added.
str - The String whose Json representation shall be put into the JsonRecord.
Returns:
The previous value associated with the passed key, or null if there was no mapping for this key.
Throws:
NullPointerException - If null was passed instead of a key.
See Also:
HashMap.put(Object, Object)

toJson

public void toJson(DuDeJsonGenerator jsonGenerator)
            throws org.codehaus.jackson.JsonGenerationException,
                   IOException
Description copied from interface: Jsonable
Generates the Json code using the passed DuDeJsonGenerator.

Specified by:
toJson in interface Jsonable
Parameters:
jsonGenerator - The DuDeJsonGenerator that is used internally.
Throws:
org.codehaus.jackson.JsonGenerationException - If an error occurs while generating the Json syntax.
IOException - If an error occurs while writing to the output.

fromJson

public void fromJson(DuDeJsonParser<?> jsonParser)
              throws org.codehaus.jackson.JsonParseException,
                     IOException
Description copied from interface: Jsonable
Initializes the current instance using the passed DuDeJsonParser.

Specified by:
fromJson in interface Jsonable
Parameters:
jsonParser - The parser that is used for extracting the data out of the Json.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurs while parsing the Json.
IOException - If an error occurs while reading from the stream.


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