Skip to content

Commit 8bb9a26

Browse files
authored
feat!: NodeChange to use Jackson (#22155)
Closes #21154
1 parent 58f60e0 commit 8bb9a26

File tree

23 files changed

+154
-181
lines changed

23 files changed

+154
-181
lines changed

flow-server/src/main/java/com/vaadin/flow/component/AbstractSinglePropertyField.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Objects;
2222
import java.util.stream.Collectors;
2323

24+
import com.fasterxml.jackson.databind.JsonNode;
2425
import com.fasterxml.jackson.databind.node.BaseJsonNode;
2526
import com.googlecode.gentyref.GenericTypeReflector;
2627
import com.vaadin.flow.dom.DomListenerRegistration;
@@ -133,7 +134,7 @@ private <C extends AbstractField<C, V>, V> SerializableBiConsumer<C, V> createWr
133134
Integer.valueOf(0));
134135
typeHandlers.put(BaseJsonNode.class,
135136
getJsonHandler(BaseJsonNode.class));
136-
typeHandlers.put(JsonValue.class, getHandler(JsonValue.class));
137+
typeHandlers.put(JsonNode.class, getJsonHandler(BaseJsonNode.class));
137138
}
138139

139140
private final SerializableBiConsumer<C, T> propertyWriter;
@@ -375,7 +376,7 @@ protected void setPresentationValue(T newPresentationValue) {
375376
}
376377

377378
@Deprecated
378-
private static <P extends JsonValue> TypeHandler<P> getHandler(
379+
private static <P extends BaseJsonNode> TypeHandler<P> getHandler(
379380
Class<P> type) {
380381
ElementGetter<P> getter = (element, property, defaultValue) -> {
381382
Serializable value = element.getPropertyRaw(property);

flow-server/src/main/java/com/vaadin/flow/dom/Element.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
import com.fasterxml.jackson.databind.node.BaseJsonNode;
3232
import com.fasterxml.jackson.databind.node.BooleanNode;
3333
import com.fasterxml.jackson.databind.node.NullNode;
34-
import com.fasterxml.jackson.databind.node.NumericNode;
35-
import com.fasterxml.jackson.databind.node.TextNode;
36-
import com.fasterxml.jackson.databind.node.ValueNode;
3734
import org.jsoup.nodes.Document;
3835

3936
import com.vaadin.flow.component.Component;
@@ -47,20 +44,18 @@
4744
import com.vaadin.flow.dom.impl.BasicTextElementStateProvider;
4845
import com.vaadin.flow.dom.impl.CustomAttribute;
4946
import com.vaadin.flow.dom.impl.ThemeListImpl;
47+
import com.vaadin.flow.internal.JacksonUtils;
5048
import com.vaadin.flow.internal.JavaScriptSemantics;
5149
import com.vaadin.flow.internal.JsonCodec;
52-
import com.vaadin.flow.internal.JsonUtils;
5350
import com.vaadin.flow.internal.StateNode;
5451
import com.vaadin.flow.internal.nodefeature.VirtualChildrenList;
5552
import com.vaadin.flow.server.AbstractStreamResource;
5653
import com.vaadin.flow.server.Command;
5754
import com.vaadin.flow.server.StreamResource;
5855
import com.vaadin.flow.server.StreamResourceRegistry;
59-
import com.vaadin.flow.server.VaadinSession;
6056
import com.vaadin.flow.server.streams.ElementRequestHandler;
6157
import com.vaadin.flow.shared.Registration;
6258

63-
import elemental.json.Json;
6459
import elemental.json.JsonValue;
6560

6661
/**
@@ -696,8 +691,8 @@ public Element setProperty(String name, double value) {
696691
* Sets the given property to the given JSON value.
697692
* <p>
698693
* Please note that this method does not accept <code>null</code> as a
699-
* value, since {@link Json#createNull()} should be used instead for JSON
700-
* values.
694+
* value, since {@link com.vaadin.flow.internal.JacksonUtils#nullNode()}
695+
* should be used instead for JSON values.
701696
* <p>
702697
* Note that properties changed on the server are updated on the client but
703698
* changes made on the client side are not reflected back to the server
@@ -710,14 +705,16 @@ public Element setProperty(String name, double value) {
710705
* @param value
711706
* the property value, not <code>null</code>
712707
* @return this element
708+
* @deprecated Will be removed when all Flow-Components use Jackson variant
713709
*/
714710
// Distinct name so setProperty("foo", null) is not ambiguous
711+
@Deprecated
715712
public Element setPropertyJson(String name, JsonValue value) {
716713
if (value == null) {
717714
throw new IllegalArgumentException(USE_SET_PROPERTY_WITH_JSON_NULL);
718715
}
719716

720-
setRawProperty(name, value);
717+
setRawProperty(name, JacksonUtils.mapElemental(value));
721718
return this;
722719
}
723720

@@ -771,7 +768,7 @@ public Element setPropertyBean(String name, Object value) {
771768
if (value == null) {
772769
throw new IllegalArgumentException(USE_SET_PROPERTY_WITH_JSON_NULL);
773770
}
774-
return setPropertyJson(name, JsonUtils.beanToJson(value));
771+
return setPropertyJson(name, JacksonUtils.beanToJson(value));
775772
}
776773

777774
/**
@@ -797,7 +794,7 @@ public <T> Element setPropertyList(String name, List<T> value) {
797794
throw new IllegalArgumentException(USE_SET_PROPERTY_WITH_JSON_NULL);
798795
}
799796

800-
return setPropertyJson(name, JsonUtils.listToJson(value));
797+
return setPropertyJson(name, JacksonUtils.listToJson(value));
801798
}
802799

803800
/**
@@ -821,7 +818,7 @@ public Element setPropertyMap(String name, Map<String, ?> value) {
821818
throw new IllegalArgumentException(USE_SET_PROPERTY_WITH_JSON_NULL);
822819
}
823820

824-
return setPropertyJson(name, JsonUtils.mapToJson(value));
821+
return setPropertyJson(name, JacksonUtils.mapToJson(value));
825822
}
826823

827824
/**
@@ -921,10 +918,10 @@ private static void verifySetPropertyName(String name) {
921918
*/
922919
public String getProperty(String name, String defaultValue) {
923920
Object value = getPropertyRaw(name);
924-
if (value == null) {
921+
if (value == null || value instanceof NullNode) {
925922
return defaultValue;
926-
} else if (value instanceof JsonValue) {
927-
return ((JsonValue) value).toJson();
923+
} else if (value instanceof JsonNode) {
924+
return ((JsonNode) value).toString();
928925
} else if (value instanceof NullNode) {
929926
return defaultValue;
930927
} else if (value instanceof Number) {
@@ -1010,8 +1007,6 @@ public double getProperty(String name, double defaultValue) {
10101007
} else if (value instanceof Number) {
10111008
Number number = (Number) value;
10121009
return number.doubleValue();
1013-
} else if (value instanceof JsonValue) {
1014-
return ((JsonValue) value).asNumber();
10151010
} else if (value instanceof Boolean) {
10161011
return ((Boolean) value).booleanValue() ? 1 : 0;
10171012
} else if (value instanceof String) {
@@ -1057,8 +1052,8 @@ public int getProperty(String name, int defaultValue) {
10571052
/**
10581053
* Gets the raw property value without any value conversion. The type of the
10591054
* value is {@link String}, {@link Double}, {@link Boolean} or
1060-
* {@link JsonValue}. <code>null</code> is returned if there is no property
1061-
* with the given name or if the value is set to <code>null</code>.
1055+
* {@link BaseJsonNode}. <code>null</code> is returned if there is no
1056+
* property with the given name or if the value is set to <code>null</code>.
10621057
*
10631058
* @param name
10641059
* the property name, not null
@@ -1474,7 +1469,7 @@ public PendingJavaScriptResult callJsFunction(String functionName,
14741469
* <li>{@link Integer}
14751470
* <li>{@link Double}
14761471
* <li>{@link Boolean}
1477-
* <li>{@link JsonValue}
1472+
* <li>{@link BaseJsonNode}
14781473
* <li>{@link Element} (will be sent as <code>null</code> if the server-side
14791474
* element instance is not attached when the invocation is sent to the
14801475
* client)

flow-server/src/main/java/com/vaadin/flow/internal/change/EmptyChange.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.vaadin.flow.internal.change;
1717

18+
import com.fasterxml.jackson.databind.node.ObjectNode;
19+
1820
import com.vaadin.flow.internal.ConstantPool;
1921
import com.vaadin.flow.internal.nodefeature.NodeFeature;
2022
import com.vaadin.flow.internal.nodefeature.NodeList;
@@ -45,7 +47,7 @@ public EmptyChange(NodeFeature feature) {
4547
}
4648

4749
@Override
48-
protected void populateJson(JsonObject json, ConstantPool constantPool) {
50+
protected void populateJson(ObjectNode json, ConstantPool constantPool) {
4951
json.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_NOOP);
5052
if (NodeList.class.isAssignableFrom(getFeature())) {
5153
json.put(JsonConstants.CHANGE_FEATURE_TYPE, true);

flow-server/src/main/java/com/vaadin/flow/internal/change/ListAddChange.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@
2020
import java.util.List;
2121
import java.util.function.Function;
2222

23+
import com.fasterxml.jackson.databind.JsonNode;
24+
import com.fasterxml.jackson.databind.node.ArrayNode;
25+
import com.fasterxml.jackson.databind.node.ObjectNode;
26+
2327
import com.vaadin.flow.internal.ConstantPool;
24-
import com.vaadin.flow.internal.JsonCodec;
25-
import com.vaadin.flow.internal.JsonUtils;
28+
import com.vaadin.flow.internal.JacksonCodec;
29+
import com.vaadin.flow.internal.JacksonUtils;
2630
import com.vaadin.flow.internal.StateNode;
2731
import com.vaadin.flow.internal.nodefeature.NodeList;
2832
import com.vaadin.flow.shared.JsonConstants;
2933

30-
import elemental.json.Json;
31-
import elemental.json.JsonArray;
32-
import elemental.json.JsonObject;
33-
import elemental.json.JsonValue;
34-
3534
/**
3635
* Change describing an add operation in a {@link NodeList list} node feature.
3736
* <p>
@@ -106,26 +105,27 @@ public ListAddChange<T> copy(int indx) {
106105
}
107106

108107
@Override
109-
protected void populateJson(JsonObject json, ConstantPool constantPool) {
108+
protected void populateJson(ObjectNode json, ConstantPool constantPool) {
110109
json.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_SPLICE);
111110

112111
super.populateJson(json, constantPool);
113112

114113
json.put(JsonConstants.CHANGE_SPLICE_INDEX, getIndex());
115114

116-
Function<Object, JsonValue> mapper;
115+
Function<Object, JsonNode> mapper;
117116
String addKey;
118117
if (nodeValues) {
119118
addKey = JsonConstants.CHANGE_SPLICE_ADD_NODES;
120-
mapper = item -> Json.create(((StateNode) item).getId());
119+
mapper = item -> JacksonUtils
120+
.createNode(((StateNode) item).getId());
121121
} else {
122122
addKey = JsonConstants.CHANGE_SPLICE_ADD;
123-
mapper = item -> JsonCodec.encodeWithConstantPool(item,
123+
mapper = item -> JacksonCodec.encodeWithConstantPool(item,
124124
constantPool);
125125
}
126126

127-
JsonArray newItemsJson = newItems.stream().map(mapper)
128-
.collect(JsonUtils.asArray());
127+
ArrayNode newItemsJson = newItems.stream().map(mapper)
128+
.collect(JacksonUtils.asArray());
129129
json.put(addKey, newItemsJson);
130130
}
131131

flow-server/src/main/java/com/vaadin/flow/internal/change/ListClearChange.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
import java.io.Serializable;
1919

20+
import com.fasterxml.jackson.databind.node.ObjectNode;
21+
2022
import com.vaadin.flow.internal.ConstantPool;
2123
import com.vaadin.flow.internal.nodefeature.NodeList;
2224
import com.vaadin.flow.shared.JsonConstants;
2325

24-
import elemental.json.JsonObject;
25-
2626
/**
2727
* Change describing a clear operation in a {@link NodeList list} node feature.
2828
* <p>
@@ -54,7 +54,7 @@ public AbstractListChange<T> copy(int indx) {
5454
}
5555

5656
@Override
57-
protected void populateJson(JsonObject json, ConstantPool constantPool) {
57+
protected void populateJson(ObjectNode json, ConstantPool constantPool) {
5858
json.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_CLEAR);
5959
super.populateJson(json, constantPool);
6060
}

flow-server/src/main/java/com/vaadin/flow/internal/change/ListRemoveChange.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
import java.io.Serializable;
1919

20+
import com.fasterxml.jackson.databind.node.ObjectNode;
21+
2022
import com.vaadin.flow.internal.ConstantPool;
2123
import com.vaadin.flow.internal.nodefeature.NodeList;
2224
import com.vaadin.flow.shared.JsonConstants;
2325

24-
import elemental.json.JsonObject;
25-
2626
/**
2727
* Change describing a remove operation in a {@link NodeList list} node feature.
2828
* <p>
@@ -72,7 +72,7 @@ public AbstractListChange<T> copy(int indx) {
7272
}
7373

7474
@Override
75-
protected void populateJson(JsonObject json, ConstantPool constantPool) {
75+
protected void populateJson(ObjectNode json, ConstantPool constantPool) {
7676
json.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_SPLICE);
7777

7878
super.populateJson(json, constantPool);

flow-server/src/main/java/com/vaadin/flow/internal/change/MapPutChange.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
package com.vaadin.flow.internal.change;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
2019
import com.fasterxml.jackson.databind.node.ArrayNode;
21-
import com.fasterxml.jackson.databind.node.BaseJsonNode;
2220
import com.fasterxml.jackson.databind.node.BooleanNode;
2321
import com.fasterxml.jackson.databind.node.NullNode;
2422
import com.fasterxml.jackson.databind.node.NumericNode;
@@ -29,14 +27,10 @@
2927
import com.vaadin.flow.internal.ConstantPool;
3028
import com.vaadin.flow.internal.JacksonCodec;
3129
import com.vaadin.flow.internal.JacksonUtils;
32-
import com.vaadin.flow.internal.JsonCodec;
3330
import com.vaadin.flow.internal.StateNode;
3431
import com.vaadin.flow.internal.nodefeature.NodeFeature;
3532
import com.vaadin.flow.shared.JsonConstants;
3633

37-
import elemental.json.Json;
38-
import elemental.json.JsonObject;
39-
4034
/**
4135
* Change describing a changed value in a map feature.
4236
* <p>
@@ -88,7 +82,7 @@ public Object getValue() {
8882
}
8983

9084
@Override
91-
protected void populateJson(JsonObject json, ConstantPool constantPool) {
85+
protected void populateJson(ObjectNode json, ConstantPool constantPool) {
9286
// Set the type and key before calling super to make the keys appear in
9387
// a more logical order
9488
json.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_PUT);
@@ -98,34 +92,30 @@ protected void populateJson(JsonObject json, ConstantPool constantPool) {
9892

9993
if (value instanceof StateNode node) {
10094
json.put(JsonConstants.CHANGE_PUT_NODE_VALUE,
101-
Json.create(node.getId()));
95+
JacksonUtils.createNode(node.getId()));
10296
} else if (value instanceof ObjectNode node) {
103-
json.put(JsonConstants.CHANGE_PUT_VALUE, Json.parse(JacksonCodec
104-
.encodeWithConstantPool(node, constantPool).toString()));
105-
} else if (value instanceof NullNode) {
10697
json.put(JsonConstants.CHANGE_PUT_VALUE,
107-
JsonCodec.encodeWithConstantPool(null, constantPool));
98+
JacksonCodec.encodeWithConstantPool(node, constantPool));
99+
} else if (value instanceof NullNode) {
100+
json.put(JsonConstants.CHANGE_PUT_VALUE, (NullNode) value);
108101
} else if (value instanceof NumericNode node) {
109-
json.put(JsonConstants.CHANGE_PUT_VALUE, Json.create(JacksonCodec
110-
.encodeWithConstantPool(node, constantPool).doubleValue()));
102+
json.put(JsonConstants.CHANGE_PUT_VALUE,
103+
JacksonCodec.encodeWithConstantPool(node, constantPool));
111104
} else if (value instanceof BooleanNode node) {
112105
json.put(JsonConstants.CHANGE_PUT_VALUE,
113-
Json.create(JacksonCodec
114-
.encodeWithConstantPool(node, constantPool)
115-
.booleanValue()));
106+
JacksonCodec.encodeWithConstantPool(node, constantPool));
116107
} else if (value instanceof TextNode node) {
117-
json.put(JsonConstants.CHANGE_PUT_VALUE, Json.create(JacksonCodec
118-
.encodeWithConstantPool(node, constantPool).textValue()));
108+
json.put(JsonConstants.CHANGE_PUT_VALUE,
109+
JacksonCodec.encodeWithConstantPool(node, constantPool));
119110
} else if (value instanceof ValueNode node) {
120-
json.put(JsonConstants.CHANGE_PUT_VALUE, Json.create(JacksonCodec
121-
.encodeWithConstantPool(node, constantPool).toString()));
122-
} else if (value instanceof ArrayNode node) {
123111
json.put(JsonConstants.CHANGE_PUT_VALUE,
124-
JacksonUtils.createElementalArray((ArrayNode) JacksonCodec
125-
.encodeWithConstantPool(node, constantPool)));
112+
JacksonCodec.encodeWithConstantPool(node, constantPool));
113+
} else if (value instanceof ArrayNode node) {
114+
json.put(JsonConstants.CHANGE_PUT_VALUE, (ArrayNode) JacksonCodec
115+
.encodeWithConstantPool(node, constantPool));
126116
} else {
127117
json.put(JsonConstants.CHANGE_PUT_VALUE,
128-
JsonCodec.encodeWithConstantPool(value, constantPool));
118+
JacksonCodec.encodeWithConstantPool(value, constantPool));
129119
}
130120
}
131121
}

flow-server/src/main/java/com/vaadin/flow/internal/change/MapRemoveChange.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package com.vaadin.flow.internal.change;
1818

19+
import com.fasterxml.jackson.databind.node.ObjectNode;
20+
1921
import com.vaadin.flow.internal.ConstantPool;
2022
import com.vaadin.flow.internal.nodefeature.NodeMap;
2123
import com.vaadin.flow.shared.JsonConstants;
2224

23-
import elemental.json.JsonObject;
24-
2525
/**
2626
* Change describing a value removed from a map.
2727
* <p>
@@ -58,7 +58,7 @@ public String getKey() {
5858
}
5959

6060
@Override
61-
protected void populateJson(JsonObject json, ConstantPool constantPool) {
61+
protected void populateJson(ObjectNode json, ConstantPool constantPool) {
6262
// Set the type before calling super to make the keys appear in a more
6363
// logical order
6464
json.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_REMOVE);

0 commit comments

Comments
 (0)