tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 JSON > GOOGLE GSON > Lowercase Policy with Dashes

Lowercase Policy with Dashes 

Google-gson is a java library from Google for encoding and decoding JSON text. Get the latest binaries from http://code.google.com/p/google-gson/. The following example shows using FieldNamingPolicy.LOWER_CASE_WITH_DASHES field name generation policy.

File Name  :  
com/bethecoder/tutorials/google_gson/common/Student.java 
   
package com.bethecoder.tutorials.google_gson.common;

import java.util.Date;

public class Student {
  private String firstName;
  private String lastName;
  private int age;
  private String hobby;
  private Date dob;

  public Student(String firstName, String lastName, int age, String hobby,
      Date dob) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.hobby = hobby;
    this.dob = dob;
  }
  
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }

  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  public Date getDob() {
    return dob;
  }
  public void setDob(Date dob) {
    this.dob = dob;
  }

  public String toString() {
    return "Student[ " +
      "firstName = " + firstName +
      ", lastName = " + lastName +
      ", age = " + age +
      ", hobby = " + hobby +
      ", dob = " + dob +
      " ]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/google_gson/tests/PolicyLowerCaseWithDashes.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.google_gson.tests;

import java.util.Date;

import com.bethecoder.tutorials.google_gson.common.Student;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


public class PolicyLowerCaseWithDashes {

  /**
   @param args
   */
  public static void main(String[] args) {
    Gson gson = new GsonBuilder()
      .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
      .setPrettyPrinting().create();
    Student stud = new Student("Sriram""Kasireddi"2"Singing"new Date(11046));
    System.out.println(gson.toJson(stud));          
  }

}
   

It gives the following output,
{
  "first-name": "Sriram",
  "last-name": "Kasireddi",
  "age": 2,
  "hobby": "Singing",
  "dob": "May 6, 2010 12:00:00 AM"
}


                    Print

   



Books Recommendation

 
 
bl  br