16
16
package com .vaadin .flow .component .react ;
17
17
18
18
import com .fasterxml .jackson .core .type .TypeReference ;
19
+ import com .fasterxml .jackson .databind .JsonNode ;
20
+ import com .fasterxml .jackson .databind .node .BaseJsonNode ;
19
21
20
22
import com .vaadin .flow .component .Component ;
21
23
import com .vaadin .flow .dom .DomListenerRegistration ;
22
24
import com .vaadin .flow .dom .Element ;
23
25
import com .vaadin .flow .function .SerializableConsumer ;
24
26
import com .vaadin .flow .function .SerializableFunction ;
25
- import com .vaadin .flow .internal .JsonCodec ;
26
- import com .vaadin .flow .internal .JsonUtils ;
27
+ import com .vaadin .flow .internal .JacksonCodec ;
28
+ import com .vaadin .flow .internal .JacksonUtils ;
27
29
28
- import com .vaadin .flow .internal .StateNode ;
30
+ import com .vaadin .flow .internal .JsonUtils ;
29
31
import com .vaadin .flow .internal .nodefeature .NodeProperties ;
30
32
31
- import elemental .json .Json ;
32
- import elemental .json .JsonValue ;
33
-
34
33
import java .util .HashMap ;
35
34
import java .util .Map ;
36
35
36
+ import elemental .json .JsonValue ;
37
+
37
38
/**
38
39
* An abstract implementation of an adapter for integrating with React
39
40
* components. To be used together with a React adapter Web Component that
@@ -110,7 +111,7 @@ protected <T> DomListenerRegistration addStateChangeListener(
110
111
* value to assign
111
112
*/
112
113
protected void setState (String stateName , Object value ) {
113
- getElement ().setPropertyJson (stateName , writeAsJson (value ));
114
+ getElement ().setPropertyJson (stateName , writeToJson (value ));
114
115
}
115
116
116
117
/**
@@ -153,12 +154,30 @@ protected <T> T getState(String stateName, TypeReference<T> typeReference) {
153
154
* @return converted object instance
154
155
* @param <T>
155
156
* type of result instance
157
+ * @deprecated use {@link #readFromJson(JsonNode, Class)} instead
156
158
*/
159
+ @ Deprecated
157
160
protected static <T > T readFromJson (JsonValue jsonValue ,
158
161
Class <T > typeClass ) {
159
162
return JsonUtils .readValue (jsonValue , typeClass );
160
163
}
161
164
165
+ /**
166
+ * Converts JsonValue into Java object of given type.
167
+ *
168
+ * @param jsonValue
169
+ * JSON value to convert, not {@code null}
170
+ * @param typeClass
171
+ * type class of converted object instance
172
+ * @return converted object instance
173
+ * @param <T>
174
+ * type of result instance
175
+ */
176
+ protected static <T > T readFromJson (JsonNode jsonValue ,
177
+ Class <T > typeClass ) {
178
+ return JacksonUtils .readValue (jsonValue , typeClass );
179
+ }
180
+
162
181
/**
163
182
* Converts JsonValue into Java object of given type.
164
183
*
@@ -169,23 +188,54 @@ protected static <T> T readFromJson(JsonValue jsonValue,
169
188
* @return converted object instance
170
189
* @param <T>
171
190
* type of result instance
191
+ * @deprecated use {@link #readFromJson(JsonNode, TypeReference)} instead
172
192
*/
193
+ @ Deprecated
173
194
protected static <T > T readFromJson (JsonValue jsonValue ,
174
195
TypeReference <T > typeReference ) {
175
196
return JsonUtils .readValue (jsonValue , typeReference );
176
197
}
177
198
199
+ /**
200
+ * Converts JsonValue into Java object of given type.
201
+ *
202
+ * @param jsonValue
203
+ * JSON value to convert, not {@code null}
204
+ * @param typeReference
205
+ * type reference of converted object instance
206
+ * @return converted object instance
207
+ * @param <T>
208
+ * type of result instance
209
+ */
210
+ protected static <T > T readFromJson (JsonNode jsonValue ,
211
+ TypeReference <T > typeReference ) {
212
+ return JacksonUtils .readValue (jsonValue , typeReference );
213
+ }
214
+
178
215
/**
179
216
* Converts Java object into JsonValue.
180
217
*
181
218
* @param object
182
219
* Java object to convert
183
220
* @return converted JSON value
221
+ * @deprecated use {@link #writeToJson(Object)}
184
222
*/
223
+ @ Deprecated
185
224
protected static JsonValue writeAsJson (Object object ) {
186
225
return JsonUtils .writeValue (object );
187
226
}
188
227
228
+ /**
229
+ * Converts Java object into JsonValue.
230
+ *
231
+ * @param object
232
+ * Java object to convert
233
+ * @return converted JSON value
234
+ */
235
+ protected static BaseJsonNode writeToJson (Object object ) {
236
+ return JacksonUtils .writeValue (object );
237
+ }
238
+
189
239
/**
190
240
* Get the Flow container element that is set up in React template for given
191
241
* name attribute.
@@ -210,29 +260,32 @@ protected Element getContentElement(String name) {
210
260
return contentMap .get (name );
211
261
}
212
262
213
- private JsonValue getPropertyJson (String propertyName ) {
263
+ private JsonNode getPropertyJson (String propertyName ) {
214
264
var rawValue = getElement ().getPropertyRaw (propertyName );
215
265
if (rawValue == null ) {
216
- return Json . createNull ();
217
- } else if (rawValue instanceof JsonValue jsonValue ) {
218
- return jsonValue ;
266
+ return JacksonUtils . nullNode ();
267
+ } else if (rawValue instanceof JsonNode jsonNode ) {
268
+ return jsonNode ;
219
269
} else if (rawValue instanceof String stringValue ) {
220
- return Json . create (stringValue );
270
+ return JacksonUtils . createNode (stringValue );
221
271
} else if (rawValue instanceof Double doubleValue ) {
222
- return Json . create (doubleValue );
272
+ return JacksonUtils . createNode (doubleValue );
223
273
} else if (rawValue instanceof Boolean booleanValue ) {
224
- return Json .create (booleanValue );
274
+ return JacksonUtils .createNode (booleanValue );
275
+ } else if (rawValue instanceof JsonValue jsonValue ) {
276
+ // TODO: remove when elemental dropped
277
+ return JacksonUtils .mapElemental (jsonValue );
225
278
} else {
226
- return Json . create (rawValue .toString ());
279
+ return JacksonUtils . createNode (rawValue .toString ());
227
280
}
228
281
}
229
282
230
283
private <T > DomListenerRegistration addJsonReaderStateChangeListener (
231
- String stateName , SerializableFunction <JsonValue , T > jsonReader ,
284
+ String stateName , SerializableFunction <JsonNode , T > jsonReader ,
232
285
SerializableConsumer <T > listener ) {
233
286
return getElement ().addPropertyChangeListener (stateName ,
234
287
stateName + "-changed" , (event -> {
235
- JsonValue newStateJson = JsonCodec
288
+ JsonNode newStateJson = JacksonCodec
236
289
.encodeWithoutTypeInfo (event .getValue ());
237
290
T newState = jsonReader .apply (newStateJson );
238
291
listener .accept (newState );
0 commit comments