Skip to content

Commit 0df4eae

Browse files
javier-godoypaodb
authored andcommitted
fix: return class that implement both JsonNode and JsonValue
Close #12
1 parent ae395fb commit 0df4eae

11 files changed

+328
-8
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.jsonmigration;
21+
22+
import elemental.json.JsonArray;
23+
import java.util.ArrayList;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import tools.jackson.databind.JsonNode;
27+
import tools.jackson.databind.node.ArrayNode;
28+
import tools.jackson.databind.node.JsonNodeFactory;
29+
30+
@SuppressWarnings("serial")
31+
class ElementalArrayNode extends ArrayNode implements UnsupportedJsonValueImpl {
32+
33+
public ElementalArrayNode(JsonArray a) {
34+
super(JsonNodeFactory.instance, children(a));
35+
}
36+
37+
private static List<JsonNode> children(JsonArray a) {
38+
switch (a.length()) {
39+
case 0:
40+
return Collections.emptyList();
41+
case 1:
42+
return Collections.singletonList(JsonMigrationHelper25.convertToJsonNode(a.get(0)));
43+
default:
44+
List<JsonNode> children = new ArrayList<>(a.length());
45+
for (int i = 0, n = a.length(); i < n; i++) {
46+
children.add(JsonMigrationHelper25.convertToJsonNode(a.get(i)));
47+
}
48+
return children;
49+
}
50+
}
51+
52+
}
53+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.jsonmigration;
21+
22+
import tools.jackson.databind.node.BooleanNode;
23+
24+
@SuppressWarnings("serial")
25+
class ElementalBooleanNode extends BooleanNode implements UnsupportedJsonValueImpl {
26+
27+
public ElementalBooleanNode(boolean value) {
28+
super(value);
29+
}
30+
31+
}
32+
33+
34+
35+
36+
37+
38+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.jsonmigration;
21+
22+
import tools.jackson.databind.node.NullNode;
23+
24+
@SuppressWarnings("serial")
25+
class ElementalNullNode extends NullNode implements UnsupportedJsonValueImpl {
26+
27+
public ElementalNullNode() {
28+
super();
29+
}
30+
31+
}
32+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.jsonmigration;
21+
22+
import tools.jackson.databind.node.DoubleNode;
23+
24+
@SuppressWarnings("serial")
25+
class ElementalNumberNode extends DoubleNode implements UnsupportedJsonValueImpl {
26+
27+
public ElementalNumberNode(double value) {
28+
super(value);
29+
}
30+
31+
}
32+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.jsonmigration;
21+
22+
import static com.flowingcode.vaadin.jsonmigration.JsonMigrationHelper25.convertToJsonNode;
23+
import elemental.json.JsonObject;
24+
import java.util.Collections;
25+
import java.util.LinkedHashMap;
26+
import java.util.Map;
27+
import tools.jackson.databind.JsonNode;
28+
import tools.jackson.databind.node.JsonNodeFactory;
29+
import tools.jackson.databind.node.ObjectNode;
30+
31+
@SuppressWarnings("serial")
32+
class ElementalObjectNode extends ObjectNode implements UnsupportedJsonValueImpl {
33+
34+
public ElementalObjectNode(JsonObject o) {
35+
super(JsonNodeFactory.instance, children(o));
36+
}
37+
38+
private static Map<String, JsonNode> children(JsonObject o) {
39+
String keys[] = o.keys();
40+
switch (keys.length) {
41+
case 0:
42+
return Collections.emptyMap();
43+
case 1:
44+
return Collections.singletonMap(keys[0], convertToJsonNode(o.get(keys[0])));
45+
default:
46+
Map<String, JsonNode> children = new LinkedHashMap<>(keys.length);
47+
for (String key : keys) {
48+
children.put(key, convertToJsonNode(o.get(key)));
49+
}
50+
return children;
51+
}
52+
}
53+
54+
}
55+
56+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*-
2+
* #%L
3+
* Json Migration Helper
4+
* %%
5+
* Copyright (C) 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.jsonmigration;
21+
22+
import tools.jackson.databind.node.StringNode;
23+
24+
@SuppressWarnings("serial")
25+
class ElementalStringNode extends StringNode implements UnsupportedJsonValueImpl {
26+
27+
public ElementalStringNode(String value) {
28+
super(value);
29+
}
30+
31+
}

src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private static Class<?> lookup_BaseJsonNode() {
7272
* @param object the object to convert
7373
* @return an {@code Object} suitable to use as the result of a {@code ClientCallable} method.
7474
*/
75-
public static Object convertToClientCallableResult(Object object) {
75+
public static <T extends JsonValue> T convertToClientCallableResult(T object) {
7676
return helper.convertToClientCallableResult(object);
7777
}
7878

src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface JsonMigrationHelper {
2929

3030
JsonValue convertToJsonValue(Object object);
3131

32-
Object convertToClientCallableResult(Object object);
32+
<T extends JsonValue> T convertToClientCallableResult(T object);
3333

3434
Object invoke(Method method, Object instance, Object... args);
3535

src/main/java/com/flowingcode/vaadin/jsonmigration/JsonMigrationHelper25.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,28 @@ public JsonValue convertToJsonValue(Object object) {
5555
}
5656
}
5757

58+
@SuppressWarnings("unchecked")
5859
@Override
59-
public Object convertToClientCallableResult(Object object) {
60-
if (object instanceof JsonValue) {
61-
return convertToJsonNode((JsonValue) object);
60+
public JsonValue convertToClientCallableResult(JsonValue object) {
61+
if (object == null) {
62+
return null;
6263
} else {
63-
return object;
64+
switch (object.getType()) {
65+
case OBJECT:
66+
return new ElementalObjectNode((JsonObject) object);
67+
case ARRAY:
68+
return new ElementalArrayNode((JsonArray) object);
69+
case BOOLEAN:
70+
return new ElementalBooleanNode(object.asBoolean());
71+
case NULL:
72+
return new ElementalNullNode();
73+
case NUMBER:
74+
return new ElementalNumberNode(object.asNumber());
75+
case STRING:
76+
return new ElementalStringNode(object.asString());
77+
default:
78+
throw new IllegalArgumentException();
79+
}
6480
}
6581
}
6682

@@ -155,7 +171,7 @@ private static JsonValue convertToJsonValue(JsonNode jsonNode) {
155171

156172
private static final JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
157173

158-
private static BaseJsonNode convertToJsonNode(JsonValue jsonValue) {
174+
static BaseJsonNode convertToJsonNode(JsonValue jsonValue) {
159175
switch (jsonValue.getType()) {
160176
case OBJECT:
161177
JsonObject jsonObject = (JsonObject) jsonValue;

src/main/java/com/flowingcode/vaadin/jsonmigration/LegacyJsonMigrationHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public JsonValue convertToJsonValue(Object object) {
4141
}
4242

4343
@Override
44-
public Object convertToClientCallableResult(Object object) {
44+
public <T extends JsonValue> T convertToClientCallableResult(T object) {
4545
return object;
4646
}
4747

0 commit comments

Comments
 (0)