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");
}
}
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();
}
}
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());
}
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());
}
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());
}
Aggregations