Skip to content

Commit 7872bca

Browse files
authored
Update migration-from-gson.md
1 parent 7b57a59 commit 7872bca

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

migration-from-gson.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: How to extend
77

88
Jsoniter support gson annotation, migrating from gson to jsoniter should be easy.
99

10-
```
10+
```java
1111
public static class TestObject {
1212
@Expose(deserialize = false)
1313
public String field1;
@@ -21,9 +21,33 @@ TestObject obj = gson.fromJson("{\"field1\":\"hello\"}", TestObject.class);
2121

2222
will be
2323

24-
```
24+
```java
2525
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
2626
.excludeFieldsWithoutExposeAnnotation()
2727
.build();
2828
TestObject obj = JsonIterator.deserialize(config, "{\"field1\":\"hello\"}", TestObject.class);
2929
```
30+
31+
serialization also works
32+
33+
```java
34+
public static class TestObject {
35+
public String field1 = "hello";
36+
}
37+
38+
Gson gson = new GsonBuilder()
39+
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
40+
.create();
41+
String output = gson.toJson(new TestObject());
42+
assertEquals("{\"Field1\":\"hello\"}", output);
43+
```
44+
45+
will be
46+
47+
```java
48+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
49+
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
50+
.build();
51+
String output = JsonStream.serialize(config, new TestObject());
52+
assertEquals("{\"Field1\":\"hello\"}", output);
53+
```

0 commit comments

Comments
 (0)