Search in sources :

Example 96 with JsonValue

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

the class JsonSerializerTest method serializeNull_returnNull.

@Test
public void serializeNull_returnNull() {
    JsonValue json = JsonSerializer.toJson((Object) null);
    Assert.assertTrue("The JsonValue should be instanceof JsonNull", json instanceof JsonNull);
}
Also used : JsonValue(elemental.json.JsonValue) JsonNull(elemental.json.JsonNull) Test(org.junit.Test)

Example 97 with JsonValue

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

the class JsonSerializerTest method serializePopulatedObjectWithBasicTypes_returnJsonObjectWithDefinedProperties.

@Test
public void serializePopulatedObjectWithBasicTypes_returnJsonObjectWithDefinedProperties() {
    ObjectWithSimpleTypes bean = new ObjectWithSimpleTypes();
    bean.setStringProperty("someProperty");
    bean.setIntProperty(1);
    bean.setIntegerProperty(2);
    bean.setLongProperty(3);
    bean.setLongObjectProperty(4l);
    bean.setShortProperty((short) 5);
    bean.setShortObjectProperty((short) 6);
    bean.setDoubleProperty(7);
    bean.setDoubleObjectProperty(8.0);
    bean.setByteProperty((byte) 9);
    bean.setByteObjectProperty((byte) 10);
    bean.setBooleanProperty(true);
    bean.setBooleanObjectProperty(false);
    bean.setCharProperty('c');
    bean.setCharacterProperty('C');
    bean.setEnumProperty(SomeEnum.SOME_VALUE_2);
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject object = (JsonObject) json;
    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"));
    bean = JsonSerializer.toObject(ObjectWithSimpleTypes.class, json);
    Assert.assertNotNull(bean);
    Assert.assertEquals("someProperty", bean.getStringProperty());
    Assert.assertEquals(1, bean.getIntProperty());
    Assert.assertEquals(Integer.valueOf(2), bean.getIntegerProperty());
    Assert.assertEquals(3, bean.getLongProperty());
    Assert.assertEquals(Long.valueOf(4), bean.getLongObjectProperty());
    Assert.assertEquals(5, bean.getShortProperty());
    Assert.assertEquals(Short.valueOf((short) 6), bean.getShortObjectProperty());
    Assert.assertEquals(7, bean.getDoubleProperty(), 0.00001);
    Assert.assertEquals(Double.valueOf(8), bean.getDoubleObjectProperty());
    Assert.assertEquals(9, bean.getByteProperty());
    Assert.assertEquals(Byte.valueOf((byte) 10), bean.getByteObjectProperty());
    Assert.assertEquals(true, bean.isBooleanProperty());
    Assert.assertEquals(Boolean.FALSE, bean.getBooleanObjectProperty());
    Assert.assertEquals('c', bean.getCharProperty());
    Assert.assertEquals((Character) 'C', bean.getCharacterProperty());
    Assert.assertEquals(SomeEnum.SOME_VALUE_2, bean.getEnumProperty());
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Example 98 with JsonValue

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

the class JsonSerializerTest method serializeObjectWithCollections_returnJsonObjectWithPopulatedProperties.

@Test
public void serializeObjectWithCollections_returnJsonObjectWithPopulatedProperties() {
    ObjectWithBasicCollections bean = new ObjectWithBasicCollections();
    bean.setListOfStrings(Arrays.asList("string1", "string2"));
    bean.setSetOfIntegers(new LinkedHashSet<>(Arrays.asList(3, 4)));
    bean.setLinkedListOfBooleans(new LinkedList<>(Arrays.asList(true, false)));
    bean.setArrayListOfDoubles(new ArrayList<>(Arrays.asList(5.0, 6.0)));
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject jsonObject = (JsonObject) json;
    JsonArray array = (JsonArray) jsonObject.get("listOfStrings");
    Assert.assertEquals("string1", array.getString(0));
    Assert.assertEquals("string2", array.getString(1));
    array = (JsonArray) jsonObject.get("setOfIntegers");
    Assert.assertEquals(3, array.getNumber(0), PRECISION);
    Assert.assertEquals(4, array.getNumber(1), PRECISION);
    array = (JsonArray) jsonObject.get("linkedListOfBooleans");
    Assert.assertEquals(true, array.getBoolean(0));
    Assert.assertEquals(false, array.getBoolean(1));
    array = (JsonArray) jsonObject.get("arrayListOfDoubles");
    Assert.assertEquals(5, array.getNumber(0), PRECISION);
    Assert.assertEquals(6, array.getNumber(1), PRECISION);
    bean = JsonSerializer.toObject(ObjectWithBasicCollections.class, json);
    Assert.assertNotNull("The deserialized object should not be null", bean);
    assertCollectionItemsAreEqual(bean.getListOfStrings(), "string1", "string2");
    assertCollectionItemsAreEqual(bean.getSetOfIntegers(), 3, 4);
    assertCollectionItemsAreEqual(bean.getLinkedListOfBooleans(), true, false);
    assertCollectionItemsAreEqual(bean.getArrayListOfDoubles(), 5.0, 6.0);
}
Also used : JsonArray(elemental.json.JsonArray) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Example 99 with JsonValue

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

the class JsonUtilsTest method collectEmptyStream.

@Test
public void collectEmptyStream() {
    Stream<JsonValue> jsonValueStream = Stream.empty();
    JsonArray a = jsonValueStream.collect(JsonUtils.asArray());
    Assert.assertEquals(0, a.length());
}
Also used : JsonArray(elemental.json.JsonArray) JsonValue(elemental.json.JsonValue) Test(org.junit.Test)

Example 100 with JsonValue

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

the class MapPutChangeTest method testJsonValueTypes.

@Test
public void testJsonValueTypes() {
    JsonValue stringValue = getValue("string");
    Assert.assertSame(JsonType.STRING, stringValue.getType());
    Assert.assertEquals("string", stringValue.asString());
    JsonValue numberValue = getValue(Integer.valueOf(1));
    Assert.assertSame(JsonType.NUMBER, numberValue.getType());
    Assert.assertEquals(1, numberValue.asNumber(), 0);
    JsonValue booleanValue = getValue(Boolean.TRUE);
    Assert.assertSame(JsonType.BOOLEAN, booleanValue.getType());
    Assert.assertTrue(booleanValue.asBoolean());
    JsonObject jsonInput = Json.createObject();
    JsonValue jsonValue = getValue(jsonInput);
    Assert.assertSame(JsonType.OBJECT, jsonValue.getType());
    Assert.assertSame(jsonInput, jsonValue);
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest) AbstractNodeFeatureTest(com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)

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