use of com.vaadin.flow.component.JsonSerializable in project flow by vaadin.
the class JsonSerializer method toJson.
/**
* Converts a Java bean, {@link JsonSerializable} instance, String, wrapper
* of primitive type or enum to a {@link JsonValue}.
* <p>
* When a bean is used, a {@link JsonObject} is returned.
*
* @param bean
* Java object to be converted
* @return the json representation of the Java object
*/
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);
}
}
use of com.vaadin.flow.component.JsonSerializable in project flow by vaadin.
the class JsonSerializer method toObject.
@SuppressWarnings("unchecked")
private static <T> T toObject(Class<T> type, Type genericType, JsonValue json) {
if (json == null || json instanceof JsonNull) {
return null;
}
Optional<?> simpleType = tryToConvertFromSimpleType(type, json);
if (simpleType.isPresent()) {
return (T) simpleType.get();
}
if (Collection.class.isAssignableFrom(type)) {
return toCollection(type, genericType, json);
}
T instance;
try {
instance = type.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("Could not create an instance of type " + type + ". Make sure it contains a default public constructor and the class is accessible.", e);
}
try {
if (instance instanceof JsonSerializable && json instanceof JsonObject) {
return type.cast(JsonSerializable.class.cast(instance).readJson((JsonObject) json));
}
JsonObject jsonObject = (JsonObject) json;
String[] keys = jsonObject.keys();
if (keys == null) {
return instance;
}
BeanInfo info = Introspector.getBeanInfo(type);
Map<String, Method> writers = new HashMap<>();
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method writer = pd.getWriteMethod();
if (writer != null) {
writers.put(pd.getName(), writer);
}
}
for (String key : keys) {
JsonValue jsonValue = jsonObject.get(key);
Method method = writers.get(key);
if (method != null) {
Class<?> parameterType = method.getParameterTypes()[0];
Type genericParameterType = method.getGenericParameterTypes()[0];
Object value = toObject(parameterType, genericParameterType, jsonValue);
method.invoke(instance, value);
}
}
return instance;
} catch (Exception e) {
throw new IllegalArgumentException("Could not deserialize object of type " + type + " from JsonValue", e);
}
}
Aggregations