org.qi4j.valueserialization.orgjson
Class OrgJsonValueDeserializer

java.lang.Object
  extended by org.qi4j.spi.value.ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
      extended by org.qi4j.valueserialization.orgjson.OrgJsonValueDeserializer
All Implemented Interfaces:
ValueDeserializer

public class OrgJsonValueDeserializer
extends ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>

ValueDeserializer reading Values from JSON documents using org.json.


Constructor Summary
OrgJsonValueDeserializer(Application application, Module module, ServiceReference<ValueDeserializer> serviceRef)
           
 
Method Summary
protected  org.json.JSONTokener adaptInput(java.io.InputStream input)
          This method is always called first, this is a chance to wrap the input type.
protected  java.lang.Object asSimpleValue(java.lang.Object inputNode)
           
protected
<T> T
getObjectFieldValue(java.lang.Object inputNode, java.lang.String key, Function<java.lang.Object,T> valueDeserializer)
          Return null if the field do not exists.
protected  boolean isObjectValue(java.lang.Object inputNode)
           
protected  boolean objectHasField(java.lang.Object inputNode, java.lang.String key)
           
protected
<T> void
putArrayNodeInCollection(java.lang.Object inputNode, Function<java.lang.Object,T> deserializer, java.util.Collection<T> collection)
           
protected
<K,V> void
putArrayNodeInMap(java.lang.Object inputNode, Function<java.lang.Object,K> keyDeserializer, Function<java.lang.Object,V> valueDeserializer, java.util.Map<K,V> map)
           
protected
<T> java.util.Collection<T>
readArrayInCollection(org.json.JSONTokener input, Function<org.json.JSONTokener,T> deserializer, java.util.Collection<T> collection)
           
protected
<K,V> java.util.Map<K,V>
readMapInMap(org.json.JSONTokener input, Function<org.json.JSONTokener,K> keyDeserializer, Function<org.json.JSONTokener,V> valueDeserializer, java.util.Map<K,V> map)
          A Map<K,V> is serialized in an array of entries objects.
protected  org.json.JSONObject readObjectTree(org.json.JSONTokener input)
           
protected  java.lang.Object readPlainValue(org.json.JSONTokener input)
           
 
Methods inherited from class org.qi4j.spi.value.ValueDeserializerAdapter
deserialize, deserialize, deserialize, deserialize, deserialize, deserialize, deserialize, onDeserializationEnd, onDeserializationStart, registerDeserializer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OrgJsonValueDeserializer

public OrgJsonValueDeserializer(@Structure
                                Application application,
                                @Structure
                                Module module,
                                @Service
                                ServiceReference<ValueDeserializer> serviceRef)
Method Detail

adaptInput

protected org.json.JSONTokener adaptInput(java.io.InputStream input)
                                   throws java.lang.Exception
Description copied from class: ValueDeserializerAdapter
This method is always called first, this is a chance to wrap the input type.

Specified by:
adaptInput in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Parameters:
input - InputStream to adapt
Returns:
Adapted input
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

readPlainValue

protected java.lang.Object readPlainValue(org.json.JSONTokener input)
                                   throws java.lang.Exception
Specified by:
readPlainValue in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Returns:
a Plain Value read from the input
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

readArrayInCollection

protected <T> java.util.Collection<T> readArrayInCollection(org.json.JSONTokener input,
                                                            Function<org.json.JSONTokener,T> deserializer,
                                                            java.util.Collection<T> collection)
                                                 throws java.lang.Exception
Specified by:
readArrayInCollection in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Returns:
The filled collection or null if no array
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

readMapInMap

protected <K,V> java.util.Map<K,V> readMapInMap(org.json.JSONTokener input,
                                                Function<org.json.JSONTokener,K> keyDeserializer,
                                                Function<org.json.JSONTokener,V> valueDeserializer,
                                                java.util.Map<K,V> map)
                                   throws java.lang.Exception
Description copied from class: ValueDeserializerAdapter
A Map<K,V> is serialized in an array of entries objects.

Here is an example in JSON:

 [
     { "key": "foo",       "value": "bar"   },
     { "key": "cathedral", "value": "bazar" }
 ]
 

And an empty Map:

[]

This allow to use any type as keys and values while keeping the Map order at the cost of having non-predictible order of key/value inside an entry object.

Specified by:
readMapInMap in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Returns:
The filled map or null if no array
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

readObjectTree

protected org.json.JSONObject readObjectTree(org.json.JSONTokener input)
                                      throws java.lang.Exception
Specified by:
readObjectTree in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Returns:
an InputNodeType or null if the value was null
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

asSimpleValue

protected java.lang.Object asSimpleValue(java.lang.Object inputNode)
                                  throws java.lang.Exception
Specified by:
asSimpleValue in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Throws:
java.lang.Exception

isObjectValue

protected boolean isObjectValue(java.lang.Object inputNode)
                         throws java.lang.Exception
Specified by:
isObjectValue in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Throws:
java.lang.Exception

objectHasField

protected boolean objectHasField(java.lang.Object inputNode,
                                 java.lang.String key)
                          throws java.lang.Exception
Specified by:
objectHasField in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Throws:
java.lang.Exception

getObjectFieldValue

protected <T> T getObjectFieldValue(java.lang.Object inputNode,
                                    java.lang.String key,
                                    Function<java.lang.Object,T> valueDeserializer)
                         throws java.lang.Exception
Description copied from class: ValueDeserializerAdapter
Return null if the field do not exists.

Specified by:
getObjectFieldValue in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

putArrayNodeInCollection

protected <T> void putArrayNodeInCollection(java.lang.Object inputNode,
                                            Function<java.lang.Object,T> deserializer,
                                            java.util.Collection<T> collection)
                                 throws java.lang.Exception
Specified by:
putArrayNodeInCollection in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Throws:
java.lang.Exception

putArrayNodeInMap

protected <K,V> void putArrayNodeInMap(java.lang.Object inputNode,
                                       Function<java.lang.Object,K> keyDeserializer,
                                       Function<java.lang.Object,V> valueDeserializer,
                                       java.util.Map<K,V> map)
                          throws java.lang.Exception
Specified by:
putArrayNodeInMap in class ValueDeserializerAdapter<org.json.JSONTokener,java.lang.Object>
Throws:
java.lang.Exception