Search in sources :

Example 86 with JsonValue

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;
}
Also used : JsonArray(elemental.json.JsonArray) JsonValue(elemental.json.JsonValue)

Example 87 with JsonValue

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;
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Example 88 with JsonValue

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;
}
Also used : ArrayList(java.util.ArrayList) JsonValue(elemental.json.JsonValue)

Example 89 with JsonValue

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);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo) JsonSerializable(com.vaadin.flow.component.JsonSerializable) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Method(java.lang.reflect.Method) JsonNull(elemental.json.JsonNull) JsonType(elemental.json.JsonType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonObject(elemental.json.JsonObject)

Example 90 with JsonValue

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);
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Aggregations

JsonValue (elemental.json.JsonValue)102 Test (org.junit.Test)76 JsonObject (elemental.json.JsonObject)46 JsonArray (elemental.json.JsonArray)31 JsonString (elemental.json.JsonString)11 JsonNull (elemental.json.JsonNull)7 Element (com.vaadin.flow.dom.Element)5 Matchers.anyString (org.mockito.Matchers.anyString)5 UI (com.vaadin.flow.component.UI)4 StateNode (com.vaadin.flow.internal.StateNode)4 JsonNumber (elemental.json.JsonNumber)4 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ArrayList (java.util.ArrayList)3 StateNode (com.vaadin.client.flow.StateNode)2 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)2 JsonSerializable (com.vaadin.flow.component.JsonSerializable)2 StateTree (com.vaadin.flow.internal.StateTree)2 AbstractNodeFeatureTest (com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2 Element (elemental.dom.Element)2