Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson Java reserved keyword

Tags:

I have some JSON that I am deserializing using Gson.

{ "resp": { "posts": [   {     ...     "public": true,     ...   }]  } 

My problem is that public is a Java keyword, so how would I make a field in my class that correlates with the public field in the JSON?

like image 991
LanguagesNamedAfterCofee Avatar asked Jun 06 '11 22:06

LanguagesNamedAfterCofee


2 Answers

You could use a different name for your field, using gson's Field Naming Support.

public class Post {     @SerializedName("public")     private boolean isPublic;     ... } 
like image 67
StriplingWarrior Avatar answered Sep 28 '22 01:09

StriplingWarrior


Worth a quick note that you need to include gson.annotations or SerializedName for this to compile as not part of the base gson.Gson package:

import com.google.gson.annotations.SerializedName; 
like image 22
Ted Cahall Avatar answered Sep 27 '22 23:09

Ted Cahall