JsonSerializer { public static JsonValue toJson(Object bean) { if (bean == null) { return Json.createNull(); } if (bean instanceof Collection) { return toJson((Collection<?>) bean); } if (bean.getClass().isArray()) { return toJsonArray(bean); } if (bean instanceof JsonSerializable) { return ((JsonSerializable) bean).toJson(); } Optional<JsonValue> simpleType = tryToConvertToSimpleType(bean); if (simpleType.isPresent()) { return simpleType.get(); } try { JsonObject json = Json.createObject(); BeanInfo info = Introspector.getBeanInfo(bean.getClass()); for (PropertyDescriptor pd : info.getPropertyDescriptors()) { if ("class".equals(pd.getName())) { continue; } Method reader = pd.getReadMethod(); if (reader != null) { json.put(pd.getName(), toJson(reader.invoke(bean))); } } return json; } catch (Exception e) { throw new IllegalArgumentException( "Could not serialize object of type " + bean.getClass() + " to JsonValue", e); } } private  JsonSerializer(); static JsonValue toJson(Object bean); static JsonArray toJson(Collection<?> beans); static T toObject(Class<T> type, JsonValue json); static List<T> toObjects(Class<T> type, JsonArray json);  }
@Test public void serializeBasicTypes_returnJsonBasicTypes() { JsonValue json = JsonSerializer.toJson("someString"); Assert.assertTrue("The JsonValue should be instanceof JsonString", json instanceof JsonString); Assert.assertEquals("someString", json.asString()); json = JsonSerializer.toJson(0); Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber); Assert.assertEquals(0.0, json.asNumber(), PRECISION); json = JsonSerializer.toJson(0.0); Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber); Assert.assertEquals(0.0, json.asNumber(), PRECISION); json = JsonSerializer.toJson(0l); Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber); Assert.assertEquals(0.0, json.asNumber(), PRECISION); json = JsonSerializer.toJson((byte) 0); Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber); Assert.assertEquals(0.0, json.asNumber(), PRECISION); json = JsonSerializer.toJson((short) 0); Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber); Assert.assertEquals(0.0, json.asNumber(), PRECISION); json = JsonSerializer.toJson(true); Assert.assertTrue("The JsonValue should be instanceof JsonBoolean", json instanceof JsonBoolean); Assert.assertTrue(json.asBoolean()); json = JsonSerializer.toJson(false); Assert.assertTrue("The JsonValue should be instanceof JsonBoolean", json instanceof JsonBoolean); Assert.assertFalse(json.asBoolean()); json = JsonSerializer.toJson(SomeEnum.SOME_VALUE_1); Assert.assertTrue("The JsonValue should be instanceof JsonString", json instanceof JsonString); Assert.assertEquals(SomeEnum.SOME_VALUE_1.name(), json.asString()); }
@Test public void serializeNull_returnNull() { JsonValue json = JsonSerializer.toJson((Object) null); Assert.assertTrue("The JsonValue should be instanceof JsonNull", json instanceof JsonNull); }
@Test public void serializeObjectWithObjects_returnJsonObjectWithPopulatedProperties() { ObjectWithOtherObjects bean = new ObjectWithOtherObjects(); ObjectWithSimpleTypes innerBean = new ObjectWithSimpleTypes(); innerBean.setStringProperty("someProperty"); innerBean.setIntProperty(1); innerBean.setIntegerProperty(2); innerBean.setLongProperty(3); innerBean.setLongObjectProperty(4l); innerBean.setShortProperty((short) 5); innerBean.setShortObjectProperty((short) 6); innerBean.setDoubleProperty(7); innerBean.setDoubleObjectProperty(8.0); innerBean.setByteProperty((byte) 9); innerBean.setByteObjectProperty((byte) 10); innerBean.setBooleanProperty(true); innerBean.setBooleanObjectProperty(false); innerBean.setCharProperty('c'); innerBean.setCharacterProperty('C'); innerBean.setEnumProperty(SomeEnum.SOME_VALUE_2); bean.setObject1(innerBean); innerBean = new ObjectWithSimpleTypes(); innerBean.setStringProperty("someOtherProperty"); innerBean.setIntProperty(10); innerBean.setIntegerProperty(20); innerBean.setLongProperty(30); innerBean.setLongObjectProperty(40l); innerBean.setShortProperty((short) 50); innerBean.setShortObjectProperty((short) 60); innerBean.setDoubleProperty(70); innerBean.setDoubleObjectProperty(80.0); innerBean.setByteProperty((byte) 90); innerBean.setByteObjectProperty((byte) 100); innerBean.setBooleanProperty(true); innerBean.setBooleanObjectProperty(false); innerBean.setCharProperty('d'); innerBean.setCharacterProperty('D'); innerBean.setEnumProperty(SomeEnum.SOME_VALUE_1); bean.setObject2(innerBean); JsonValue json = JsonSerializer.toJson(bean); Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject); JsonObject object = ((JsonObject) json).getObject("object1"); Assert.assertNotNull("The object1 should be not be null", object); Assert.assertEquals("someProperty", object.getString("stringProperty")); Assert.assertEquals(1, object.getNumber("intProperty"), PRECISION); Assert.assertEquals(2, object.getNumber("integerProperty"), PRECISION); Assert.assertEquals(3, object.getNumber("longProperty"), PRECISION); Assert.assertEquals(4, object.getNumber("longObjectProperty"), PRECISION); Assert.assertEquals(5, object.getNumber("shortProperty"), PRECISION); Assert.assertEquals(6, object.getNumber("shortObjectProperty"), PRECISION); Assert.assertEquals(7, object.getNumber("doubleProperty"), PRECISION); Assert.assertEquals(8, object.getNumber("doubleObjectProperty"), PRECISION); Assert.assertEquals(9, object.getNumber("byteProperty"), PRECISION); Assert.assertEquals(10, object.getNumber("byteObjectProperty"), PRECISION); Assert.assertEquals(true, object.getBoolean("booleanProperty")); Assert.assertEquals(false, object.getBoolean("booleanObjectProperty")); Assert.assertEquals('c', object.getString("charProperty").charAt(0)); Assert.assertEquals('C', object.getString("characterProperty").charAt(0)); Assert.assertEquals(SomeEnum.SOME_VALUE_2.name(), object.getString("enumProperty")); object = ((JsonObject) json).getObject("object2"); Assert.assertNotNull("The object2 should be not be null", object); Assert.assertEquals("someOtherProperty", object.getString("stringProperty")); Assert.assertEquals(10, object.getNumber("intProperty"), PRECISION); Assert.assertEquals(20, object.getNumber("integerProperty"), PRECISION); Assert.assertEquals(30, object.getNumber("longProperty"), PRECISION); Assert.assertEquals(40, object.getNumber("longObjectProperty"), PRECISION); Assert.assertEquals(50, object.getNumber("shortProperty"), PRECISION); Assert.assertEquals(60, object.getNumber("shortObjectProperty"), PRECISION); Assert.assertEquals(70, object.getNumber("doubleProperty"), PRECISION); Assert.assertEquals(80, object.getNumber("doubleObjectProperty"), PRECISION); Assert.assertEquals(90, object.getNumber("byteProperty"), PRECISION); Assert.assertEquals(100, object.getNumber("byteObjectProperty"), PRECISION); Assert.assertEquals(true, object.getBoolean("booleanProperty")); Assert.assertEquals(false, object.getBoolean("booleanObjectProperty")); Assert.assertEquals('d', object.getString("charProperty").charAt(0)); Assert.assertEquals('D', object.getString("characterProperty").charAt(0)); Assert.assertEquals(SomeEnum.SOME_VALUE_1.name(), object.getString("enumProperty")); }