Code Monkey home page Code Monkey logo

gson-flatten's Introduction

gson-flatten

gson-flatten

Library to collapse inner objects when parsing json

  1. To use library you should add it in your build.gradle:

        compile 'org.itishka.gson-flatten:gson-flatten:0.7'
  2. Then register it in your gson builder:

    final Gson gson = new GsonBuilder()
              .registerTypeAdapterFactory(new FlattenTypeAdapterFactory())
              .create();
  3. Define your class and use @Flatten annotation to get data from embedded objects. For example class

    class Weather {
      @Flatten("temperature::min")
      int min_temperture;
      @Flatten("temperature::max")
      int max_temperture;
    }

    will be filled with data from json

    {
      "temperature": {
         "min": -273,
         "max": 1000
      }
    }
  4. Then just parse or serialize json as your usually do:

      String json = gson.toJson(weather);
      Weather weather2 = gson.fromJson(json, Weather.class);

Additional features

  • You can skip field name if it equlas in inner object and outer one. E.g.:
class Weather {
    @Flatten("temperature::")
    int min;
    @Flatten("temperature::")
    int max;
}
  • You can get array items as well (tnx @tominsam):
class SomeClass {
    @Flatten("some_array::0")
    int first_value;
}
  • If you are using Gson 2.7 with some FieldNamingStrategies with and gson-flatten 0.6 you do not need to do anything more!

With previous versions of gson and gson-flatten you should provide FieldNamingStrategy to FlattenTypeAdapterFactory:

final Gson gson = new GsonBuilder()
          .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
          .registerTypeAdapterFactory(new FlattenTypeAdapterFactory(FieldNamingPolicy.UPPER_CAMEL_CASE))
          .create();

License

Based on https://github.com/noties/Flatten

Gson-flatten is released under LGPL 3.0 license

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.