use of org.apache.pivot.json.JSONSerializer in project pivot by apache.
the class BindTest method testTypedList.
/**
* Tests returning a typed list using
* {@code org.apache.pivot.util.TypeLiteral}.
*
* @throws IOException
* @throws SerializationException
*/
@Test
public void testTypedList() throws IOException, SerializationException {
JSONSerializer listSerializer = new JSONSerializer();
@SuppressWarnings("unchecked") List<Object> list = (List<Object>) listSerializer.readObject(getClass().getResourceAsStream("list.json"));
JSONSerializer typedListSerializer = new JSONSerializer((new TypeLiteral<ArrayList<SampleBean2>>() {
}).getType());
@SuppressWarnings("unchecked") ArrayList<SampleBean2> typedList = (ArrayList<SampleBean2>) typedListSerializer.readObject(getClass().getResourceAsStream("list.json"));
Object item0 = typedList.get(0);
assertTrue(item0 instanceof SampleBean2);
assertEquals((Integer) typedList.get(0).getA(), (Integer) JSON.get(list, "[0].a"));
}
use of org.apache.pivot.json.JSONSerializer in project pivot by apache.
the class BindTest method testTypedMap.
/**
* Tests returning a typed map using
* {@code org.apache.pivot.util.TypeLiteral}.
*
* @throws IOException
* @throws SerializationException
*/
@Test
public void testTypedMap() throws IOException, SerializationException {
JSONSerializer typedMapSerializer = new JSONSerializer((new TypeLiteral<HashMap<String, SampleBean2>>() {
}).getType());
@SuppressWarnings("unchecked") HashMap<String, SampleBean2> map = (HashMap<String, SampleBean2>) typedMapSerializer.readObject(new StringReader("{foo: {a:1, b:2, c:'3'}}"));
assertTrue(JSON.get(map, "foo") instanceof SampleBean2);
assertEquals(JSON.get(map, "foo.c"), "3");
}
use of org.apache.pivot.json.JSONSerializer in project pivot by apache.
the class BindTest method testDictionary.
/**
* Tests returning a class that implements
* {@code org.apache.pivot.collections.Dictionary}.
*
* @throws IOException
* @throws SerializationException
*/
@Test
public void testDictionary() throws IOException, SerializationException {
JSONSerializer dictionarySerializer = new JSONSerializer(SampleBean2DictionarySubclass.class);
SampleBean2Dictionary dictionary = (SampleBean2Dictionary) dictionarySerializer.readObject(new StringReader("{foo: {a:1, b:2, c:'3'}}"));
assertTrue(JSON.get(dictionary, "foo") instanceof SampleBean2);
assertEquals(JSON.get(dictionary, "foo.c"), "3");
}
use of org.apache.pivot.json.JSONSerializer in project pivot by apache.
the class JSONSerializerTest method testEquals.
@Test
public void testEquals() throws IOException, SerializationException {
JSONSerializer jsonSerializer = new JSONSerializer();
JSONSerializerListener jsonSerializerListener = new JSONSerializerListener() {
@Override
public void beginDictionary(JSONSerializer jsonSerializerArgument, Dictionary<String, ?> value) {
System.out.println("Begin dictionary: " + value);
}
@Override
public void endDictionary(JSONSerializer jsonSerializerArgument) {
System.out.println("End dictionary");
}
@Override
public void readKey(JSONSerializer jsonSerializerArgument, String key) {
System.out.println("Read key: " + key);
}
@Override
public void beginSequence(JSONSerializer jsonSerializerArgument, Sequence<?> value) {
System.out.println("Begin sequence: " + value);
}
@Override
public void endSequence(JSONSerializer jsonSerializerArgument) {
System.out.println("End sequence");
}
@Override
public void readString(JSONSerializer jsonSerializerArgument, String value) {
System.out.println("Read string: " + value);
}
@Override
public void readNumber(JSONSerializer jsonSerializerArgument, Number value) {
System.out.println("Read number: " + value);
}
@Override
public void readBoolean(JSONSerializer jsonSerializerArgument, Boolean value) {
System.out.println("Read boolean: " + value);
}
@Override
public void readNull(JSONSerializer jsonSerializerArgument) {
System.out.println("Read null");
}
};
jsonSerializer.getJSONSerializerListeners().add(jsonSerializerListener);
Object o1 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
assertEquals((Integer) JSON.get(o1, "a"), (Integer) 100);
assertEquals(JSON.get(o1, "b"), "Hello");
assertEquals(JSON.get(o1, "c"), false);
assertEquals((Integer) JSON.get(o1, "e.g"), (Integer) 5);
assertEquals((Integer) JSON.get(o1, "i.a"), (Integer) 200);
assertEquals(JSON.get(o1, "i.c"), true);
assertEquals(JSON.get(o1, "m"), "Hello\r\n\tWorld!");
jsonSerializer.getJSONSerializerListeners().remove(jsonSerializerListener);
Object o2 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
assertEquals((Integer) JSON.get(o2, "k[1].a"), (Integer) 10);
assertEquals((Integer) JSON.get(o2, "k[2].a"), (Integer) 100);
assertEquals((Integer) JSON.get(o2, "k[2].b"), (Integer) 200);
assertEquals(JSON.get(o2, "k[2].c"), "300");
assertEquals((Integer) JSON.get(o2, "j"), (Integer) 200);
assertEquals(JSON.get(o2, "n"), "This is a \"test\" of the 'quoting' in \\JSON\\");
assertTrue(o1.equals(o2));
List<?> d = JSON.get(o1, "d");
d.remove(0, 1);
assertFalse(o1.equals(o2));
}
use of org.apache.pivot.json.JSONSerializer in project pivot by apache.
the class BeanAdapterTest method setUp.
@Before
public void setUp() {
srcTest = new BeanAdapterSampleObject();
jsonSerializer = new JSONSerializer(BeanAdapterSampleObject.class);
writer = new StringWriter();
}
Aggregations