3

I'm doing a web service call, and parsing the json response into a wrapper object for display on a Visualforce page.

My wrapper object looks like:

public class CustomerAddresses {
    public String line1 {get; set;}
    public String line2 {get; set;}
    public String city {get; set;}
    public String state {get; set;}
}

My json parser looks like:

String fieldName = parser.getCurrentName();                
            if(fieldName == 'addr' && parser.getCurrentToken() == JsonToken.START_ARRAY){
                while(parser.nextToken()!= JsonToken.END_ARRAY){
                    if(parser.getCurrentToken()==JSONToken.START_OBJECT){
                        CustomerAddresses s = (CustomerAddresses)parser.readValueAs(CustomerAddresses.class);
                        customerAddressList.add(s);
                    }
                }
            }

and the json file looks something like:

    {
   "theresponse":{
      "addrs":{
         "addr":[
            {
               "line1":"11562 TEST DR",
               "line2":"ORANGEVILLE",
               "city":"GARDEN GROVE",
               "state":"AZ"
            },
            {
               "line1":"123 TEST BLVD",
               "line2":"TREE HARBOR",
               "city":"FAIRMONT",
               "state":"CA"
            }
         ]
      },

Here's my problem, if (in the future) someone adds another attribute to the json file, such as "zip" (see example below), then my class breaks because I can't create an object without the attribute being declared in the CustomerAddresses class. How can I ignore json attributes that aren't in my class CustomerAddresses? Thank you in advance for any help with this!!

Ex: bad json file:

        {
   "theresponse":{
      "addrs":{
         "addr":[
            {
               "line1":"11562 TEST DR",
               "line2":"ORANGEVILLE",
               "city":"GARDEN GROVE",
               "state":"AZ",
               "zip":"80001"
        },
6

The documentation for readValueAs says (emphasis mine):

When deserializing JSON content into an Apex class in any API version, or into an object in API version 35.0 or later, no exception is thrown. When no exception is thrown, this method ignores extraneous attributes and parses the rest of the JSON content.

My reading of this is that your code should continue to work when extra JSON attributes are added. But I suggest you create a test case to ensure that the code does what the documentation says.

| improve this answer | |
  • 1
    Ninja'd by seconds! - I'd also add to double check the version of the apex class being used for parsing as this "silently ignore" behavior is new as of the last two API releases (35 & 36). And for good measure, use the .readValueAsStrict() method to throw an exception if you don't desire this silent behavior post API v.35. – Mark Pond Mar 1 '16 at 22:43
  • 2
    +1, but I'd add that it might be easier to just deserialize using JSON.deserialize. – sfdcfox Mar 1 '16 at 22:45
  • @MarkPond Good to flag; the documentation reads like deserializing into an instance of an Apex class doesn't require the API >= 35 but if I was writing the code I'd set the API to >= 35 to be safer. – Keith C Mar 1 '16 at 22:48
  • 1
    @sfdcfox Agreed; not sure why JSONParser gets used so much. – Keith C Mar 1 '16 at 22:49
  • 1
    Good catch Keith. I missed the previous sentence in the documentation and incorrectly assumed what "object" meant. "When deserializing JSON content into a custom object or an sObject...". As for JSON.deserialize(), I guess some people just like to do it the hard(er) way. To each their own! – Mark Pond Mar 1 '16 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.