1

I am exploring the JSONParser methods and trying to understand how its objects are structured and stored in Salesforce database. Here is my JSON string - String input = '{"name":"paulo","age":42, "car" : {"model" : "outlander", "year" : "2016"}}'; and I am trying to create a parser and print it like - JSONParser parser = JSON.createParser(jsonStr); System.debug('PARSER' +parser); It outputs- PARSERJSONParser:[delegate=common.apex.json.ApexJsonParser@9b2dd68] I am trying to run the class in the anonymous window. Also, the last part (@9b2dd68) varies with each execution. My first question - what does this output mean?

Now, I use a while loop to iterate the parser (as seen in the most examples)

JSONParser parser = JSON.createParser(jsonStr);
   System.debug('PARSER' +parser);
    while(parser!=null){
         System.debug('CurrentTokenName ' +parser.getCurrentToken());
         currentName = parser.getCurrentName();
         System.debug('CurrentName ' +currentName);
         currentText = parser.getText();
         System.debug('CurrentText ' +currentText); 
         parser.nextToken(); 
         System.debug('CurrentTokenName ' +parser.getCurrentToken());
         currentName = parser.getCurrentName();
         System.debug('CurrentName ' +currentName);
         currentText = parser.getText();
         System.debug('CurrentText ' +currentText);
         break;
    }

I see first outputs of first three system.debug() statements as null and below is the ouput of 4th, 5th 6th system.debug() statements :-

4th CurrentTokenName - START_OBJECT

5th CurrentName - null

6th CurrentText - {

My second questions- Why is it so ? / How parser's is structured? Is it in the form of linked-list / tree etc?

I also noticed- while (parser != null) { } goes to maybe an infinite loop and throws APEX CPU Time limit error, until I add 'break' statement. My third question- Why would it take so much of time just for the small JSON structure? Does it go to an infinte loop?

  • Manish, what are you trying to do? Have you tried using the JSON.deserialize methods? – Sebastian Kessel Apr 12 '19 at 20:04
  • Sebastian, I have used Json.deserialize in the past. As per my requirement, I need to update few of the nodes coming as the json response. So trying to explore parser methods – Manish Anand Apr 12 '19 at 20:55
  • 1
    Still not sure. If you deserialize into a regular object (or even into a Map<String, Object>) you can still change the structure.... and even re-serialize back into JSON – Sebastian Kessel Apr 12 '19 at 20:56
3

My first question - what does this output mean?

This output tells you two things: first, it is a delegate object, meaning that the actual implementation is written in Java (common.apex.json.ApexJsonParser). Apex is essentially a Virtual Machine inside a Virtual Machine, where some types of objects and methods are marshalled by the runtime. If you're really interested in knowing more, you can research how the Apex VM works on the Internet.

The second part identifies its location in heap memory (@9b2dd68). You can refer to this as a "reference" or "pointer", depending on your preferred terminology. It will typically be at different points of heap during each run, based on internal factors beyond our control. The main use of this part of the output is to be able to identify one object from another in the debug logs and check points.

My second questions- Why is it so ?

Before you've advanced, there is no current token. You need to move forward first.

How parser's is structured? Is it in the form of linked-list / tree etc?

We're not told how it works. Presumably, it's probably a variant of some JSON parsing library already available in Java.

My third question- Why would it take so much of time just for the small JSON structure? Does it go to an infinte loop?

Yes, parser will never become null until you make it so (parser = null). The more appropriate method (which would have at least partially solved your problems above), is to start off the loop like this:

while(parser.nextToken() != null) {
  ...
}

As long as there's something to do, the parser will automatically advance, then break the loop when processing is done.

I suggest reading JSON Parsing for a better understanding; it even has some example code you can follow along with.

| improve this answer | |

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