Skip to content

Commit 295f183

Browse files
javier-godoypaodb
authored andcommitted
docs(readme): add examples to README.md
1 parent 4eb4b74 commit 295f183

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

README.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ Provides a compatibility layer for JSON handling to abstract away breaking chang
1010

1111
## Features
1212

13-
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.
13+
- **Zero-effort migration**: Write your code once and run it seamlessly on Vaadin 14, 23, 24 and 25
14+
- **Automatic version detection**: Detects the runtime Vaadin version and uses the appropriate JSON handling strategy
15+
- **Drop-in replacement**: Simple static methods that replace version-specific APIs
16+
1417

1518
## Download release
1619

@@ -84,9 +87,68 @@ Json Migration Helper is written by Flowing Code S.A.
8487

8588
# Developer Guide
8689

87-
## Getting started
90+
## Using Lombok @ExtensionMethod
91+
92+
The `JsonMigration` class is designed to be used with Lombok's `@ExtensionMethod` annotation. This allows you to call the helper methods as if they were instance methods of `Element` or `DomEvent`:
93+
94+
```java
95+
import com.flowingcode.vaadin.jsonmigration.JsonMigration;
96+
import lombok.experimental.ExtensionMethod;
97+
98+
@ExtensionMethod(JsonMigration.class)
99+
public class MyComponent extends Div {
100+
101+
public MyComponent() {
102+
getElement().setPropertyJson("property", jsonValue);
103+
104+
getElement().executeJs("console.log($0)", jsonValue)
105+
.then(json->{ ... });
106+
107+
getElement().addEventListener("click", event -> {
108+
JsonObject eventData = event.getEventData();
109+
});
110+
}
111+
}
112+
```
113+
114+
## Returning JSON from ClientCallable methods
115+
116+
When a `@ClientCallable` method needs to return a JSON value, use `convertToClientCallableResult` to ensure compatibility across Vaadin versions:
117+
118+
```java
119+
@ClientCallable
120+
public Object getJsonData() {
121+
JsonValue json = ...;
122+
return JsonMigration.convertToClientCallableResult(json);
123+
}
124+
```
125+
126+
## Direct Usage
127+
128+
The helper methods can also be used directly from the `JsonMigration` class:
129+
130+
```java
131+
import com.flowingcode.vaadin.jsonmigration.JsonMigration;
132+
import elemental.json.Json;
133+
import elemental.json.JsonValue;
134+
135+
// ...
136+
137+
// 1. Setting a JSON Property
138+
JsonValue json = Json.createObject();
139+
// ... populate json
140+
JsonMigration.setPropertyJson(element, "property", json);
141+
142+
// 2. Executing JavaScript
143+
JsonMigration.executeJs(element, "console.log($0)", "Hello World");
144+
145+
// 3. Getting Event Data
146+
element.addEventListener("click", event -> {
147+
JsonObject eventData = JsonMigration.getEventData(event);
148+
// ...
149+
}).addEventData("event.detail");
150+
```
88151

89-
Add your code samples in this section
90152

91153
## Special configuration when using Spring
92154

0 commit comments

Comments
 (0)