Class JsonMigration

java.lang.Object
com.flowingcode.vaadin.jsonmigration.JsonMigration

public class JsonMigration extends Object
Provides a compatibility layer for JSON handling to abstract away breaking changes introduced in Vaadin version 25.

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 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 a method. In Vaadin 25, this method converts JsonValue into JsonNode.
      Parameters:
      object - the object to convert
      Returns:
      an Object suitable to use as the result of a ClientCallable method.
    • convertToJsonValue

      public static elemental.json.JsonValue convertToJsonValue(Object object)
      Converts a given Java object into a JsonValue.

      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 JsonValue representation of the object
    • setPropertyJson

      public static void setPropertyJson(Element element, String name, elemental.json.JsonValue json)
      Sets a JSON-valued property on a given Element, transparently handling version-specific method signatures.

      This method uses reflection to call the appropriate setPropertyJson method on the Element class, which has a different signature for its JSON parameter in library versions before and after Vaadin 25.

      Parameters:
      element - the Element on which to set the property
      name - the name of the property to set
      json - the JsonValue to 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 - the Element on which to run the JavaScript expression
      expression - the JavaScript expression to invoke
      parameters - 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

      public static elemental.json.JsonObject getEventData(DomEvent event)
      Gets additional data related to the event.
      Parameters:
      event - the DomEvent from which to retrieve the data
      Returns:
      a JSON object containing event data, never null
      See Also:
    • instrumentClass

      public static <T extends Component> Class<? extends T> instrumentClass(Class<T> clazz)
      Instruments a component class to ensure compatibility with Vaadin 25+ JSON handling changes in ClientCallable methods.

      This method creates a dynamically generated subclass of the given component class that automatically converts JsonValue return values from ClientCallable methods to the appropriate JsonNode type required by Vaadin 25+.

      Behavior by Vaadin version:

      • Vaadin 25+: Returns an instrumented subclass that overrides all @ClientCallable methods returning JsonValue types. These overridden methods call the parent implementation and then convert the result through convertToClientCallableResult(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 original clazz
      Throws:
      IllegalArgumentException - if the class does not meet the requirements for instrumentation
      RuntimeException - if the instrumentation fails
      See Also: