JSON Generator

  #
/** * This snippet simply uses StringBuffer to create a JSON data structure. * JSON Generator Class * * @author codercrunch * */ class JSONGenerator { private StringBuffer buf = new StringBuffer(); public static final String CURL_BEGIN = "{"; public static final String CURL_END = "}"; public static final String SQUARE_BEGIN = "["; public static final String SQUARE_END = "]"; public static final String COLON = ":"; public static final String COMMA = ","; public static final String QUOTES = ""; public static final String NEW_LINE = "\n"; private int pairCount = 0; /** * Begin the JSON Doc * * @param value */ public void beginDoc(String value) { buf.append(CURL_BEGIN).append(QUOTES).append(value).append(QUOTES); } /** * Add Object Attribute * @param key * @param value */ public void addPair(String key, String value) { if (pairCount > 0) { buf.append(COMMA).append(NEW_LINE); } else { buf.append(COLON).append(CURL_BEGIN).append(NEW_LINE); } buf.append(QUOTES).append(key).append(QUOTES).append(COLON).append( QUOTES).append(value).append(QUOTES); pairCount++; } /** * Add array to the JSON doc * * @param name * @param values */ public void addArray(String name, String values[]) { StringBuffer ar = new StringBuffer(); for (int i = 0; i < values.length; i++) { // append comma if (i > 0) { ar.append(COMMA); } ar.append(QUOTES).append(values[i]).append(QUOTES); } if (pairCount > 0) { buf.append(COMMA).append(NEW_LINE); } else { buf.append(COLON).append(CURL_BEGIN).append(NEW_LINE); } // add it as pair buf.append(QUOTES).append(name).append(QUOTES).append(COLON); buf.append(SQUARE_BEGIN); buf.append(ar.toString()); buf.append(SQUARE_END); } /** * End JSON doc */ public void endDoc() { buf.append(NEW_LINE); if (pairCount > 0) { buf.append(CURL_END); } buf.append(CURL_END); } public String toJSON() { return buf.toString(); } } /** * Test Class * @author java4learners * */ class JSONGeneratorTest { public static void main(String[] args) throws Exception { JSONGenerator doc = new JSONGenerator(); // populate a customer Customer cust = new Customer(); cust.setName("Mr A"); cust.setAge(33); cust.setCountry("USA"); String tels[] = { "111111111", "9999999999" }; cust.setTelephones(tels); // Add elements to the doc doc.beginDoc("customer"); doc.addPair("name", cust.getName()); doc.addPair("age", "" + cust.getAge()); doc.addPair("country", cust.getCountry()); doc.addArray("tel", cust.getTelephones()); doc.endDoc(); System.out.println("JSON struct :n" + doc.toJSON()); } } /** * Customer, a sample data structure to be serialized * * @author java4learners * */ class Customer { private String name; private int age; private String country; private String telephones[]; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String[] getTelephones() { return telephones; } public void setTelephones(String[] telephones) { this.telephones = telephones; } }

Also See



Loading Please Wait...