<commit_before># Static Gson [![Circle CI](https://circleci.com/gh/gfx/StaticGson.svg?style=svg)](https://circleci.com/gh/gfx/StaticGson) [ ![Download](https://api.bintray.com/packages/gfx/maven/static-gson/images/download.svg) ](https://bintray.com/gfx/maven/static-gson/)

This library makes [Gson](https://github.com/google/gson) faster by generating `TypeAapterFactory` with annotation processing. In other words, this is an AOT compiler for Gson.

## Getting Started

For Android apps:

```gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

apply plugin: 'com.neenbedankt.android-apt'

repositories {
    jcenter()
}

// ...

dependencies {
    apt 'com.github.gfx.static_gson:static-gson-processor:0.9.7'
    compile 'com.github.gfx.static_gson:static-gson:0.9.7'
}
```

For Java apps with Gradle 2.12+:

```gradle
dependencies {
    compileOnly 'com.github.gfx.static_gson:static-gson-processor:0.9.7'
    compile 'com.github.gfx.static_gson:static-gson:0.9.7'
}
```

## Usage

Add `@JsonSerializable` to JSON serializable models:

```java
@JsonSerializable(fieldNamingPolicy = FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
public class User {
    public String firstName; // serialized to "first_name"
    public Stirng lastName; // serialized to "last_name"
}
```

Then, give `StaticGsonTypeAdapterFactory.newInstance()` to `GsonBuilder`:

```java
Gson gson = new GsonBuilder()
        .registerTypeAdapterFactory(StaticGsonTypeAdapterFactory.newInstance())
        .create();
```

That's all. `Gson#toJson()` and `Gson#fromGson()` becomes faster
for `@JsonSerializable` classes.

## ProGuard

Keep classes generated by the annotation processor.

```proguard
-keepnames @com.github.gfx.static_gson.annotation.StaticGsonGenerated class *
```

## Benchmark

See `example/MainActivity.java` for details.

On Xperia Z4 / Android 5.0.2:

```
$ adb logcat -v tag | ag D/XXX

D/XXX     : start benchmarking Dynamic Gson
D/XXX     : Dynamic Gson in serialization: 449ms
D/XXX     : Dynamic Gson in deserialization: 387ms
D/XXX     : start benchmarking Static Gson
D/XXX     : Static Gson in serialization: 198ms
D/XXX     : Static Gson in deserialization: 233ms
D/XXX     : start benchmarking Moshi
D/XXX     : Moshi in serialization: 270ms
D/XXX     : Moshi in deserialization: 656ms
D/XXX     : start benchmarking LoganSquare<commit_msg>Use -keep to keep generated classes in proguard<commit_after># Static Gson [![Circle CI](https://circleci.com/gh/gfx/StaticGson.svg?style=svg)](https://circleci.com/gh/gfx/StaticGson) [ ![Download](https://api.bintray.com/packages/gfx/maven/static-gson/images/download.svg) ](https://bintray.com/gfx/maven/static-gson/)

This library makes [Gson](https://github.com/google/gson) faster by generating `TypeAapterFactory` with annotation processing. In other words, this is an AOT compiler for Gson.

## Getting Started

For Android apps:

```gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

apply plugin: 'com.neenbedankt.android-apt'

repositories {
    jcenter()
}

// ...

dependencies {
    apt 'com.github.gfx.static_gson:static-gson-processor:0.9.7'
    compile 'com.github.gfx.static_gson:static-gson:0.9.7'
}
```

For Java apps with Gradle 2.12+:

```gradle
dependencies {
    compileOnly 'com.github.gfx.static_gson:static-gson-processor:0.9.7'
    compile 'com.github.gfx.static_gson:static-gson:0.9.7'
}
```

## Usage

Add `@JsonSerializable` to JSON serializable models:

```java
@JsonSerializable(fieldNamingPolicy = FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
public class User {
    public String firstName; // serialized to "first_name"
    public Stirng lastName; // serialized to "last_name"
}
```

Then, give `StaticGsonTypeAdapterFactory.newInstance()` to `GsonBuilder`:

```java
Gson gson = new GsonBuilder()
        .registerTypeAdapterFactory(StaticGsonTypeAdapterFactory.newInstance())
        .create();
```

That's all. `Gson#toJson()` and `Gson#fromGson()` becomes faster
for `@JsonSerializable` classes.

## ProGuard

Keep classes generated by the annotation processor.

```proguard
-keep @com.github.gfx.static_gson.annotation.StaticGsonGenerated class *
```

## Benchmark

See `example/MainActivity.java` for details.

On Xperia Z4 / Android 5.0.2:

```
$ adb logcat -v tag | ag D/XXX

D/XXX     : start benchmarking Dynamic Gson
D/XXX     : Dynamic Gson in serialization: 449ms
D/XXX     : Dynamic Gson in deserialization: 387ms
D/XXX     : start benchmarking Static Gson
D/XXX     : Static Gson in serialization: 198ms
D/XXX     : Static Gson in deserialization: 233ms
D/XXX     : start benchmarking Moshi
D/XXX     : Moshi in serialization: 270ms
D/XXX     : Moshi in deserialization: 656ms
D/XXX     : start benchmarking LoganSquare
{'fraction_non_alphanumeric': 0.12441648061700832, 'fraction_numerical': 0.0158311345646438, 'mean_word_length': 3.99290780141844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3885983', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1662, 'n_words': 384}