use of elemental.json.JsonValue in project flow by vaadin.
the class NodeList method getDebugJson.
@Override
public JsonValue getDebugJson() {
JsonArray json = Json.createArray();
for (int i = 0; i < values.length(); i++) {
Object value = values.get(i);
JsonValue jsonValue = getAsDebugJson(value);
json.set(json.length(), jsonValue);
}
return json;
}
use of elemental.json.JsonValue in project flow by vaadin.
the class NodeMap method convert.
@Override
public JsonValue convert(Function<Object, JsonValue> converter) {
JsonObject json = WidgetUtil.createJsonObject();
properties.forEach((property, name) -> {
if (property.hasValue()) {
// Crazy cast since otherwise SDM fails for string values since
// String is not a JSO
JsonValue jsonValue = WidgetUtil.crazyJsoCast(converter.apply(property.getValue()));
json.put(name, jsonValue);
}
});
if (json.keys().length == 0) {
return null;
}
return json;
}
use of elemental.json.JsonValue in project flow by vaadin.
the class ComponentEventBus method createEventDataObjects.
/**
* Creates a list of data objects which can be passed to the constructor
* returned by {@link #getEventConstructor(Class)} as parameters 3+.
*
* @param domEvent
* the DOM event containing the data
* @param eventType
* the component event type
* @return a list of event data objects in the same order as defined in the
* component event constructor
*/
private List<Object> createEventDataObjects(DomEvent domEvent, Class<? extends ComponentEvent<?>> eventType) {
List<Object> eventDataObjects = new ArrayList<>();
LinkedHashMap<String, Class<?>> expressions = ComponentEventBusUtil.getEventDataExpressions(eventType);
expressions.forEach((expression, type) -> {
JsonValue jsonValue = domEvent.getEventData().get(expression);
if (jsonValue == null) {
jsonValue = Json.createNull();
}
Object value = JsonCodec.decodeAs(jsonValue, type);
eventDataObjects.add(value);
});
return eventDataObjects;
}
use of elemental.json.JsonValue 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);
}
}
use of elemental.json.JsonValue in project flow by vaadin.
the class GwtPolymerModelTest method testPolymerUtilsStoreNodeIdNotAvailableAsListItem.
public void testPolymerUtilsStoreNodeIdNotAvailableAsListItem() {
nextId = 98;
List<String> serverList = Arrays.asList("one", "two");
StateNode andAttachNodeWithList = createAndAttachNodeWithList(modelNode, serverList);
JsonValue jsonValue = PolymerUtils.convertToJson(andAttachNodeWithList);
assertTrue("Expected instance of JsonObject from converter, but was not.", jsonValue instanceof JsonObject);
double nodeId = ((JsonObject) jsonValue).getNumber("nodeId");
assertFalse("JsonValue array contained nodeId even though it shouldn't be visible", jsonValue.toJson().contains(Double.toString(nodeId)));
assertEquals("Found nodeId didn't match the set nodeId", 98.0, nodeId);
}
Aggregations