Class JsonMigration
This utility class detects the runtime version and uses version-specific helpers to ensure that code calling its methods does not need to be aware of underlying Vaadin API changes.
- Author:
- Javier Godoy
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends elemental.json.JsonValue>
TconvertToClientCallableResult(T object) Converts a given Java object into the return type of amethod.static elemental.json.JsonValueconvertToJsonValue(Object object) Converts a given Java object into aJsonValue.executeJs(Element element, String expression, Serializable... parameters) Asynchronously runs the given JavaScript expression in the browser in the context of this element.static elemental.json.JsonObjectgetEventData(DomEvent event) Gets additional data related to the event.instrumentClass(Class<T> clazz) Instruments a component class to ensure compatibility with Vaadin 25+ JSON handling changes inClientCallablemethods.static voidsetPropertyJson(Element element, String name, elemental.json.JsonValue json) Sets a JSON-valued property on a givenElement, transparently handling version-specific method signatures.
-
Constructor Details
-
JsonMigration
public JsonMigration()
-
-
Method Details
-
convertToClientCallableResult
public static <T extends elemental.json.JsonValue> T convertToClientCallableResult(T object) Converts a given Java object into the return type of amethod. In Vaadin 25, this method convertsJsonValueintoJsonNode.- Parameters:
object- the object to convert- Returns:
- an
Objectsuitable to use as the result of aClientCallablemethod.
-
convertToJsonValue
Converts a given Java object into aJsonValue.This method delegates the conversion to a version-specific helper to handle any differences in the serialization process.
- Parameters:
object- the object to convert- Returns:
- the
JsonValuerepresentation of the object
-
setPropertyJson
Sets a JSON-valued property on a givenElement, transparently handling version-specific method signatures.This method uses reflection to call the appropriate
setPropertyJsonmethod on theElementclass, which has a different signature for its JSON parameter in library versions before and after Vaadin 25.- Parameters:
element- theElementon which to set the propertyname- the name of the property to setjson- theJsonValueto be set as the property's value
-
executeJs
public static ElementalPendingJavaScriptResult executeJs(Element element, String expression, Serializable... parameters) Asynchronously runs the given JavaScript expression in the browser in the context of this element.- Parameters:
element- theElementon which to run the JavaScript expressionexpression- the JavaScript expression to invokeparameters- parameters to pass to the expression- Returns:
- a pending result that can be used to get a value returned from the expression
- See Also:
-
getEventData
Gets additional data related to the event.- Parameters:
event- theDomEventfrom which to retrieve the data- Returns:
- a JSON object containing event data, never
null - See Also:
-
instrumentClass
Instruments a component class to ensure compatibility with Vaadin 25+ JSON handling changes inClientCallablemethods.This method creates a dynamically generated subclass of the given component class that automatically converts
JsonValuereturn values fromClientCallablemethods to the appropriateJsonNodetype required by Vaadin 25+.Behavior by Vaadin version:
- Vaadin 25+: Returns an instrumented subclass that overrides all
@ClientCallablemethods returningJsonValuetypes. These overridden methods call the parent implementation and then convert the result throughconvertToClientCallableResult(JsonValue)to ensure compatibility with the new Jackson-based JSON API. - Vaadin 24 and earlier: Returns the original class unchanged, as no instrumentation is needed for the elemental.json API.
- Type Parameters:
T- the type of the component class- Parameters:
clazz- the component class to instrument- Returns:
- in Vaadin 25+, an instrumented subclass of
clazz; in earlier versions, the originalclazz - Throws:
IllegalArgumentException- if the class does not meet the requirements for instrumentationRuntimeException- if the instrumentation fails- See Also:
- Vaadin 25+: Returns an instrumented subclass that overrides all
-