GsonValue


Source link: https://github.com/evant/gsonvalue

GsonValue

Compile-time generation of gson TypeAdapters to preserve class encapsulation.

By default, gson uses reflection to read and write fields of you class from json. The problem with this is that it breaks encapsulation. This prevents you from enforcing invariants when you class is constructed and properly hide implementation details from serialization. This library will generate TypeAdapters the call your constructor, factory method, or builder to construct you an instance of your class and will only use accessible fields and methods to write it out.

This library is a nice companion with google's AutoValue, but you may use it with any class you choose. It's nice with kotlin's data classes too!

Download

Gradle

Java

You may want to use a gradle plugin for easier management of apt dependencies. For example, gradle-apt-plugin.

apt 'me.tatarka.gsonvalue:gsonvalue-processor:0.7' compile 'me.tatarka.gsonvalue:gsonvalue:0.7'

Android

annotationProcessor 'me.tatarka.gsonvalue:gsonvalue-processor:0.7' compile 'me.tatarka.gsonvalue:gsonvalue:0.7'

Maven

<dependency>
<groupId>me.tatarka.gsonvalue</groupId>
<artifactId>gsonvalue</artifactId>
<version>0.7</version> </dependency> <dependency>
<groupId>me.tatarka.gsonvalue</groupId>
<artifactId>gsonvalue-processor</artifactId>
<version>0.7</version>
<scope>provided</scope> </dependency>

Usage

Annotate your constructor or factory method with @GsonConstructor. Json will be deserialized using the parameter names and serialized using public fields or getters. The following classes will map to the json

{
"arg":1
}

POJO

import me.tatarka.gsonvalue.annotations.GsonConstructor;  public class Foo {

  @GsonConstructor
  public Foo(int arg) {

...
  
}

 public int arg() {

...
  
}
 
}
  public class Bar {

  @GsonConstructor
  public static Bar newInstance(int arg) {

...
  
}

 public int getArg() {

...
  
}
 
}

AutoValue

@AutoValue public abstract class Foo {

  @GsonConstructor
  public static Foo newInstance(int arg) {

return new AutoValue_Foo(arg);

  
}

 public abstract int arg();
 
}

Kotlin Data Class

@GsonConstructor data class Foo(val arg: Int)

Note that both bare getters and bean-style prefixes are supported. Like AutoValue, prefixes only apply if all getters follow that style.

Builders

Alternatively, you can create a builder class. Annotate either the builder's constructor or the factory method that returns the builder with @GsonBuilder.

POJO

import me.tatarka.gsonvalue.annotations.GsonConstructor;  public class Foo {

  public static class Builder {

@GsonBuilder

public Builder(int arg1) {

 ...

}

public Builder arg2(String arg2) {

 ...

}

public Foo build() {

 ...

}

  
}
 
}
  public class Bar {

  @GsonBuilder
  public static Builder builder() {

...
  
}

 public static class Builder {

...
  
}
 
}

AutoValue

@AutoValue public abstract class Foo {

@GsonBuilder
  public static Builder builder() {

return new AutoValue_Foo.Builder();

  
}

@AutoValue.Builder
  public interface Builder {

  Builder arg(int arg);

Foo build();

  
}
 
}

In most cases the class being built will be discovered from the builder. However, it's possible that it is ambiguous. In that case, you can provide the built class in the annotation @GsonBuilder(Foo.class).

Gson

Create an abstract TypeAdapterFactory class an annotate it.

import com.google.gson.TypeAdapterFactory; import me.tatarka.gsonvalue.annotations.GsonValueTypeAdapterFactory;  @GsonValueTypeAdapterFactory public abstract class MyTypeAdapterFactory implements TypeAdapterFactory {

  public static MyTypeAdapterFactory create() {

return new GsonValue_MyTypeAdapterFactory();

  
}
 
}

Then register it to your gson builder.

gson = new GsonBuilder()

.registerTypeAdapterFactory(MyTypeAdapterFactory.create())

.create();

Alternatively, you can use the old reflection-based ValueTypeAdapterFactory.

gson = new GsonBuilder()

.registerTypeAdapterFactory(new ValueTypeAdapterFactory())

.create();

Supported Gson features.

  • @SerializeName is supported on fields or getters. It will map to both the constructor parameter on deserialization and the field or getter on serialization.
  • Transient fields are ignored.
  • @JsonAdapter is supported with either a TypeAdapter or TypeAdapterFactory as long as it has a public no-args constructor. Since gson's version cannot be placed on a getter method, you may use @me.tatarka.gsonvalue.annotations.JsonAdapter instead if necessary.

Unsupported features.

The following features are not supported. They may be added if there is enough demand.

  • @Since and @Until versioning support.
  • @Expose and exclusion strategies.
  • Field naming policies.

License

Copyright 2016 Evan Tatarka  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

It's Gradle Plug-in for Android Play-store Publisher API.

A type-safe interface of image processing algorithms.

Alexei has some predefined image processing calculus that are accessible from the class ImageProcessingThing:

  • Dominant Color
  • Color Palette
  • Average RGB

Blur Navigation Drawer like Etsy app.

Just replace your default toggle with this awesome blurred toggle effect by adding 4 letters!

Bubble Notification with an explosion animation.

This plugin adds support for writing Android applications using the Groovy language.

Plugin has been successfully tested with Android Studio. It is recommended, for performance, memory and battery life, that you use @CompileStatic wherever possible.

Lint Cleaner Plugin removes unused resources reported by Android lint including strings, colors and dimensions.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes