@Test public void convertsObjectWithClassFieldToJson() throws Exception { BeanWithClassField beanWithClassField = createBeanWithClassField(); JsonValue jsonValue = JsonGenerator.createJsonObject(beanWithClassField); assertEquals(BeanWithClassField.class.getName(), jsonValue.getElement("klass").getStringValue()); }
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; }
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; } }
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; }  }
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); }
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);  }