metadaddy-sfdc/json2apex — pull request #1 by metadaddy-sfdc (2012-01-16 23:00:42+00:00)
Title: Added template for generating explicit parse code

Given input JSON:

{
"someobject": {
"somestring": "abcd"
},
"somearray": [
{
"string1": "bcde"
},
{
"string2": "cdef"
}
],
"somestring": "abcd",
"someinteger": 123,
"someboolean": true,
"somedouble": 1.234,
"somelong" : 2147483648
}

the new 'Create explicit parse code' option will produce the following Apex code:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {
public static void consumeObject(JSONParser parser) {
Integer depth = 0;
do {
JSONToken curr = parser.getCurrentToken();
if (curr == JSONToken.START_OBJECT ||
curr == JSONToken.START_ARRAY) {
depth++;
} else if (curr == JSONToken.END_OBJECT ||
curr == JSONToken.END_ARRAY) {
depth--;
}
} while (depth > 0 && parser.nextToken() != null);
}

public class Somearray {
public String string1 {get;set;}
public String string2 {get;set;}

public Somearray(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
parser.nextToken();
if (text == 'string1') {
string1 = parser.getText();
} else if (text == 'string2') {
string2 = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Somearray consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}

public class Root {
public Someobject someobject {get;set;}
public List somearray {get;set;}
public String somestring {get;set;}
public Integer someinteger {get;set;}
public Boolean someboolean {get;set;}
public Double somedouble {get;set;}
public Long somelong {get;set;}

public Root(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
parser.nextToken();
if (text == 'someobject') {
someobject = new Someobject(parser);
} else if (text == 'somearray') {
somearray = new List();
while (parser.nextToken() != JSONToken.END_ARRAY) {
somearray.add(new Somearray(parser));
}
} else if (text == 'somestring') {
somestring = parser.getText();
} else if (text == 'someinteger') {
someinteger = parser.getIntegerValue();
} else if (text == 'someboolean') {
someboolean = parser.getBooleanValue();
} else if (text == 'somedouble') {
somedouble = parser.getDoubleValue();
} else if (text == 'somelong') {
somelong = parser.getLongValue();
} else {
System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}

public class Someobject {
public String somestring {get;set;}

public Someobject(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
parser.nextToken();
if (text == 'somestring') {
somestring = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Someobject consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}

public static Root parse(String json) {
return new Root(System.JSON.createParser(json));
}

// This test method should give 100% coverage
static testMethod void testParse() {
String json = '{\n \"someobject\": {\n \"somestring\": \"abcd\"\n },\n \"somearray\": [\n {\n \"string1\": \"bcde\"\n },\n {\n \"string2\": \"cdef\"\n }\n ],\n \"somestring\": \"abcd\",\n \"someinteger\": 123,\n \"someboolean\": true,\n \"somedouble\": 1.234,\n \"somelong\" : 2147483648\n}';
Root r = parse(json);
System.assert(r != null);

json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
Somearray objSomearray = new Somearray(System.JSON.createParser(json));
System.assert(objSomearray != null);
System.assert(objSomearray.string1 == null);
System.assert(objSomearray.string2 == null);

json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
Root objRoot = new Root(System.JSON.createParser(json));
System.assert(objRoot != null);
System.assert(objRoot.someobject == null);
System.assert(objRoot.somearray == null);
System.assert(objRoot.somestring == null);
System.assert(objRoot.someinteger == null);
System.assert(objRoot.someboolean == null);
System.assert(objRoot.somedouble == null);
System.assert(objRoot.somelong == null);

json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
Someobject objSomeobject = new Someobject(System.JSON.createParser(json));
System.assert(objSomeobject != null);
System.assert(objSomeobject.somestring == null);
}
}

I'm quite pleased with this one :-)