Skip to content

Latest commit

 

History

History
53 lines (42 loc) · 1.26 KB

migrate-from-gson.md

File metadata and controls

53 lines (42 loc) · 1.26 KB
layout title
default
Migrate from gson

Migrate from gson

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

public static class TestObject {
    @Expose(deserialize = false)
    public String field1;
}

Gson gson = new GsonBuilder()
        .excludeFieldsWithoutExposeAnnotation()
        .create();
TestObject obj = gson.fromJson("{\"field1\":\"hello\"}", TestObject.class);

will be

GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
        .excludeFieldsWithoutExposeAnnotation()
        .build();
TestObject obj = JsonIterator.deserialize(config, "{\"field1\":\"hello\"}", TestObject.class);

serialization also works

public static class TestObject {
    public String field1 = "hello";
}

Gson gson = new GsonBuilder()
        .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
        .create();
String output = gson.toJson(new TestObject());
assertEquals("{\"Field1\":\"hello\"}", output);

will be

GsonCompatibilityMode config = new GsonCompatibilityMode.Builder()
        .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
        .build();
String output = JsonStream.serialize(config, new TestObject());
assertEquals("{\"Field1\":\"hello\"}", output);