Leanpub Header

Skip to main content

Gson: Enjoy JSON (De-)Serialization in Java

Our learning package contains everything you need to know about Gson. Value your time & save yourself from endless hours of Google searches to quick fix your issues. Invest in your knowledge upfront & select the right package for you.

The book is based on Gson 2.8.

Extensive code samples are included, an additional workbook in the premium package.

This book is available in multiple packages!

Pick Your Package
PDF
EPUB
WEB
159
Pages
30,138Words
About

About

About the Book

Learn Every Detail About Gson

Every modern Java application will need to do some Java-JSON parsing in their application life cycle. Gson is the most commonly used library to do the Java-JSON serialization and deserialization automatically.

Data parsing and mapping is complex. Even though Gson makes things quite simple, there are a lot of details that can trip your development up. If it's crazy data structures, weird variable names or complex dynamic data, you can lose a lot of time on something that should be done in minutes. Gson solves most problems in just a few lines. You just need to know the solutions!

This book includes an extensive introduction to Gson's features. We recommend to read it before you even have to use Gson for the first time. But even if you're already an avid user of Gson and stuck at a problem, we're confident we'll solve your problem.

Use our extensive bundle to learn about:

  • Introduction to JSON & JSON-Java mapping
  • Quick start to jump right into Gson
  • Mapping of any Java type
  • Deep dive into Gson's configuration options
  • Practical examples of Gson customization
  • Even advanced topics (Streaming, Generics, ...) are covered
  • Necessary ProGuard configuration for Android app releases

Additionally, you'll get an extensive appendix:

  • Combining a customized Gson with Retrofit 2
  • Overview over common mistakes
  • Exploration of Gson alternatives

Who Is This Book For?

This book is for Android or Java developers who want to get a substantial overview and reference book for Gson. You’ll benefit from the clearly recognizable code examples in regard to your daily work with Gson.

Rookie

If you’re just starting out with Gson (or coming from any other Java-JSON library like Jackson databind) this book will show you all important parts on how to work with Gson. The provided code snippets let you jumpstart and create the first mapping within minutes.

Expert

You already worked with Gson before? You’ll profit from our extensive code snippets and can improve your existing code base. Additionally, the book illustrates various optimizations for an even better performance.

Code Samples

Every code snippet in the book has been tested. We provide our test project as a leanpub extra. Feel free to download it and check the full code base as you move through the book. We try to keep the code snippets in the book as short as possible. If more context is necessary, the code examples should provide it.

Workbook

For the first time, we provide additional separated resources besides our book. The workbook gives you a chance to implement everything you'll learn in this book yourself. In our experience actually using a library like Gson helps to reinforce the newly gained knowledge. Thus, we've designed the workbook, which gives you a guided playground to experiment and learn the features of Gson.

Packages

Pick Your Package

All packages include the ebook in the following formats: PDF, EPUB, and Web

Just The eBook

Minimum price

Suggested price$30.00

$25.00

  • Gson Sample Code
    Code snippets of the book in action as an Android project.

eBook & Workbook

Minimum price

Suggested price$35.00

$30.00

  • Gson Workbook
    This packages Includes the Gson workbook (https://leanpub.com/gson-workbook).
  • Gson Sample Code
    Code snippets of the book in action as an Android project.

Author

About the Author

Norman Peitek

futurestud.io is an online learning platform. Skyrocket in Android and Node.js with in-depth tutorials.

Contents

Table of Contents

About the Book

  1. What Topics Are Covered in This Book?
  2. Who Is This Book For?
  3. Code Examples
  4. Workbook
  5. Get the Workbook for Free

Chapter 1 — Introduction to Java-JSON Mapping

  1. Adding Gson as Dependency
  2. JSON Members, Properties & Fields
  3. Basics of Java-JSON Serialization
  4. Workbook Task A1
  5. Basics of Java-JSON Deserialization
  6. JSON Strings in Java
  7. Workbook Task A2

Chapter 2 — JSON Mapping with Gson

  1. Mapping of Nested Objects
  2. Workbook Task B1
  3. Automate Data Modelling
  4. Workbook Task B2
  5. Mapping of Arrays and Lists of Objects
  6. Keeping It Simple
  7. Workbook Task B3 & B5
  8. Workbook Task B4 & B6
  9. Multiple Property Names
  10. Lists Have to Be Consistent Objects
  11. Mapping of Maps
  12. Workbook Task B9
  13. Workbook Task B10
  14. Mapping of Sets
  15. Workbook Task B11
  16. Workbook Task B12
  17. Mapping of Null Values
  18. Mapping of Java Enums & Circular References

Chapter 3 — Gson Model Annotations

  1. How to Ignore Fields
  2. Workbook Task C1, C2 & C3
  3. How to Adjust the Naming of Fields
  4. Workbook Task C4 & C5
  5. Multiple Deserialization Names with @SerializedName
  6. Workbook Task C6

Chapter 4 — Configuring Gson with GsonBuilder

  1. GsonBuilder Basics
  2. Naming Policies
  3. Force Serialization of null Values
  4. Workbook Task D1
  5. Go Beyond transient and @Expose with Exclusion Strategies
  6. Workbook Task D2
  7. Relax Gson with Lenient
  8. Special Values of Floats/Doubles
  9. Workbook Task D3
  10. Model Versioning
  11. Workbook Task D4
  12. Formatting of Dates & Custom Date/Time Mapping
  13. Workbook Task D5 & D6
  14. Pretty Printing
  15. HTML Escaping
  16. Workbook Task D7

Chapter 5 — Advanced Gson Usage

  1. Mapping of Enums
  2. Workbook Task E1
  3. Workbook Task E2
  4. Mapping of Circular References
  5. Workbook Task E3
  6. Mapping of Generics
  7. Custom Serialization for Simplification (Part 1)
  8. Workbook Task E4
  9. Changing the Default Serialization with Custom Serialization (Part 2)
  10. Workbook Task E5
  11. Custom Deserialization Basics
  12. Workbook Task E6
  13. Custom Instance Creator
  14. Workbook Task E7
  15. Customizing (De)Serialization via @JsonAdapter
  16. Gson 2.7
  17. JsonElement, JsonArray & More
  18. Custom Deserialization for Calculated Fields
  19. On-The-Fly-Parsing With Streams
  20. Workbook Task F3
  21. Workbook Task F4

Chapter 6 — ProGuard Configuration

  1. Gson and ProGuard Are Not Best Friends
  2. Standard Gson Configuration

Chapter 7 — Practical Gson

  1. Low Level Parsing
  2. Workbook Task F1
  3. Storing Data in Android’s SharedPreferences With Gson
  4. How to Deserialize a List of Polymorphic Objects
  5. Unknown Types
  6. Workbook Task F2

Chapter 8 — Appendix

  1. Gson’s Configuration for Retrofit 2
  2. 5 Common Mistakes & Issues
  3. jsonschema2pojo.org
  4. Overview of Alternatives
  5. Gson’s Performance

Outro

  1. Workbook
  2. Talk to Us

Future Studio Books

  1. Retrofit
  2. Picasso
  3. Glide
  4. Future Studio Bundle

Contributors

About the Contributors

Marcus Pöhls

Marcus is an enthusiastic Android & Java developer. He’s developed multiple production-ready client and server applications. While following the core principle of performance for client apps, he also builds REST+JSON APIs that focus on clear and consistent design.

Besides his day job, sharing knowledge and teaching others has become a characteristic he never wants to miss.

Marcus has a Master’s degree in computer science from the Otto von Guericke University Magdeburg.

Feel free to get in touch on Twitter @marcuspoehls.

Christian Fellner

Christian is an experienced Software Engineer and consultant working with a wide variety of companies, ranging from startups to Fortune 500 companies.

His experiences range across a number of fields and technologies, including web and game development, virtual reality and machine learning.

Christian is an all-round nerd, passionate about outstanding design and creating innovational user experiences.

He has given training to many developers and students since 2011; explaining state of the art technologies and project management methodologies.

Christian has a BSc degree in Computer Systems in Engineering from the Otto von Guericke University Magdeburg.

Find him on Twitter: @celsiusF.

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub