JsonGenerator { public static JsonValue createJsonObject(Object object) throws JsonException { if (object == null) { return new NullValue(); } Class<?> aClass = object.getClass(); JsonValue jsonRootValue = new ObjectValue(); JsonMethod[] getters; try { getters = methodsCache.get(aClass); } catch (ExecutionException e) { propagateIfPossible(e.getCause()); throw new JsonException(e.getCause()); } for (JsonMethod getter : getters) { try { Object getterResult = getter.method.invoke(object); Types getterResultType = JsonUtils.getType(getterResult); if (getterResultType == null) { jsonRootValue.addElement(getter.field, createJsonObject(getterResult)); } else { jsonRootValue.addElement(getter.field, createJsonValue(getterResult, getterResultType)); } } catch (InvocationTargetException | IllegalAccessException e) { throw new JsonException(e.getMessage(), e); } } return jsonRootValue; }  static JsonValue createJsonArray(Collection<?> collection); static JsonValue createJsonArray(Object array); static JsonValue createJsonObjectFromMap(Map<String, ?> map); static JsonValue createJsonObject(Object object);  }
@Test public void convertsObjectToJson() throws Exception { JsonValue json = JsonGenerator.createJsonObject(junitBook); assertTrue(json.isObject()); assertionHelper.assertThatJsonHasAllFieldsFromObject(junitBook, json); }
@Test public void convertsNullObjectToJson() throws Exception { JsonValue json = JsonGenerator.createJsonObject(null); assertTrue(json.isNull()); }
@UseDataProvider("complexObjectsForConvertToJson") @Test public void convertsComplexObjectToJson(Object object) throws Exception { JsonValue jsonValue = JsonGenerator.createJsonObject(object); assertTrue(jsonValue.isObject()); assertionHelper.assertThatJsonHasAllFieldsFromObject(object, jsonValue); }
@Test public void convertsObjectThatContainsCollectionsOfComplexObjects() throws Exception { BookCollections bookCollections = createBookCollections(); JsonValue jsonValue = JsonGenerator.createJsonObject(bookCollections); assertTrue(jsonValue.isObject()); assertionHelper.assertThatJsonHasAllFieldsFromObject(bookCollections, jsonValue); }
@Test public void convertsObjectThatContainsArraysOfComplexObjects() throws Exception { BookArrays bookArrays = createBookArrays(); JsonValue jsonValue = JsonGenerator.createJsonObject(bookArrays); assertTrue(jsonValue.isObject()); assertionHelper.assertThatJsonHasAllFieldsFromObject(bookArrays, jsonValue); }
@Test public void ignoresTransientFields() throws Exception { BeanWithTransientField bean = new BeanWithTransientField(); JsonValue jsonValue = JsonGenerator.createJsonObject(bean); assertEquals("visible", jsonValue.getElement("field").getStringValue()); assertNull(jsonValue.getElement("transientField")); }
@Test public void ignoresFieldsAnnotatedWithJsonTransient() throws Exception { BeanWithTransientField bean = new BeanWithTransientField(); JsonValue jsonValue = JsonGenerator.createJsonObject(bean); assertEquals("visible", jsonValue.getElement("field").getStringValue()); assertNull(jsonValue.getElement("jsonTransientField")); }
@Test public void convertsObjectWithEnumsToJson() throws Exception { BeanWithEnums beanWithEnums = createBeanWithEnums(); JsonValue jsonValue = JsonGenerator.createJsonObject(beanWithEnums); assertTrue(jsonValue.isObject()); assertionHelper.assertThatJsonHasAllFieldsFromObject(beanWithEnums, jsonValue); }
@Test public void convertsObjectWithClassFieldToJson() throws Exception { BeanWithClassField beanWithClassField = createBeanWithClassField(); JsonValue jsonValue = JsonGenerator.createJsonObject(beanWithClassField); assertEquals(BeanWithClassField.class.getName(), jsonValue.getElement("klass").getStringValue()); }
@Test public void convertsObjectWithPrimitiveFieldsAndTheirArraysToJson() throws Exception { BeanWithPrimitiveFields beanWithPrimitiveFields = createBeanWithPrimitiveFields(); JsonValue jsonValue = JsonGenerator.createJsonObject(beanWithPrimitiveFields); assertionHelper.assertThatJsonHasAllFieldsFromObject(beanWithPrimitiveFields, jsonValue); }
@Test public void convertsSimpleGroovyBeanToJson() throws Exception { Class<?> aClass = parseGroovyClass("SimpleBean.groovy"); GroovyObject groovyObject = (GroovyObject)aClass.newInstance(); groovyObject.invokeMethod("setValue", new Object[]{"test serialize groovy bean"}); JsonValue jsonValue = JsonGenerator.createJsonObject(groovyObject); assertionHelper.assertThatJsonHasAllFieldsFromObject(groovyObject, jsonValue); }
@Test public void convertsComplexGroovyBeanToJson() throws Exception { Class<?> aClass = parseGroovyClass("BookStorage.groovy"); Object groovyObject = aClass.getDeclaredMethod("createBookStorage").invoke(null); JsonValue jsonValue = JsonGenerator.createJsonObject(groovyObject); assertionHelper.assertThatJsonHasAllFieldsFromObject(groovyObject, jsonValue); }