Skip to content

Instantly share code, notes, and snippets.

@ajduke
Created January 5, 2014 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajduke/8269508 to your computer and use it in GitHub Desktop.
Save ajduke/8269508 to your computer and use it in GitHub Desktop.
package in.ajduke.ap013;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Example34 {
public static void main(String[] args) {
Gson gson = new Gson();
String json = gson.toJson(new JsonClass());
System.out.println("Default behaviour....");
System.out.println(json);
gson = new GsonBuilder().setFieldNamingPolicy(
FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
json = gson.toJson(new JsonClass());
System.out.println("\nFields with lower case with dashes...");
System.out.println(json);
gson = new GsonBuilder().setFieldNamingPolicy(
FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES).create();
json = gson.toJson(new JsonClass());
System.out.println("\nFields with upper case with spaces...");
System.out.println(json);
}
}
class JsonClass {
String myField = "value1";
String myAnotherField = "value2";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment