Skip to content

Commit ebf8f54

Browse files
authored
feat: Internals to jackson (#21064)
Change internal functions to use jackson. part of #20741
1 parent 44f4e68 commit ebf8f54

File tree

9 files changed

+116
-55
lines changed

9 files changed

+116
-55
lines changed

flow-server/src/main/java/com/vaadin/flow/component/webcomponent/WebComponent.java

+62-13
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import java.io.Serializable;
2020
import java.util.Objects;
2121

22+
import com.fasterxml.jackson.databind.JsonNode;
23+
import com.fasterxml.jackson.databind.node.ObjectNode;
24+
import com.fasterxml.jackson.databind.node.ValueNode;
25+
2226
import com.vaadin.flow.component.Component;
2327
import com.vaadin.flow.dom.Element;
28+
import com.vaadin.flow.internal.JacksonUtils;
2429
import com.vaadin.flow.server.webcomponent.PropertyConfigurationImpl;
2530
import com.vaadin.flow.server.webcomponent.WebComponentBinding;
2631

27-
import elemental.json.Json;
28-
import elemental.json.JsonObject;
2932
import elemental.json.JsonValue;
3033

3134
/**
@@ -81,14 +84,55 @@ public WebComponent(WebComponentBinding binding, Element componentHost) {
8184

8285
/**
8386
* Fires a custom event on the client-side originating from the web
84-
* component. This event does not bubble in the DOM hierarchy.
87+
* component with custom event data. This event does not bubble in the DOM
88+
* hierarchy.
8589
*
8690
* @param eventName
8791
* name of the event, not null
92+
* @param objectData
93+
* data the event should carry. This data is placed as the {@code
94+
* detail} property of the event, nullable
8895
* @see #fireEvent(String, JsonValue, EventOptions) for full set of options
8996
*/
97+
@Deprecated
98+
public void fireEvent(String eventName, JsonValue objectData) {
99+
fireEvent(eventName, objectData, BASIC_OPTIONS);
100+
}
101+
102+
/**
103+
* Fires a custom event on the client-side originating from the web
104+
* component with custom event data. Allows modifying the default event
105+
* behavior with {@link EventOptions}.
106+
*
107+
* @param eventName
108+
* name of the event, not null
109+
* @param objectData
110+
* data the event should carry. This data is placed as the {@code
111+
* detail} property of the event, nullable
112+
* @param options
113+
* event options for {@code bubbles}, {@code cancelable}, and
114+
* {@code
115+
* composed} flags, not null
116+
* @throws NullPointerException
117+
* if either {@code eventName} or {@code options} is
118+
* {@code null}
119+
*/
120+
@Deprecated
121+
public void fireEvent(String eventName, JsonValue objectData,
122+
EventOptions options) {
123+
fireEvent(eventName, JacksonUtils.mapElemental(objectData), options);
124+
}
125+
126+
/**
127+
* Fires a custom event on the client-side originating from the web
128+
* component. This event does not bubble in the DOM hierarchy.
129+
*
130+
* @param eventName
131+
* name of the event, not null
132+
* @see #fireEvent(String, JsonNode, EventOptions) for full set of options
133+
*/
90134
public void fireEvent(String eventName) {
91-
fireEvent(eventName, Json.createNull(), BASIC_OPTIONS);
135+
fireEvent(eventName, JacksonUtils.nullNode(), BASIC_OPTIONS);
92136
}
93137

94138
/**
@@ -101,9 +145,9 @@ public void fireEvent(String eventName) {
101145
* @param objectData
102146
* data the event should carry. This data is placed as the {@code
103147
* detail} property of the event, nullable
104-
* @see #fireEvent(String, JsonValue, EventOptions) for full set of options
148+
* @see #fireEvent(String, JsonNode, EventOptions) for full set of options
105149
*/
106-
public void fireEvent(String eventName, JsonValue objectData) {
150+
public void fireEvent(String eventName, JsonNode objectData) {
107151
fireEvent(eventName, objectData, BASIC_OPTIONS);
108152
}
109153

@@ -125,21 +169,20 @@ public void fireEvent(String eventName, JsonValue objectData) {
125169
* if either {@code eventName} or {@code options} is
126170
* {@code null}
127171
*/
128-
public void fireEvent(String eventName, JsonValue objectData,
172+
public void fireEvent(String eventName, JsonNode objectData,
129173
EventOptions options) {
130174
Objects.requireNonNull(eventName,
131175
"Parameter 'eventName' must not be " + "null!");
132176
Objects.requireNonNull(options, "Parameter 'options' must not be null");
133177

134-
JsonObject object = Json.createObject();
178+
ObjectNode object = JacksonUtils.createObjectNode();
135179
object.put("bubbles", options.isBubbles());
136180
object.put("cancelable", options.isCancelable());
137181
object.put("composed", options.isComposed());
138-
object.put("detail",
139-
objectData == null ? Json.createNull() : objectData);
182+
object.set("detail",
183+
objectData == null ? JacksonUtils.nullNode() : objectData);
140184

141-
componentHost.executeJs(String.format(CUSTOM_EVENT, object.toJson()),
142-
eventName);
185+
componentHost.executeJs(String.format(CUSTOM_EVENT, object), eventName);
143186
}
144187

145188
/**
@@ -222,9 +265,15 @@ private void setProperty(String propertyName, Object value) {
222265
} else if (value instanceof Boolean) {
223266
componentHost.executeJs(UPDATE_PROPERTY, propertyName,
224267
(Boolean) value);
268+
} else if (value instanceof ValueNode) {
269+
// this gets around executeJavaScript limitation.
270+
// Since properties can take JSON values, this was needed to allow
271+
// that expected behavior.
272+
componentHost.executeJs(String.format(UPDATE_PROPERTY_FORMAT,
273+
((ValueNode) value).toString()), propertyName);
225274
} else if (value instanceof JsonValue) {
226275
// this gets around executeJavaScript limitation.
227-
// Since properties can take JsonValues, this was needed to allow
276+
// Since properties can take JSON values, this was needed to allow
228277
// that expected behavior.
229278
componentHost.executeJs(String.format(UPDATE_PROPERTY_FORMAT,
230279
((JsonValue) value).toJson()), propertyName);

flow-server/src/main/java/com/vaadin/flow/i18n/TranslationFileRequestHandler.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.vaadin.flow.i18n;
1717

18+
import com.vaadin.flow.internal.JacksonUtils;
1819
import com.vaadin.flow.server.HandlerHelper;
1920
import com.vaadin.flow.server.HttpStatusCode;
2021
import com.vaadin.flow.server.SynchronizedRequestHandler;
@@ -23,8 +24,7 @@
2324
import com.vaadin.flow.server.VaadinSession;
2425
import com.vaadin.flow.shared.JsonConstants;
2526

26-
import elemental.json.Json;
27-
import elemental.json.JsonObject;
27+
import com.fasterxml.jackson.databind.node.ObjectNode;
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
3030

@@ -131,10 +131,10 @@ private void handleCustomI18NProvider(VaadinSession session,
131131

132132
private void writeFileToResponse(VaadinResponse response,
133133
ResourceBundle translationPropertyFile) throws IOException {
134-
JsonObject json = Json.createObject();
134+
ObjectNode json = JacksonUtils.createObjectNode();
135135
translationPropertyFile.keySet().forEach(
136136
key -> json.put(key, translationPropertyFile.getString(key)));
137-
response.getWriter().write(json.toJson());
137+
response.getWriter().write(json.toString());
138138
}
139139

140140
private Locale getLocale(VaadinRequest request) {

flow-server/src/main/java/com/vaadin/flow/internal/UsageStatisticsExporter.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
import java.io.Serializable;
2020
import java.util.stream.Collectors;
2121

22+
import com.fasterxml.jackson.databind.node.ObjectNode;
2223
import org.jsoup.nodes.Document;
2324

24-
import elemental.json.Json;
25-
import elemental.json.JsonObject;
26-
2725
/**
2826
* A class for exporting {@link UsageStatistics} entries.
2927
* <p>
@@ -58,11 +56,11 @@ public static void exportUsageStatisticsToDocument(Document document) {
5856

5957
private static String createUsageStatisticsJson(
6058
UsageStatistics.UsageEntry entry) {
61-
JsonObject json = Json.createObject();
59+
ObjectNode json = JacksonUtils.createObjectNode();
6260

6361
json.put("is", entry.getName());
6462
json.put("version", entry.getVersion());
6563

66-
return json.toJson();
64+
return json.toString();
6765
}
6866
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

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

19+
import com.fasterxml.jackson.databind.JsonNode;
20+
1921
import com.vaadin.flow.internal.ConstantPool;
22+
import com.vaadin.flow.internal.JacksonCodec;
2023
import com.vaadin.flow.internal.JsonCodec;
2124
import com.vaadin.flow.internal.StateNode;
2225
import com.vaadin.flow.internal.nodefeature.NodeFeature;
@@ -84,10 +87,12 @@ protected void populateJson(JsonObject json, ConstantPool constantPool) {
8487

8588
super.populateJson(json, constantPool);
8689

87-
if (value instanceof StateNode) {
88-
StateNode node = (StateNode) value;
90+
if (value instanceof StateNode node) {
8991
json.put(JsonConstants.CHANGE_PUT_NODE_VALUE,
9092
Json.create(node.getId()));
93+
} else if (value instanceof JsonNode node) {
94+
json.put(JsonConstants.CHANGE_PUT_VALUE, Json.parse(JacksonCodec
95+
.encodeWithConstantPool(node, constantPool).toString()));
9196
} else {
9297
json.put(JsonConstants.CHANGE_PUT_VALUE,
9398
JsonCodec.encodeWithConstantPool(value, constantPool));

flow-server/src/main/java/com/vaadin/flow/internal/nodefeature/ElementAttributeMap.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import java.util.Optional;
2424
import java.util.stream.Stream;
2525

26+
import com.fasterxml.jackson.databind.JsonNode;
27+
import com.fasterxml.jackson.databind.node.ObjectNode;
28+
29+
import com.vaadin.flow.internal.JacksonUtils;
2630
import com.vaadin.flow.internal.NodeOwner;
2731
import com.vaadin.flow.internal.StateNode;
2832
import com.vaadin.flow.internal.StateTree;
@@ -34,9 +38,6 @@
3438
import com.vaadin.flow.server.VaadinSession;
3539
import com.vaadin.flow.shared.Registration;
3640

37-
import elemental.json.Json;
38-
import elemental.json.JsonObject;
39-
4041
/**
4142
* Map for element attribute values.
4243
* <p>
@@ -112,13 +113,13 @@ public String get(String attribute) {
112113
return (String) value;
113114
} else {
114115
// If the value is not a string then current impl only uses
115-
// JsonObject
116-
assert value instanceof JsonObject;
117-
JsonObject object = (JsonObject) value;
116+
// JsonNode
117+
assert value instanceof JsonNode;
118+
JsonNode node = (JsonNode) value;
118119
// The only object which may be set by the current imlp contains
119120
// "uri" attribute, only this situation is expected here.
120-
assert object.hasKey(NodeProperties.URI_ATTRIBUTE);
121-
return object.getString(NodeProperties.URI_ATTRIBUTE);
121+
assert node.has(NodeProperties.URI_ATTRIBUTE);
122+
return node.get(NodeProperties.URI_ATTRIBUTE).textValue();
122123
}
123124
}
124125

@@ -158,9 +159,9 @@ private void doSetResource(String attribute,
158159
} else {
159160
targetUri = StreamResourceRegistry.getURI(resource);
160161
}
161-
JsonObject object = Json.createObject();
162+
ObjectNode object = JacksonUtils.createObjectNode();
162163
object.put(NodeProperties.URI_ATTRIBUTE, targetUri.toASCIIString());
163-
// don't use sring as a value, but wrap it into an object to let know
164+
// don't use string as a value, but wrap it into an object to let know
164165
// the client side about specific nature of the value
165166
doSet(attribute, object);
166167
}

flow-server/src/main/java/com/vaadin/flow/internal/springcsrf/SpringCsrfTokenUtil.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717

1818
import java.util.Optional;
1919

20+
import com.fasterxml.jackson.databind.JsonNode;
2021
import org.jsoup.nodes.DataNode;
2122
import org.jsoup.nodes.Element;
2223

2324
import jakarta.servlet.ServletRequest;
2425

25-
import com.vaadin.flow.internal.JsonUtils;
26+
import com.vaadin.flow.internal.JacksonUtils;
2627
import com.vaadin.flow.server.VaadinRequest;
2728

28-
import elemental.json.JsonObject;
29-
3029
/**
3130
* A util class for helping dealing with Spring CSRF token.
3231
*/
@@ -64,18 +63,17 @@ public static Optional<SpringCsrfToken> getSpringCsrfToken(
6463
private static Optional<SpringCsrfToken> extractTokenFromBean(
6564
Object springCsrfToken) {
6665
if (springCsrfToken != null) {
67-
JsonObject springCsrfTokenJson = JsonUtils
66+
JsonNode springCsrfTokenJson = JacksonUtils
6867
.beanToJson(springCsrfToken);
6968
if (springCsrfTokenJson != null
70-
&& springCsrfTokenJson.hasKey(SPRING_CSRF_TOKEN_PROPERTY)
71-
&& springCsrfTokenJson
72-
.hasKey(SPRING_CSRF_HEADER_PROPERTY)) {
69+
&& springCsrfTokenJson.has(SPRING_CSRF_TOKEN_PROPERTY)
70+
&& springCsrfTokenJson.has(SPRING_CSRF_HEADER_PROPERTY)) {
7371
String token = springCsrfTokenJson
74-
.getString(SPRING_CSRF_TOKEN_PROPERTY);
72+
.get(SPRING_CSRF_TOKEN_PROPERTY).textValue();
7573
String headerName = springCsrfTokenJson
76-
.getString(SPRING_CSRF_HEADER_PROPERTY);
74+
.get(SPRING_CSRF_HEADER_PROPERTY).textValue();
7775
String parameterName = springCsrfTokenJson
78-
.getString(SPRING_CSRF_PARAMETER_PROPERTY);
76+
.get(SPRING_CSRF_PARAMETER_PROPERTY).textValue();
7977

8078
return Optional.of(
8179
new SpringCsrfToken(headerName, parameterName, token));

flow-server/src/main/java/com/vaadin/flow/server/frontend/CvdlProducts.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
import java.io.IOException;
2020
import java.nio.charset.StandardCharsets;
2121

22+
import com.vaadin.flow.internal.JacksonUtils;
2223
import com.vaadin.pro.licensechecker.Product;
2324

25+
import com.fasterxml.jackson.databind.JsonNode;
2426
import org.apache.commons.io.FileUtils;
2527

26-
import elemental.json.Json;
27-
import elemental.json.JsonObject;
28-
2928
/** Utilities for commercial product handling. */
3029
public class CvdlProducts {
3130

@@ -48,11 +47,12 @@ public static Product getProductIfCvdl(File nodeModules, String npmModule) {
4847
}
4948

5049
try {
51-
JsonObject packageJson = Json.parse(FileUtils
50+
JsonNode packageJson = JacksonUtils.readTree(FileUtils
5251
.readFileToString(packageJsonFile, StandardCharsets.UTF_8));
53-
if (packageJson.hasKey(CVDL_PACKAGE_KEY)) {
54-
return new Product(packageJson.getString(CVDL_PACKAGE_KEY),
55-
packageJson.getString("version"));
52+
if (packageJson.has(CVDL_PACKAGE_KEY)) {
53+
return new Product(
54+
packageJson.get(CVDL_PACKAGE_KEY).textValue(),
55+
packageJson.get("version").textValue());
5656
}
5757
return null;
5858
} catch (IOException e) {

0 commit comments

Comments
 (0)