Search in sources :

Example 51 with JsonValue

use of elemental.json.JsonValue in project flow by vaadin.

the class JsonUtilsTest method testStream.

@Test
public void testStream() {
    JsonArray array = createTestArray1();
    List<JsonValue> list = JsonUtils.stream(array).collect(Collectors.toList());
    Assert.assertEquals(2, list.size());
    Assert.assertEquals("foo", list.get(0).asString());
    Assert.assertTrue(JsonUtils.jsonEquals(list.get(1), Json.createObject()));
}
Also used : JsonArray(elemental.json.JsonArray) JsonValue(elemental.json.JsonValue) Test(org.junit.Test)

Example 52 with JsonValue

use of elemental.json.JsonValue in project che by eclipse.

the class JsonRpcParams method getAsListOf.

public <T> List<T> getAsListOf(Class<T> type) {
    checkNotNull(type, "Type must not be null");
    List<T> list = new ArrayList<>(paramsList.size());
    for (int i = 0; i < paramsList.size(); i++) {
        JsonValue jsonValue = paramsList.get(i);
        T item;
        if (type.equals(String.class)) {
            item = (T) jsonValue.asString();
        } else if (type.equals(Double.class)) {
            Double d = jsonValue.asNumber();
            item = (T) d;
        } else if (type.equals(Boolean.class)) {
            Boolean b = jsonValue.asBoolean();
            item = (T) b;
        } else {
            item = dtoFactory.createDtoFromJson(jsonValue.toJson(), type);
        }
        list.add(i, item);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) JsonValue(elemental.json.JsonValue)

Example 53 with JsonValue

use of elemental.json.JsonValue in project che by eclipse.

the class JsonRpcResult method getAsListOf.

public <T> List<T> getAsListOf(Class<T> type) {
    List<T> list = new ArrayList<>(resultList.size());
    for (int i = 0; i < resultList.size(); i++) {
        JsonValue jsonValue = resultList.get(i);
        T item;
        if (type.equals(String.class)) {
            item = (T) jsonValue.asString();
        } else if (type.equals(Double.class)) {
            item = (T) (Double) jsonValue.asNumber();
        } else if (type.equals(Boolean.class)) {
            item = (T) (Boolean) jsonValue.asBoolean();
        } else {
            item = dtoFactory.createDtoFromJson(jsonValue.toJson(), type);
        }
        list.add(i, item);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) JsonValue(elemental.json.JsonValue)

Example 54 with JsonValue

use of elemental.json.JsonValue in project che by eclipse.

the class JsonRpcErrorTest method shouldToJsonObjectProperly.

@Test
public void shouldToJsonObjectProperly() {
    JsonRpcError real = new JsonRpcError(ERROR_JSON, jsonFactory);
    JsonValue expected = jsonFactory.parse(ERROR_JSON);
    assertTrue(real.toJsonObject().jsEquals(expected));
}
Also used : JsonValue(elemental.json.JsonValue) Test(org.junit.Test)

Example 55 with JsonValue

use of elemental.json.JsonValue in project che by eclipse.

the class JsonRpcListTest method shouldToJsonArrayCreatedDtoArray.

@Test
public void shouldToJsonArrayCreatedDtoArray() throws Exception {
    Dto dto = mock(Dto.class);
    when(dto.toString()).thenReturn(DTO);
    when(dto.getParameter()).thenReturn("value");
    when(dtoFactory.createDtoFromJson(DTO, Dto.class)).thenReturn(dto);
    JsonArray expected = jsonFactory.createArray();
    JsonValue parse = jsonFactory.parse(dto.toString());
    expected.set(0, parse);
    JsonRpcList jsonRpcList = new JsonRpcList(singletonList(dto), jsonFactory, dtoFactory);
    JsonArray actual = jsonRpcList.toJsonArray();
    assertTrue(expected.jsEquals(actual));
}
Also used : JsonArray(elemental.json.JsonArray) JsonValue(elemental.json.JsonValue) Test(org.junit.Test)

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