org.shredzone.flattr4j.connector
Class FlattrObject

java.lang.Object
  extended by org.shredzone.flattr4j.connector.FlattrObject
All Implemented Interfaces:
Externalizable, Serializable

public class FlattrObject
extends Object
implements Serializable, Externalizable

Represents the raw Flattr data.

Basically, this is a wrapper around JSONObject, which takes care for the JSONException and also for serialization of JSON structures.

Author:
Richard "Shred" Körber
See Also:
Serialized Form

Constructor Summary
FlattrObject()
          Creates a new, empty FlattrObject.
FlattrObject(org.json.JSONObject data)
          Creates a FlattrObject from the given JSONObject.
FlattrObject(String json)
          Creates a FlattrObject from the given JSON string.
 
Method Summary
 String get(String key)
          Gets a String from the given key.
 boolean getBoolean(String key)
          Gets a boolean from the given key.
 Date getDate(String key)
          Gets a Date from the given key.
 FlattrObject getFlattrObject(String key)
          Gets a FlattrObject from the given key.
 int getInt(String key)
          Gets an integer from the given key.
 org.json.JSONObject getJSONObject()
          Returns the JSONObject that represents this FlattrObject.
 Object getObject(String key)
          Gets an Object from the given key.
 List<FlattrObject> getObjects(String key)
          Gets a collection of FlattrObject from the given key.
 List<String> getStrings(String key)
          Gets a collection of String from the given key.
 String getSubString(String key, String subKey)
          Gets a String from the given subKey which is a property of the given key.
 boolean has(String key)
          Checks if there is a key.
 void put(String key, Object value)
          Changes the key and sets it to the given value.
 void putStrings(String key, Collection<String> value)
          Puts a collection of strings as array object to the given key.
 void readExternal(ObjectInput in)
           
 String toString()
          Returns the current state of the FlattrObject as JSON string.
 void writeExternal(ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FlattrObject

public FlattrObject()
Creates a new, empty FlattrObject.


FlattrObject

public FlattrObject(org.json.JSONObject data)
Creates a FlattrObject from the given JSONObject.

Parameters:
data - JSONObject to use. It is not cloned. It's contents may be changed by this FlattrObject.

FlattrObject

public FlattrObject(String json)
Creates a FlattrObject from the given JSON string.

Parameters:
json - JSON string to initialize the FlattrObject with
Method Detail

has

public boolean has(String key)
Checks if there is a key.

Parameters:
key - Key to check for
Returns:
true if there is such a key (value may still be null).

getObject

public Object getObject(String key)
Gets an Object from the given key.

Parameters:
key - Key to read from
Returns:
Object that was read
Throws:
MarshalException - if there was no such key

get

public String get(String key)
Gets a String from the given key.

Parameters:
key - Key to read from
Returns:
String that was read
Throws:
MarshalException - if there was no such key

getSubString

public String getSubString(String key,
                           String subKey)
Gets a String from the given subKey which is a property of the given key.

Parameters:
key - Key of the parent object
subKey - Key to read from
Returns:
String that was read
Throws:
MarshalException - if there was no such key or subKey

getFlattrObject

public FlattrObject getFlattrObject(String key)
Gets a FlattrObject from the given key.

Parameters:
key - Key to read from
Returns:
FlattrObject that was read
Throws:
MarshalException - if there was no such key

getInt

public int getInt(String key)
Gets an integer from the given key.

Parameters:
key - Key to read from
Returns:
integer that was read
Throws:
MarshalException - if there was no such key, or if it did not contain the expected type

getBoolean

public boolean getBoolean(String key)
Gets a boolean from the given key.

Parameters:
key - Key to read from
Returns:
boolean that was read
Throws:
MarshalException - if there was no such key, or if it did not contain the expected type

getDate

public Date getDate(String key)
Gets a Date from the given key.

Parameters:
key - Key to read from
Returns:
Date that was read, or null if no date was set
Throws:
MarshalException - if there was no such key, or if it did not contain the expected type

getStrings

public List<String> getStrings(String key)
Gets a collection of String from the given key.

Parameters:
key - Key to read from
Returns:
Collection of Strings
Throws:
MarshalException - if there was no such key, or if it did not contain the expected type

getObjects

public List<FlattrObject> getObjects(String key)
Gets a collection of FlattrObject from the given key.

Parameters:
key - Key to read from
Returns:
Collection of FlattrObject
Throws:
MarshalException - if there was no such key, or if it did not contain the expected type

put

public void put(String key,
                Object value)
Changes the key and sets it to the given value.

Parameters:
key - Key to write to
value - Value to be written
Throws:
MarshalException - if the key could not be changed

putStrings

public void putStrings(String key,
                       Collection<String> value)
Puts a collection of strings as array object to the given key.

Parameters:
key - Key to write to
value - Collection of Strings to write
Throws:
MarshalException - if the key could not be changed

toString

public String toString()
Returns the current state of the FlattrObject as JSON string.

Overrides:
toString in class Object
Returns:
JSON representation of the current state

getJSONObject

public org.json.JSONObject getJSONObject()
Returns the JSONObject that represents this FlattrObject. Note that changes to this JSONObject will affect the FlattrObject as well.

Returns:
JSONObject

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable
Throws:
IOException

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Specified by:
readExternal in interface Externalizable
Throws:
IOException
ClassNotFoundException


Copyright © 2010-2012. All Rights Reserved.