Search in sources :

Example 1 with JsonNull

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

the class AttachTemplateChildRpcHandler method handleNode.

@Override
protected Optional<Runnable> handleNode(StateNode node, JsonObject invocationJson) {
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_ID);
    int requestedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    int assignedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    StateTree tree = (StateTree) node.getOwner();
    StateNode requestedNode = tree.getNodeById(requestedId);
    StateNode parent = tree.getNodeById(requestedId).getParent();
    JsonValue id = invocationJson.get(JsonConstants.RPC_ATTACH_ID);
    String tag = requestedNode.getFeature(ElementData.class).getTag();
    Logger logger = LoggerFactory.getLogger(AttachTemplateChildRpcHandler.class.getName());
    if (assignedId == -1) {
        logger.error("Attach existing element has failed because " + "the client-side element is not found");
        if (id instanceof JsonNull) {
            throw new IllegalStateException(String.format("The element with the tag name '%s' was " + "not found in the parent with id='%d'", tag, parent.getId()));
        } else {
            throw new IllegalStateException(String.format("The element with the tag name '%s' and id '%s' was " + "not found in the parent with id='%d'", tag, id.asString(), parent.getId()));
        }
    } else if (requestedId != assignedId) {
        logger.error("Attach existing element has failed because " + "the element has been already attached from the server side");
        if (id instanceof JsonNull) {
            throw new IllegalStateException(String.format("The element with the tag name '%s' is already " + "attached to the parent with id='%d'", tag, parent.getId()));
        } else {
            throw new IllegalStateException(String.format("The element with the tag name '%s' and id '%s' is " + "already attached to the parent with id='%d'", tag, id.asString(), parent.getId()));
        }
    } else {
        logger.error("Attach existing element request succeeded. " + "But the response about this is unexpected");
        // side should not respond
        throw new IllegalArgumentException("Unexpected successful attachment " + "response is received from the client-side. " + "Client side should not respond if everything is fine");
    }
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) StateNode(com.vaadin.flow.internal.StateNode) JsonValue(elemental.json.JsonValue) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) Logger(org.slf4j.Logger) JsonNull(elemental.json.JsonNull)

Example 2 with JsonNull

use of elemental.json.JsonNull 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 3 with JsonNull

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

the class JsonSerializerTest method serializeEmptyRecursiveObject_returnJsonObjectWithNullProperties.

@Test
public void serializeEmptyRecursiveObject_returnJsonObjectWithNullProperties() {
    RecursiveObject bean = new RecursiveObject();
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject jsonObject = (JsonObject) json;
    Assert.assertTrue(jsonObject.hasKey("recursive"));
    Assert.assertTrue(jsonObject.get("recursive") instanceof JsonNull);
    Assert.assertEquals(0, jsonObject.getNumber("index"), PRECISION);
    bean = JsonSerializer.toObject(RecursiveObject.class, json);
    Assert.assertNotNull("The deserialized object should not be null", bean);
    Assert.assertNull(bean.getRecursive());
    Assert.assertEquals(0, bean.getIndex());
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) JsonNull(elemental.json.JsonNull) Test(org.junit.Test)

Example 4 with JsonNull

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

the class JsonSerializerTest method serializeEmptyObjectWithBasicCollections_returnJsonObjectWithNullProperties.

@Test
public void serializeEmptyObjectWithBasicCollections_returnJsonObjectWithNullProperties() {
    ObjectWithBasicCollections bean = new ObjectWithBasicCollections();
    /*
         * private List<String> listOfStrings; private Set<Integer>
         * setOfIntegers; private LinkedList<Boolean> linkedListOfBooleans;
         * private ArrayList<Double> arrayListOfDoubles;
         * 
         */
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject jsonObject = (JsonObject) json;
    Assert.assertTrue(jsonObject.hasKey("listOfStrings"));
    Assert.assertTrue(jsonObject.get("listOfStrings") instanceof JsonNull);
    Assert.assertTrue(jsonObject.hasKey("setOfIntegers"));
    Assert.assertTrue(jsonObject.get("setOfIntegers") instanceof JsonNull);
    Assert.assertTrue(jsonObject.hasKey("linkedListOfBooleans"));
    Assert.assertTrue(jsonObject.get("linkedListOfBooleans") instanceof JsonNull);
    Assert.assertTrue(jsonObject.hasKey("arrayListOfDoubles"));
    Assert.assertTrue(jsonObject.get("arrayListOfDoubles") instanceof JsonNull);
    bean = JsonSerializer.toObject(ObjectWithBasicCollections.class, json);
    Assert.assertNotNull("The deserialized object should not be null", bean);
    Assert.assertNull(bean.getListOfStrings());
    Assert.assertNull(bean.getSetOfIntegers());
    Assert.assertNull(bean.getLinkedListOfBooleans());
    Assert.assertNull(bean.getArrayListOfDoubles());
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) JsonNull(elemental.json.JsonNull) Test(org.junit.Test)

Example 5 with JsonNull

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

the class JsonSerializerTest method serializeEmptyObjectWithObjects_returnJsonObjectWithNullProperties.

@Test
public void serializeEmptyObjectWithObjects_returnJsonObjectWithNullProperties() {
    ObjectWithOtherObjects bean = new ObjectWithOtherObjects();
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject jsonObject = (JsonObject) json;
    Assert.assertTrue(jsonObject.hasKey("object1"));
    Assert.assertTrue(jsonObject.get("object1") instanceof JsonNull);
    Assert.assertTrue(jsonObject.hasKey("object2"));
    Assert.assertTrue(jsonObject.get("object2") instanceof JsonNull);
    bean = JsonSerializer.toObject(ObjectWithOtherObjects.class, json);
    Assert.assertNotNull("The deserialized object should not be null", bean);
    Assert.assertNull(bean.getObject1());
    Assert.assertNull(bean.getObject2());
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) JsonNull(elemental.json.JsonNull) Test(org.junit.Test)

Aggregations

JsonNull (elemental.json.JsonNull)8 JsonValue (elemental.json.JsonValue)8 Test (org.junit.Test)6 JsonObject (elemental.json.JsonObject)5 JsonSerializable (com.vaadin.flow.component.JsonSerializable)1 StateNode (com.vaadin.flow.internal.StateNode)1 StateTree (com.vaadin.flow.internal.StateTree)1 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)1 JsonBoolean (elemental.json.JsonBoolean)1 JsonString (elemental.json.JsonString)1 JsonType (elemental.json.JsonType)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Logger (org.slf4j.Logger)1