Search in sources :

Example 46 with JsonValue

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

the class JsonSerializerTest method serializeBasicTypes_returnJsonBasicTypes.

@Test
public void serializeBasicTypes_returnJsonBasicTypes() {
    JsonValue json = JsonSerializer.toJson("someString");
    Assert.assertTrue("The JsonValue should be instanceof JsonString", json instanceof JsonString);
    Assert.assertEquals("someString", json.asString());
    json = JsonSerializer.toJson(0);
    Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber);
    Assert.assertEquals(0.0, json.asNumber(), PRECISION);
    json = JsonSerializer.toJson(0.0);
    Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber);
    Assert.assertEquals(0.0, json.asNumber(), PRECISION);
    json = JsonSerializer.toJson(0l);
    Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber);
    Assert.assertEquals(0.0, json.asNumber(), PRECISION);
    json = JsonSerializer.toJson((byte) 0);
    Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber);
    Assert.assertEquals(0.0, json.asNumber(), PRECISION);
    json = JsonSerializer.toJson((short) 0);
    Assert.assertTrue("The JsonValue should be instanceof JsonNumber", json instanceof JsonNumber);
    Assert.assertEquals(0.0, json.asNumber(), PRECISION);
    json = JsonSerializer.toJson(true);
    Assert.assertTrue("The JsonValue should be instanceof JsonBoolean", json instanceof JsonBoolean);
    Assert.assertTrue(json.asBoolean());
    json = JsonSerializer.toJson(false);
    Assert.assertTrue("The JsonValue should be instanceof JsonBoolean", json instanceof JsonBoolean);
    Assert.assertFalse(json.asBoolean());
    json = JsonSerializer.toJson(SomeEnum.SOME_VALUE_1);
    Assert.assertTrue("The JsonValue should be instanceof JsonString", json instanceof JsonString);
    Assert.assertEquals(SomeEnum.SOME_VALUE_1.name(), json.asString());
}
Also used : JsonNumber(elemental.json.JsonNumber) JsonValue(elemental.json.JsonValue) JsonBoolean(elemental.json.JsonBoolean) JsonString(elemental.json.JsonString) Test(org.junit.Test)

Example 47 with JsonValue

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

the class JsonSerializerTest method serializeObjectWithObjects_returnJsonObjectWithPopulatedProperties.

@Test
public void serializeObjectWithObjects_returnJsonObjectWithPopulatedProperties() {
    ObjectWithOtherObjects bean = new ObjectWithOtherObjects();
    ObjectWithSimpleTypes innerBean = new ObjectWithSimpleTypes();
    innerBean.setStringProperty("someProperty");
    innerBean.setIntProperty(1);
    innerBean.setIntegerProperty(2);
    innerBean.setLongProperty(3);
    innerBean.setLongObjectProperty(4l);
    innerBean.setShortProperty((short) 5);
    innerBean.setShortObjectProperty((short) 6);
    innerBean.setDoubleProperty(7);
    innerBean.setDoubleObjectProperty(8.0);
    innerBean.setByteProperty((byte) 9);
    innerBean.setByteObjectProperty((byte) 10);
    innerBean.setBooleanProperty(true);
    innerBean.setBooleanObjectProperty(false);
    innerBean.setCharProperty('c');
    innerBean.setCharacterProperty('C');
    innerBean.setEnumProperty(SomeEnum.SOME_VALUE_2);
    bean.setObject1(innerBean);
    innerBean = new ObjectWithSimpleTypes();
    innerBean.setStringProperty("someOtherProperty");
    innerBean.setIntProperty(10);
    innerBean.setIntegerProperty(20);
    innerBean.setLongProperty(30);
    innerBean.setLongObjectProperty(40l);
    innerBean.setShortProperty((short) 50);
    innerBean.setShortObjectProperty((short) 60);
    innerBean.setDoubleProperty(70);
    innerBean.setDoubleObjectProperty(80.0);
    innerBean.setByteProperty((byte) 90);
    innerBean.setByteObjectProperty((byte) 100);
    innerBean.setBooleanProperty(true);
    innerBean.setBooleanObjectProperty(false);
    innerBean.setCharProperty('d');
    innerBean.setCharacterProperty('D');
    innerBean.setEnumProperty(SomeEnum.SOME_VALUE_1);
    bean.setObject2(innerBean);
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject object = ((JsonObject) json).getObject("object1");
    Assert.assertNotNull("The object1 should be not be null", object);
    Assert.assertEquals("someProperty", object.getString("stringProperty"));
    Assert.assertEquals(1, object.getNumber("intProperty"), PRECISION);
    Assert.assertEquals(2, object.getNumber("integerProperty"), PRECISION);
    Assert.assertEquals(3, object.getNumber("longProperty"), PRECISION);
    Assert.assertEquals(4, object.getNumber("longObjectProperty"), PRECISION);
    Assert.assertEquals(5, object.getNumber("shortProperty"), PRECISION);
    Assert.assertEquals(6, object.getNumber("shortObjectProperty"), PRECISION);
    Assert.assertEquals(7, object.getNumber("doubleProperty"), PRECISION);
    Assert.assertEquals(8, object.getNumber("doubleObjectProperty"), PRECISION);
    Assert.assertEquals(9, object.getNumber("byteProperty"), PRECISION);
    Assert.assertEquals(10, object.getNumber("byteObjectProperty"), PRECISION);
    Assert.assertEquals(true, object.getBoolean("booleanProperty"));
    Assert.assertEquals(false, object.getBoolean("booleanObjectProperty"));
    Assert.assertEquals('c', object.getString("charProperty").charAt(0));
    Assert.assertEquals('C', object.getString("characterProperty").charAt(0));
    Assert.assertEquals(SomeEnum.SOME_VALUE_2.name(), object.getString("enumProperty"));
    object = ((JsonObject) json).getObject("object2");
    Assert.assertNotNull("The object2 should be not be null", object);
    Assert.assertEquals("someOtherProperty", object.getString("stringProperty"));
    Assert.assertEquals(10, object.getNumber("intProperty"), PRECISION);
    Assert.assertEquals(20, object.getNumber("integerProperty"), PRECISION);
    Assert.assertEquals(30, object.getNumber("longProperty"), PRECISION);
    Assert.assertEquals(40, object.getNumber("longObjectProperty"), PRECISION);
    Assert.assertEquals(50, object.getNumber("shortProperty"), PRECISION);
    Assert.assertEquals(60, object.getNumber("shortObjectProperty"), PRECISION);
    Assert.assertEquals(70, object.getNumber("doubleProperty"), PRECISION);
    Assert.assertEquals(80, object.getNumber("doubleObjectProperty"), PRECISION);
    Assert.assertEquals(90, object.getNumber("byteProperty"), PRECISION);
    Assert.assertEquals(100, object.getNumber("byteObjectProperty"), PRECISION);
    Assert.assertEquals(true, object.getBoolean("booleanProperty"));
    Assert.assertEquals(false, object.getBoolean("booleanObjectProperty"));
    Assert.assertEquals('d', object.getString("charProperty").charAt(0));
    Assert.assertEquals('D', object.getString("characterProperty").charAt(0));
    Assert.assertEquals(SomeEnum.SOME_VALUE_1.name(), object.getString("enumProperty"));
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Example 48 with JsonValue

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

the class JsonSerializerTest method serializePopulatedRecursiveObject_returnJsonObjectWithPopulatedProperties.

@Test
public void serializePopulatedRecursiveObject_returnJsonObjectWithPopulatedProperties() {
    final int recursions = 10;
    RecursiveObject bean = createRecusiveObject(recursions, 0);
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject object = ((JsonObject) json);
    for (int i = 0; i < recursions; i++) {
        Assert.assertEquals(i, object.getNumber("index"), PRECISION);
        if (i < recursions - 1) {
            object = object.getObject("recursive");
        } else {
            Assert.assertTrue(object.get("recursive") instanceof JsonNull);
        }
    }
    bean = JsonSerializer.toObject(RecursiveObject.class, json);
    for (int i = 0; i < recursions; i++) {
        Assert.assertEquals(i, bean.getIndex());
        bean = bean.getRecursive();
    }
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) JsonNull(elemental.json.JsonNull) Test(org.junit.Test)

Example 49 with JsonValue

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

the class JsonSerializerTest method serializeEmptyObjectWithBasicTypes_returnJsonObjectWithEmptyProperties.

@Test
public void serializeEmptyObjectWithBasicTypes_returnJsonObjectWithEmptyProperties() {
    ObjectWithSimpleTypes bean = new ObjectWithSimpleTypes();
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject object = (JsonObject) json;
    assertObjectHasNullValueForKey(object, "stringProperty");
    assertObjectHas0ValueForKey(object, "intProperty");
    assertObjectHasNullValueForKey(object, "integerProperty");
    assertObjectHas0ValueForKey(object, "longProperty");
    assertObjectHasNullValueForKey(object, "longObjectProperty");
    assertObjectHas0ValueForKey(object, "shortProperty");
    assertObjectHasNullValueForKey(object, "shortObjectProperty");
    assertObjectHas0ValueForKey(object, "doubleProperty");
    assertObjectHasNullValueForKey(object, "doubleObjectProperty");
    assertObjectHas0ValueForKey(object, "byteProperty");
    assertObjectHasNullValueForKey(object, "byteObjectProperty");
    assertObjectHasFalseValueForKey(object, "booleanProperty");
    assertObjectHasNullValueForKey(object, "booleanObjectProperty");
    assertObjectHasNullValueForKey(object, "characterProperty");
    assertObjectHasNullValueForKey(object, "enumProperty");
    // char is a different case, it is a string at the json
    Assert.assertTrue(object.hasKey("charProperty"));
    Assert.assertEquals((char) 0, object.getString("charProperty").charAt(0));
    bean = JsonSerializer.toObject(ObjectWithSimpleTypes.class, json);
    Assert.assertNotNull(bean);
    Assert.assertNull(bean.getStringProperty());
    Assert.assertEquals(0, bean.getIntProperty());
    Assert.assertNull(bean.getIntegerProperty());
    Assert.assertEquals(0, bean.getLongProperty());
    Assert.assertNull(bean.getLongObjectProperty());
    Assert.assertEquals(0, bean.getShortProperty());
    Assert.assertNull(bean.getShortObjectProperty());
    Assert.assertEquals(0, bean.getDoubleProperty(), 0.00001);
    Assert.assertNull(bean.getDoubleObjectProperty());
    Assert.assertEquals(0, bean.getByteProperty());
    Assert.assertNull(bean.getByteObjectProperty());
    Assert.assertEquals(false, bean.isBooleanProperty());
    Assert.assertNull(bean.getBooleanObjectProperty());
    Assert.assertEquals(0, bean.getCharProperty());
    Assert.assertNull(bean.getCharacterProperty());
    Assert.assertNull(bean.getEnumProperty());
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Example 50 with JsonValue

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

the class JsonUtilsTest method testAsArray.

@Test
public void testAsArray() {
    Stream<JsonValue> stream = JsonUtils.stream(createTestArray1());
    JsonArray array = stream.collect(JsonUtils.asArray());
    Assert.assertTrue(JsonUtils.jsonEquals(createTestArray1(), array));
}
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