Skip to content

Commit 4f932f2

Browse files
docs: how to map the event arguments to POJO
Related: #235
1 parent c7d50b8 commit 4f932f2

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

src/site/markdown/faq.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,197 @@ socket.connect();
205205
socket.disconnect();
206206
dispatcher.executorService().shutdown();
207207
```
208+
209+
## How to map the event arguments to POJO
210+
211+
This library uses the [JSONTokener](https://developer.android.com/reference/org/json/JSONTokener) class from the `org.json` package in order to parse the packets that are sent by the server, which means you will receive [JSONObjects](https://developer.android.com/reference/org/json/JSONObject) in your listeners.
212+
213+
Here's how you can convert these JSONObjects to Plain Old Java Objects (POJO):
214+
215+
- [with Jackson](#With_Jackson)
216+
- [with Gson](#With_Gson)
217+
218+
### With Jackson
219+
220+
`pom.xml`
221+
222+
```xml
223+
<?xml version="1.0" encoding="UTF-8"?>
224+
<project>
225+
<dependencies>
226+
<dependency>
227+
<groupId>com.fasterxml.jackson.core</groupId>
228+
<artifactId>jackson-core</artifactId>
229+
<version>2.13.3</version>
230+
</dependency>
231+
<dependency>
232+
<groupId>com.fasterxml.jackson.core</groupId>
233+
<artifactId>jackson-databind</artifactId>
234+
<version>2.13.3</version>
235+
</dependency>
236+
<dependency>
237+
<groupId>com.fasterxml.jackson.datatype</groupId>
238+
<artifactId>jackson-datatype-json-org</artifactId>
239+
<version>2.13.3</version>
240+
</dependency>
241+
...
242+
</dependencies>
243+
...
244+
</project>
245+
```
246+
247+
Maven repository:
248+
249+
- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
250+
- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
251+
- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-json-org
252+
253+
`src/main/java/MyApp.java`
254+
255+
```java
256+
public class MyApp {
257+
private static final ObjectMapper MAPPER = new ObjectMapper().registerModule(new JsonOrgModule());
258+
259+
public static void main(String[] argz) throws Exception {
260+
Socket socket = IO.socket(URI.create("https://example.com"));
261+
262+
socket.on("my-event", (args) -> {
263+
MyObject object = MAPPER.convertValue(args[0], MyObject.class);
264+
265+
// ...
266+
});
267+
268+
socket.connect();
269+
}
270+
271+
public static class MyObject {
272+
public int id;
273+
public String label;
274+
}
275+
}
276+
```
277+
278+
### With Gson
279+
280+
`pom.xml`
281+
282+
```xml
283+
<?xml version="1.0" encoding="UTF-8"?>
284+
<project>
285+
<dependencies>
286+
<dependency>
287+
<groupId>com.google.code.gson</groupId>
288+
<artifactId>gson</artifactId>
289+
<version>2.8.9</version>
290+
</dependency>
291+
...
292+
</dependencies>
293+
...
294+
</project>
295+
```
296+
297+
Maven repository:
298+
299+
- https://mvnrepository.com/artifact/com.google.code.gson/gson
300+
301+
`src/main/java/MyApp.java`
302+
303+
You can either call `toString()` on the `JSONObject`:
304+
305+
```java
306+
public class MyApp {
307+
private static final Gson GSON = new Gson();
308+
309+
public static void main(String[] argz) throws Exception {
310+
Socket socket = IO.socket(URI.create("https://example.com"));
311+
312+
socket.on("my-event", (args) -> {
313+
MyObject object = GSON.fromJson(args[0].toString(), MyObject.class);
314+
315+
// ...
316+
});
317+
318+
socket.connect();
319+
}
320+
321+
public static class MyObject {
322+
public int id;
323+
public String label;
324+
}
325+
}
326+
```
327+
328+
Or manually convert the `JSONObject` to a `JsonObject` (for performance purposes):
329+
330+
```java
331+
public class MyApp {
332+
private static final Gson GSON = new Gson();
333+
334+
public static void main(String[] argz) throws Exception {
335+
Socket socket = IO.socket(URI.create("https://example.com"));
336+
337+
socket.on("my-event", (args) -> {
338+
MyObject object = GSON.fromJson(map(args[0]), MyObject.class);
339+
340+
// ...
341+
});
342+
343+
socket.connect();
344+
}
345+
346+
public static class MyObject {
347+
public int id;
348+
public String label;
349+
}
350+
351+
public static JsonObject map(JSONObject source) throws JSONException {
352+
JsonObject output = new JsonObject();
353+
354+
Iterator<String> iterator = source.keys();
355+
while (iterator.hasNext()) {
356+
String key = iterator.next();
357+
Object value = source.get(key);
358+
359+
if (value instanceof JSONObject) {
360+
output.add(key, map((JSONObject) value));
361+
} else if (value instanceof JSONArray) {
362+
output.add(key, map((JSONArray) value));
363+
} else if (value instanceof Number) {
364+
output.addProperty(key, (Number) value);
365+
} else if (value instanceof String) {
366+
output.addProperty(key, (String) value);
367+
} else if (value instanceof Boolean) {
368+
output.addProperty(key, (Boolean) value);
369+
} else if (value instanceof Character) {
370+
output.addProperty(key, (Character) value);
371+
}
372+
}
373+
374+
return output;
375+
}
376+
377+
public static JsonArray map(JSONArray source) throws JSONException {
378+
JsonArray output = new JsonArray();
379+
380+
for (int i = 0; i < source.length(); i++) {
381+
Object value = source.get(i);
382+
383+
if (value instanceof JSONObject) {
384+
output.add(map((JSONObject) value));
385+
} else if (value instanceof JSONArray) {
386+
output.add(map((JSONArray) value));
387+
} else if (value instanceof Number) {
388+
output.add((Number) value);
389+
} else if (value instanceof String) {
390+
output.add((String) value);
391+
} else if (value instanceof Boolean) {
392+
output.add((Boolean) value);
393+
} else if (value instanceof Character) {
394+
output.add((Character) value);
395+
}
396+
}
397+
398+
return output;
399+
}
400+
}
401+
```

0 commit comments

Comments
 (0)