function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex.AcostaAlex.Acosta 

System.JSONParser is not serializable.

Now I've used this logic multiple times in the past... and they are working just fine, but this time for what ever reason I'm getting this error, 'System.JSONParser is not serializable.'

 

On my controller, if I just call a new instance of the class for example:

HttpResponse response; // has response from outbound call 

// This works just fine
new JSONResponseParser(response.getBody());

 

This is where I get the exception on my controller...

HttpResponse response; // has response from outbound call 

// This throws me the error
JSONResponseParser myParsedJSON = new JSONResponseParser(response.getBody());

 

 

Why is this occuring? I've done this before in the past just fine... is there just a limit to the JSON return size that causes it to not be serializable? 

 

 

My sample code that's doing the parser...

public class JSONResponseParser{
	private ResponseWrapper theResponse;
	private final JSONParser parser;
		
	public JSONResponseParser(String jsonString){
	    parser = JSON.createParser(jsonString);
	    parseJSON();
	}
		
	private void parseJSON(){	
	    while (parser.nextToken() != null) {
	        if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
	            theResponse = (ResponseWrapper)parser.readValueAs(ResponseWrapper.class);                
	        }
	    }
        }		
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

Well if anyone else ever has this issue, it seems to resolve itself when I declare the varible to be transient.

 

// global variable
public transient JSONResponseParser myParsedJSON { get; set; } // inside contructor myParsedJSON = new JSONResponseParser(response.getBody());