@Test public void testDeserializeJsonWithClassAlias() { Genson genson = new GensonBuilder().addAlias("rect", Rectangle.class).create(); Shape p = genson.deserialize("{\"@class\":\"rect\"}", Shape.class); assertTrue(p instanceof Rectangle); p = genson.deserialize("{\"@class\":\"java.awt.Rectangle\"}", Shape.class); assertTrue(p instanceof Rectangle); }
@Test public void testJsonNumbersLimit() { String src = "[" + String.valueOf(Long.MAX_VALUE) + "," + String.valueOf(Long.MIN_VALUE) + "]"; long[] arr = genson.deserialize(src, long[].class); assertTrue(Long.MAX_VALUE == arr[0]); assertTrue(Long.MIN_VALUE == arr[1]); src = "[" + String.valueOf(Double.MAX_VALUE) + "," + String.valueOf(Double.MIN_VALUE) + "]"; double[] arrD = genson.deserialize(src, double[].class); assertTrue(Double.MAX_VALUE == arrD[0]); assertTrue(Double.MIN_VALUE == arrD[1]); }
@Override public void serialize(Object value, boolean pretty, OutputStream out) throws IOException { try { if (pretty) { gensonPretty.serialize(value, out); } else { genson.serialize(value, out); } } catch (TransformationException e) { throw new IOException(e.toString(), e); } }
@Test public void testMultidimensionalArray() { String json = "[[\"abc\",[42,24]],[\"def\",[43,34]]]"; Object[][] array = genson.deserialize(json, Object[][].class); assertEquals("abc", array[0][0]); assertArrayEquals(new Object[] {42L, 24L}, (Object[]) array[0][1]); assertEquals("def", array[1][0]); assertArrayEquals(new Object[] {43L, 34L}, (Object[]) array[1][1]); String json3 = "[[[\"abc\"],[42,24],[\"def\"],[43,34]]]"; genson.deserialize(json3, Object[][][].class); }
public static CarryForwardContext getCarryForwardContext(Message message) throws JMSException { if (!message.propertyExists(MessagePropertyNames.CARRY_FORWARD_CONTEXT)) { return CarryForwardContext.getDefault(); } String carryForwardContextJson = message.getStringProperty(MessagePropertyNames.CARRY_FORWARD_CONTEXT); if (!StringUtil.isEmpty(carryForwardContextJson)) { Genson genson = new Genson(); CarryForwardContext carryForwardContext = genson.deserialize(carryForwardContextJson, CarryForwardContext.class); return carryForwardContext; } return CarryForwardContext.getDefault(); }
public static void setCarryForwardContext(Message message, Object carryForwardContext) throws JMSException { if (carryForwardContext == null) { return; } String carryForwardContextJson; // carryForwardContextJson = fioranoJsonUtil.serialize((CarryForwardContext) // carryForwardContext); Genson genson = new Genson(); carryForwardContextJson = genson.serialize(carryForwardContext); // message.setObjectProperty(MessagePropertyNames.CARRY_FORWARD_CONTEXT, carryForwardContext); message.setStringProperty(MessagePropertyNames.CARRY_FORWARD_CONTEXT, carryForwardContextJson); }
@Test public void testDeserealizeIntoExistingBean() { BeanDescriptor<ClassWithConstructorFieldsAndGetters> desc = (BeanDescriptor<ClassWithConstructorFieldsAndGetters>) genson .getBeanDescriptorFactory() .provide( ClassWithConstructorFieldsAndGetters.class, ClassWithConstructorFieldsAndGetters.class, genson); ClassWithConstructorFieldsAndGetters c = new ClassWithConstructorFieldsAndGetters(1, 2, "3") { @Override public void setP2(Integer p2) { this.p2 = p2; } }; c.constructorCalled = false; String json = "{\"p0\":0,\"p1\":1,\"p2\":2,\"shouldSkipIt\":55, \"nameInJson\":\"125\"}"; desc.deserialize(c, new JsonReader(json), new Context(genson)); assertFalse(c.constructorCalled); assertEquals(c.p0, new Integer(0)); assertEquals(c.p1, new Integer(1)); assertEquals(c.p2, new Integer(2)); }
@SuppressWarnings("unchecked") @Test public void testJsonToUntypedList() { String src = "[1, 1.1, \"aa\", true, false]"; List<Object> l = genson.deserialize(src, List.class); assertArrayEquals(new Object[] {1L, 1.1, "aa", true, false}, l.toArray(new Object[l.size()])); }
@Override public <T> T deserialize(InputStream in, Class<T> cls) throws IOException { try { return genson.deserialize(in, cls); } catch (TransformationException e) { throw new IOException(e.toString(), e); } }
@Test public void testJsonDoubleArray() { String src = "[5, 0.006, 9.0E-11 ]"; double[] array = genson.deserialize(src, double[].class); assertEquals(array[0], 5, 0); assertEquals(array[1], 0.006, 0); assertEquals(array[2], 0.00000000009, 0); }
@Test public void testJsonToBeanWithConstructor() { String json = "{\"other\":{\"name\":\"TITI\", \"age\": 13}, \"name\":\"TOTO\", \"age\":26}"; BeanWithConstructor bean = genson.deserialize(json, BeanWithConstructor.class); assertEquals(bean.age, 26); assertEquals(bean.name, "TOTO"); assertEquals(bean.other.age, 13); assertEquals(bean.other.name, "TITI"); }
@Test public void testJsonComplexObjectEmpty() { String src = "{\"primitives\":null,\"listOfPrimitives\":[], \"arrayOfPrimitives\": null}"; ComplexObject co = genson.deserialize(src, ComplexObject.class); assertNull(co.getPrimitives()); assertTrue(co.getListOfPrimitives().size() == 0); assertNull(co.getArrayOfPrimitives()); }
@Test public void testJsonComplexObject() { ComplexObject coo = new ComplexObject( createPrimitives(), Arrays.asList(createPrimitives(), createPrimitives()), new Primitives[] {createPrimitives(), createPrimitives()}); ComplexObject co = genson.deserialize(coo.jsonString(), ComplexObject.class); ComplexObject.assertCompareComplexObjects(co, coo); }
@Test public void testDeserializeWithConstructorAndFieldsAndSetters() { String json = "{\"p0\":0,\"p1\":1,\"p2\":2,\"shouldSkipIt\":55, \"nameInJson\": 3}"; ClassWithConstructorFieldsAndGetters c = genson.deserialize(json, ClassWithConstructorFieldsAndGetters.class); assertEquals(c.p0, new Integer(0)); assertEquals(c.p1, new Integer(1)); assertEquals(c.p2, new Integer(2)); assertTrue(c.constructorCalled); }
public boolean addTrigger(Trigger drug) { DBCollection collection = db.getCollection("trigger"); // convert JSON to DBObject directly BasicDBObject bdbo = new BasicDBObject(); bdbo.put("name", drug.getName()); DBCursor curs = collection.find(bdbo); if (curs.count() > 0) return false; Genson genson = new Genson(); DBObject dbObject = (DBObject) JSON.parse(genson.serialize(drug)); collection.insert(dbObject); DBCursor cursorDoc = collection.find(); while (cursorDoc.hasNext()) { System.out.println(cursorDoc.next()); } System.out.println("Done"); return true; }
@Test public void testDeserializeWithConstructorAndMissingFields() { String json = "{\"p0\":0,\"p1\":1, \"nameInJson\": 3}"; ClassWithConstructorFieldsAndGetters c = genson.deserialize(json, ClassWithConstructorFieldsAndGetters.class); assertTrue(c.constructorCalled); assertEquals(new Integer(0), c.p0); assertEquals(new Integer(1), c.p1); assertEquals(null, c.p2); assertEquals(new Integer(3), c.hidden); }
@SuppressWarnings("unchecked") @Test public void testContentDrivenDeserialization() { String src = "{\"list\":[1, 2.3, 5, null]}"; TypeVariableList<Number> tvl = genson.deserialize(src, TypeVariableList.class); assertArrayEquals( tvl.list.toArray(new Number[tvl.list.size()]), new Number[] {1, 2.3, 5, null}); String json = "[\"hello\",5,{\"name\":\"GREETINGS\",\"source\":\"guest\"}]"; Map<String, String> map = new HashMap<String, String>(); map.put("name", "GREETINGS"); map.put("source", "guest"); assertEquals(Arrays.asList("hello", 5L, map), genson.deserialize(json, Collection.class)); // doit echouer du a la chaine et que list est de type <E extends Number> src = "{\"list\":[1, 2.3, 5, \"a\"]}"; try { tvl = genson.deserialize(src, TypeVariableList.class); fail(); } catch (Exception e) { } }
@SuppressWarnings("unchecked") @Test public void testUntypedDeserializationToMap() { String src = "{\"key1\": 1, \"key2\": 1.2, \"key3\": true, \"key4\": \"string\", \"key5\": null, \"list\":[1,2.0005,3]}"; Map<String, Object> map = genson.deserialize(src, Map.class); assertEquals(map.get("key1"), 1L); assertEquals(map.get("key2"), 1.2); assertEquals(map.get("key3"), true); assertEquals(map.get("key4"), "string"); assertNull(map.get("key5")); Object[] list = (Object[]) map.get("list"); assertArrayEquals(new Number[] {1L, 2.0005, 3L}, list); }
/** * Creates an instance of BeanDescriptor based on the passed arguments. Subclasses can override * this method to create their own BeanDescriptors. * * @param forClass * @param ofType * @param creator * @param accessors * @param mutators * @return a instance */ protected <T> BeanDescriptor<T> create( Class<T> forClass, Type ofType, BeanCreator creator, List<PropertyAccessor> accessors, Map<String, PropertyMutator> mutators, Genson genson) { return new BeanDescriptor<T>( forClass, getRawClass(ofType), accessors, mutators, creator, genson.failOnMissingProperty()); }
@Test public void testJsonPrimitivesObject() { String src = "{\"intPrimitive\":1, \"integerObject\":7, \"doublePrimitive\":1.01," + "\"doubleObject\":2.003,\"text\": \"HEY...YA!\", " + "\"booleanPrimitive\":true,\"booleanObject\":false}"; Primitives p = genson.deserialize(src, Primitives.class); assertEquals(p.getIntPrimitive(), 1); assertEquals(p.getIntegerObject(), new Integer(7)); assertEquals(p.getDoublePrimitive(), 1.01, 0); assertEquals(p.getDoubleObject(), new Double(2.003)); assertEquals(p.getText(), "HEY...YA!"); assertEquals(p.isBooleanPrimitive(), true); assertEquals(p.isBooleanObject(), Boolean.FALSE); }
@Test public void testDeserializeEmptyJson() { Integer i = genson.deserialize("\"\"", Integer.class); assertNull(i); i = genson.deserialize("", Integer.class); assertNull(i); i = genson.deserialize("null", Integer.class); assertNull(i); int[] arr = genson.deserialize("", int[].class); assertNull(arr); arr = genson.deserialize("null", int[].class); assertNull(arr); arr = genson.deserialize("[]", int[].class); assertNotNull(arr); Primitives p = genson.deserialize("", Primitives.class); assertNull(p); p = genson.deserialize("null", Primitives.class); assertNull(p); p = genson.deserialize("{}", Primitives.class); assertNotNull(p); }
private Converter<Object> provide(BeanProperty property, Genson genson) { // contextual converters must not be retrieved from cache nor stored in cache, by first // trying to create it and reusing it during the // call to genson.provideConverter we avoid retrieving it from cache, and by setting // DO_NOT_CACHE_CONVERTER to true we tell genson not to store // this converter in cache @SuppressWarnings("unchecked") Converter<Object> converter = (Converter<Object>) contextualConverterFactory.provide(property, genson); if (converter != null) { ThreadLocalHolder.store(DO_NOT_CACHE_CONVERTER_KEY, true); ThreadLocalHolder.store(CONTEXT_KEY, converter); } try { return genson.provideConverter(property.type); } finally { if (converter != null) { ThreadLocalHolder.remove(DO_NOT_CACHE_CONVERTER_KEY, Boolean.class); ThreadLocalHolder.remove(CONTEXT_KEY, Converter.class); } } }
@Test public void testJsonComplexObjectSkipValue() { ComplexObject coo = new ComplexObject( createPrimitives(), Arrays.asList( createPrimitives(), createPrimitives(), createPrimitives(), createPrimitives(), createPrimitives(), createPrimitives()), new Primitives[] {createPrimitives(), createPrimitives()}); DummyWithFieldToSkip dummy = new DummyWithFieldToSkip(coo, coo, createPrimitives(), Arrays.asList(coo)); DummyWithFieldToSkip dummy2 = genson.deserialize(dummy.jsonString(), DummyWithFieldToSkip.class); ComplexObject.assertCompareComplexObjects(dummy.getO1(), dummy2.getO1()); ComplexObject.assertCompareComplexObjects(dummy.getO2(), dummy2.getO2()); Primitives.assertComparePrimitives(dummy.getP(), dummy2.getP()); }
@Test public void testASMResolverShouldNotFailWhenUsingBootstrapClassloader() { assertNotNull(genson.deserialize("{}", Exception.class)); }
@Test public void testDeserializeEnum() { assertEquals(Player.JAVA, genson.deserialize("\"JAVA\"", Player.class)); }
@Test public void testJsonEmptyObject() { String src = "{}"; Primitives p = genson.deserialize(src, Primitives.class); assertNull(p.getText()); }
@Test public void testJsonEmptyArray() { String src = "[]"; Integer[] p = genson.deserialize(src, Integer[].class); assertTrue(p.length == 0); }