Coder Social home page Coder Social logo

gson-tutorial's Introduction

gson-tutorial

Guide to google gson library

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.
Gson can work with arbitrary Java objects including pre-existing objects that you do not have a source code of. The following tutorials will demonstrate how you can leverage GSON to manage your JSON conversions.
Gson was originally created for use inside Google where it is currently used in a number of projects. It is now used by a number of public projects and companies.

Goals for Gson

  • Provide easy to use mechanisms like toString() and constructor (factory method) to convert Java to JSON and vice-versa
  • Allow pre-existing unmodifiable objects to be converted to and from JSON
  • Allow custom representations for objects
  • Support arbitrarily complex objects
  • Generate compact and readable JSON output

GSON Maven Dependency

To use Gson with Maven2/3, you can use the Gson version available in Maven Central by adding the following dependency:
<dependencies>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
      <scope>compile</scope>
    </dependency>
</dependencies>

Two ways to create Gson objects

Gson object can be created in two ways. First way gives you a quick Gson object ready for faster coding, while the second way uses GsonBuilder to build a more sophisticated Gson object.
//First way to create a Gson object for faster coding
Gson gson = new Gson();
//Second way to create a Gson object using GsonBuilder
Gson gson = new GsonBuilder()
             .disableHtmlEscaping()
             .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
             .setPrettyPrinting()
             .serializeNulls()
             .create();

Using Gson and GsonBuilder Examples

  • GSON - Serializing and Deserializing Generic Types
  • In this quick article, we will see how to serialize and deserialize a generic class using GSON. Generic type information is lost while serializing because of Java Type Erasure. You can solve this problem by specifying the correct parameterized type for your generic type. Gson provides this with the TypeToken class.

  • GSON - Serializing and Deserializing Enums
  • In this quick article, we show you how to serialize and deserialize enum types to and from its JSON representation.

  • Gson - Serializing Inner Classes Example
  • In this article, we will discuss how to serialization/deserialization of classes having inner classes.
  • GSON - Excluding fields from JSON with @Expose Annotation
  • In this quick article, we will discuss how to mark certain fields of our Java objects to be excluded for consideration for serialization and deserialization to JSON.

  • GSON - Null Object Support
  • Gson by default generates optimized JSON content ignoring the NULL values. But GsonBuilder provides flags to show NULL values in the JSON output using the GsonBuilder.serializeNulls() method.

  • GSON - Version Support Example
  • In this quick article, we will discuss how to use @Since annotation to support multiple versions of the same object. GSON introduced the @Since annotation to support multiple versions of the same object. We can use this annotation on Classes and Fields.
  • Convert JSON to a Map Using Gson
  • In this quick tutorial, we’ll learn how to convert a JSON string to a Map using Gson from Google.

    We’ll see three different approaches to accomplish that and discuss their pros and cons – with some practical examples.

Reference

gson-tutorial's People

Contributors

rameshmf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.