From 77f97fe246449c7fe73aa9feb0a5b9eb3026df0e Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Tue, 25 Nov 2025 22:05:42 -0300 Subject: [PATCH] fix: return class that implement both JsonNode and JsonValue Close #12 --- .../jsonmigration/ElementalArrayNode.java | 53 ++++++++++++++++ .../jsonmigration/ElementalBooleanNode.java | 38 ++++++++++++ .../jsonmigration/ElementalNullNode.java | 32 ++++++++++ .../jsonmigration/ElementalNumberNode.java | 32 ++++++++++ .../jsonmigration/ElementalObjectNode.java | 56 +++++++++++++++++ .../jsonmigration/ElementalStringNode.java | 31 ++++++++++ .../vaadin/jsonmigration/JsonMigration.java | 2 +- .../jsonmigration/JsonMigrationHelper.java | 2 +- .../jsonmigration/JsonMigrationHelper25.java | 26 ++++++-- .../LegacyJsonMigrationHelper.java | 2 +- .../UnsupportedJsonValueImpl.java | 62 +++++++++++++++++++ 11 files changed, 328 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalArrayNode.java create mode 100644 src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalBooleanNode.java create mode 100644 src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalNullNode.java create mode 100644 src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalNumberNode.java create mode 100644 src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalObjectNode.java create mode 100644 src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalStringNode.java create mode 100644 src/main/java/com/flowingcode/vaadin/jsonmigration/UnsupportedJsonValueImpl.java diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalArrayNode.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalArrayNode.java new file mode 100644 index 0000000..75a00a5 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalArrayNode.java @@ -0,0 +1,53 @@ +/*- + * #%L + * Json Migration Helper + * %% + * Copyright (C) 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.jsonmigration; + +import elemental.json.JsonArray; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.node.ArrayNode; +import tools.jackson.databind.node.JsonNodeFactory; + +@SuppressWarnings("serial") +class ElementalArrayNode extends ArrayNode implements UnsupportedJsonValueImpl { + + public ElementalArrayNode(JsonArray a) { + super(JsonNodeFactory.instance, children(a)); + } + + private static List children(JsonArray a) { + switch (a.length()) { + case 0: + return Collections.emptyList(); + case 1: + return Collections.singletonList(JsonMigrationHelper25.convertToJsonNode(a.get(0))); + default: + List children = new ArrayList<>(a.length()); + for (int i = 0, n = a.length(); i < n; i++) { + children.add(JsonMigrationHelper25.convertToJsonNode(a.get(i))); + } + return children; + } + } + +} + diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalBooleanNode.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalBooleanNode.java new file mode 100644 index 0000000..b628fdc --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalBooleanNode.java @@ -0,0 +1,38 @@ +/*- + * #%L + * Json Migration Helper + * %% + * Copyright (C) 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.jsonmigration; + +import tools.jackson.databind.node.BooleanNode; + +@SuppressWarnings("serial") +class ElementalBooleanNode extends BooleanNode implements UnsupportedJsonValueImpl { + + public ElementalBooleanNode(boolean value) { + super(value); + } + +} + + + + + + + diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalNullNode.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalNullNode.java new file mode 100644 index 0000000..c14ac17 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalNullNode.java @@ -0,0 +1,32 @@ +/*- + * #%L + * Json Migration Helper + * %% + * Copyright (C) 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.jsonmigration; + +import tools.jackson.databind.node.NullNode; + +@SuppressWarnings("serial") +class ElementalNullNode extends NullNode implements UnsupportedJsonValueImpl { + + public ElementalNullNode() { + super(); + } + +} + diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalNumberNode.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalNumberNode.java new file mode 100644 index 0000000..2654b92 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalNumberNode.java @@ -0,0 +1,32 @@ +/*- + * #%L + * Json Migration Helper + * %% + * Copyright (C) 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.jsonmigration; + +import tools.jackson.databind.node.DoubleNode; + +@SuppressWarnings("serial") +class ElementalNumberNode extends DoubleNode implements UnsupportedJsonValueImpl { + + public ElementalNumberNode(double value) { + super(value); + } + +} + diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalObjectNode.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalObjectNode.java new file mode 100644 index 0000000..ba6e7e4 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalObjectNode.java @@ -0,0 +1,56 @@ +/*- + * #%L + * Json Migration Helper + * %% + * Copyright (C) 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.jsonmigration; + +import static com.flowingcode.vaadin.jsonmigration.JsonMigrationHelper25.convertToJsonNode; +import elemental.json.JsonObject; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.node.JsonNodeFactory; +import tools.jackson.databind.node.ObjectNode; + +@SuppressWarnings("serial") +class ElementalObjectNode extends ObjectNode implements UnsupportedJsonValueImpl { + + public ElementalObjectNode(JsonObject o) { + super(JsonNodeFactory.instance, children(o)); + } + + private static Map children(JsonObject o) { + String keys[] = o.keys(); + switch (keys.length) { + case 0: + return Collections.emptyMap(); + case 1: + return Collections.singletonMap(keys[0], convertToJsonNode(o.get(keys[0]))); + default: + Map children = new LinkedHashMap<>(keys.length); + for (String key : keys) { + children.put(key, convertToJsonNode(o.get(key))); + } + return children; + } + } + +} + + diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalStringNode.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalStringNode.java new file mode 100644 index 0000000..82be137 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/ElementalStringNode.java @@ -0,0 +1,31 @@ +/*- + * #%L + * Json Migration Helper + * %% + * Copyright (C) 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.jsonmigration; + +import tools.jackson.databind.node.StringNode; + +@SuppressWarnings("serial") +class ElementalStringNode extends StringNode implements UnsupportedJsonValueImpl { + + public ElementalStringNode(String value) { + super(value); + } + +} \ No newline at end of file diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigration.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigration.java index 10cc8e2..1c3b080 100644 --- a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigration.java +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigration.java @@ -72,7 +72,7 @@ private static Class lookup_BaseJsonNode() { * @param object the object to convert * @return an {@code Object} suitable to use as the result of a {@code ClientCallable} method. */ - public static Object convertToClientCallableResult(Object object) { + public static T convertToClientCallableResult(T object) { return helper.convertToClientCallableResult(object); } diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper.java index aca9084..5681823 100644 --- a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper.java +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper.java @@ -29,7 +29,7 @@ interface JsonMigrationHelper { JsonValue convertToJsonValue(Object object); - Object convertToClientCallableResult(Object object); + T convertToClientCallableResult(T object); Object invoke(Method method, Object instance, Object... args); diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java index 591b0e9..2aa6ca3 100644 --- a/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java @@ -55,12 +55,28 @@ public JsonValue convertToJsonValue(Object object) { } } + @SuppressWarnings("unchecked") @Override - public Object convertToClientCallableResult(Object object) { - if (object instanceof JsonValue) { - return convertToJsonNode((JsonValue) object); + public JsonValue convertToClientCallableResult(JsonValue object) { + if (object == null) { + return null; } else { - return object; + switch (object.getType()) { + case OBJECT: + return new ElementalObjectNode((JsonObject) object); + case ARRAY: + return new ElementalArrayNode((JsonArray) object); + case BOOLEAN: + return new ElementalBooleanNode(object.asBoolean()); + case NULL: + return new ElementalNullNode(); + case NUMBER: + return new ElementalNumberNode(object.asNumber()); + case STRING: + return new ElementalStringNode(object.asString()); + default: + throw new IllegalArgumentException(); + } } } @@ -155,7 +171,7 @@ private static JsonValue convertToJsonValue(JsonNode jsonNode) { private static final JsonNodeFactory nodeFactory = JsonNodeFactory.instance; - private static BaseJsonNode convertToJsonNode(JsonValue jsonValue) { + static BaseJsonNode convertToJsonNode(JsonValue jsonValue) { switch (jsonValue.getType()) { case OBJECT: JsonObject jsonObject = (JsonObject) jsonValue; diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/LegacyJsonMigrationHelper.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/LegacyJsonMigrationHelper.java index a00b9bc..449538c 100644 --- a/src/main/java/com/flowingcode/vaadin/jsonmigration/LegacyJsonMigrationHelper.java +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/LegacyJsonMigrationHelper.java @@ -41,7 +41,7 @@ public JsonValue convertToJsonValue(Object object) { } @Override - public Object convertToClientCallableResult(Object object) { + public T convertToClientCallableResult(T object) { return object; } diff --git a/src/main/java/com/flowingcode/vaadin/jsonmigration/UnsupportedJsonValueImpl.java b/src/main/java/com/flowingcode/vaadin/jsonmigration/UnsupportedJsonValueImpl.java new file mode 100644 index 0000000..25b55f6 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/jsonmigration/UnsupportedJsonValueImpl.java @@ -0,0 +1,62 @@ +/*- + * #%L + * Json Migration Helper + * %% + * Copyright (C) 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.jsonmigration; + +import elemental.json.JsonType; +import elemental.json.JsonValue; + +interface UnsupportedJsonValueImpl extends JsonValue { + + @Override + default boolean asBoolean() { + throw new UnsupportedOperationException(); + } + + @Override + default double asNumber() { + throw new UnsupportedOperationException(); + } + + @Override + default String asString() { + throw new UnsupportedOperationException(); + } + + @Override + default JsonType getType() { + throw new UnsupportedOperationException(); + } + + @Override + default String toJson() { + throw new UnsupportedOperationException(); + } + + @Override + default boolean jsEquals(JsonValue value) { + throw new UnsupportedOperationException(); + } + + @Override + default Object toNative() { + throw new UnsupportedOperationException(); + } + +} \ No newline at end of file