All Projects → LachlanMcKee → Gsonpath

LachlanMcKee / Gsonpath

Licence: mit
A Java annotation processor library which generates gson type adapters using basic JsonPath style annotations

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Gsonpath

Kson
Gson TypeAdapter & Factory generator for Kotlin data classes
Stars: ✭ 90 (+66.67%)
Mutual labels:  annotation-processor, json, gson
Kripton
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.
Stars: ✭ 110 (+103.7%)
Mutual labels:  annotation-processor, json, annotations
AnnotationProcessorStarter
Project to set up basics of a Java annotation processor
Stars: ✭ 19 (-64.81%)
Mutual labels:  annotations, annotation-processor
ColdStorage
Lightweight data loading and caching library for android
Stars: ✭ 39 (-27.78%)
Mutual labels:  annotations, annotation-processor
WinAnalytics
A light-weight android library that can be quickly integrated into any app to use analytics tools.
Stars: ✭ 23 (-57.41%)
Mutual labels:  annotations, annotation-processor
aptk
A toolkit project to enable you to build annotation processors more easily
Stars: ✭ 28 (-48.15%)
Mutual labels:  annotations, annotation-processor
simple-preferences
Android Library to simplify SharedPreferences use with code generation.
Stars: ✭ 48 (-11.11%)
Mutual labels:  annotations, annotation-processor
dagger2-ktx
Kotlin extension bridge library for Dagger2 (proof-of-concept)
Stars: ✭ 41 (-24.07%)
Mutual labels:  annotations, annotation-processor
Preferenceroom
🚚 Android processing library for managing SharedPreferences persistence efficiently and structurally.
Stars: ✭ 341 (+531.48%)
Mutual labels:  annotation-processor, annotations
Jsonformat4flutter
受zzz40500/GsonFormat启发,将JSONObject格式的String解析成dart语言的实体类
Stars: ✭ 411 (+661.11%)
Mutual labels:  json, gson
Jsonschema2pojo
Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
Stars: ✭ 5,633 (+10331.48%)
Mutual labels:  json, gson
Moshix
Moshi Extensions
Stars: ✭ 243 (+350%)
Mutual labels:  annotation-processor, json
Placeholderview
This library provides advance views for lists and stacks. Some of the views are build on top of RecyclerView and others are written in their own. Annotations are compiled by annotation processor to generate bind classes. DOCS -->
Stars: ✭ 2,104 (+3796.3%)
Mutual labels:  annotation-processor, annotations
AnnotationProcessing
✔️ㅤ[ARTICLE] Writing your own Annotation Processors in Android
Stars: ✭ 47 (-12.96%)
Mutual labels:  annotations, annotation-processor
I18nplugin
Intellij idea i18next support plugin
Stars: ✭ 43 (-20.37%)
Mutual labels:  json, annotations
AutoBindings
Set of annotations that aims to make your Android development experience easier along with lint checks.
Stars: ✭ 15 (-72.22%)
Mutual labels:  annotations, annotation-processor
Kpoet
An expressive DSL built on top of JavaPoet to make writing code almost as easy as writing the code yourself.
Stars: ✭ 58 (+7.41%)
Mutual labels:  annotation-processor, annotations
Kotlin Builder Annotation
A minimal viable replacement for the Lombok @Builder plugin for Kotlin code
Stars: ✭ 67 (+24.07%)
Mutual labels:  annotation-processor, annotations
Immutables
Annotation processor to create immutable objects and builders. Feels like Guava's immutable collections but for regular value objects. JSON, Jackson, Gson, JAX-RS integrations included
Stars: ✭ 3,031 (+5512.96%)
Mutual labels:  annotation-processor, gson
Bandcamp Api
API wrapper for querying band, album, and track data from bandcamp.com
Stars: ✭ 20 (-62.96%)
Mutual labels:  json, gson

Gson Path

Maven Central Android Arsenal

An annotation processor library which generates gson type adapters at compile time which also use basic JsonPath functionality.

Benefits

The benefits of this library are as follows:

Statically generated Gson Type Adapters

By statically generating Gson Type Adapters, the majority of reflection used by the Gson library can be removed. This greatly improves performance and removes code obfuscation issues.

JsonPath syntax

JsonPath syntax can be used to reduce the number of POJOs required to parse a JSON file. See the example section for more details.

This allows for easier integration with other libraries which rely on a flat class structure (such as DBFlow).

POJO immutability via interfaces

You are given the option of using immutable POJOs based off annotated interfaces.

These POJOs are similar to AutoValue, however you do not need to reference the concrete implementation as the Type Adapter creates it on your behalf.

See the interfaces guide for further details.

JsonPath - Path Substitutions

You can reduce the amount of repetition when creating POJOs using Path Substitutions by using straightforward string replacement functionality within the AutoGsonAdapter annotation.

See the path substitution guide for further details.

Optional client side validation

Add optional client side validation to your json using @Nullable and NonNull annotations to add mandatory field constraints.

The client side valiation can also be enhanced through extensions. These extensions are separate annotation processors that register to be notified whenever a field with a specific annotation is encountered.

Notable extensions

Android Support Annotation validation

Example

Given the following JSON:

{
   "person": {
      "names": {
         "first": "Lachlan",
         "last": "McKee"
      }
   }
}

We can deserialize the content with a single class by using Gson Path. The following class demonstrates the annotations required to create a type adapter which can correctly read the content.

@AutoGsonAdapter(rootField = "person.names")
public class PersonModel {
   @SerializedName("first")
   String firstName;
   
   @SerializedName("last")
   String lastName;
}

We could also write it as follows (to reduce the number of annotations required):

@AutoGsonAdapter(rootField = "person.names")
public class PersonModel {
   String first;
   String last;
}

Setup

The following steps are required to use the generated TypeAdapters within your project.

AutoGsonAdapterFactory

Create a type adapter factory by annotating an interface as follows:

package com.example;
 
@AutoGsonAdapterFactory
public interface ExampleGsonTypeFactory extends TypeAdapterFactory {
}

Gson Path can be used across multiple modules by defining a factory within each.

Note: Only one @AutoGsonAdapterFactory annotation may be used per module/project. If you do this accidentally, the annotation processor will raise a helpful error.

AutoGsonAdapter

Create any number of type adapters by annotating a class or interface as follows:

package com.example;
 
@AutoGsonAdapter
public class ExampleModel {
    String value;
}

or

package com.example;
 
@AutoGsonAdapter
public interface ExampleModel {
    String getValue();
}

Gson Integration

For each type adapter factory interface, register it with your Gson builder as follows:

return new GsonBuilder()
                .registerTypeAdapterFactory(GsonPath.createTypeAdapterFactory(ExampleGsonTypeFactory.class))
                .create();

Incremental annotation processing

You must update your build.gradle (for each module) to opt into incremental annotation processing.

When enabled, all custom annotations (annotations which are either themselves annotated with either: @AutoGsonAdapter, @GsonSubType, @AutoGsonAdapterFactory) must be registered in the build.gradle (comma separated), otherwise they will be silently ignored.

Examples of this configuration are as follows:

// Standard annotation processor
javaCompileOptions {
   annotationProcessorOptions {
      arguments = [
         'gsonpath.incremental': 'true',
         'gsonpath.additionalAnnotations': 'com.example.CustomAutoGsonAdapter,com.example.CustomGsonSubType'
      ]
   }
}

// Kotlin annotation processor (kapt)
kapt {
   arguments {
      arg("gsonpath.incremental", "true")
      arg("gsonpath.additionalAnnotations", "com.example.CustomAutoGsonAdapter,com.example.CustomGsonSubType")
   }
}

Proguard

To use proguard within your project, you must add the generated type adapter factory. Using the example above, this would be:

-keep public class com.example.ExampleGsonTypeFactoryImpl

Download

This library is available on Maven, you can add it to your project using the following gradle dependencies:

compile 'net.lachlanmckee:gsonpath:4.0.0'
apt 'net.lachlanmckee:gsonpath-compiler:4.0.0'

compile 'net.lachlanmckee:gsonpath-kt:4.0.0' // an optional Kotlin library 
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].