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
MickleMickle 

JSON deserialization System.UnexpectedException: Salesforce System Error

I have some JSON that I'm trying to deserialize using System.JSON.deserialize and am getting an unexpected error.

 

Thinking the error was made by me, I found SimonF's & Pat Patterson's awesome tool to generate the required Apex Class, and the automagically generated unit test is still failing.

 

http://json2apex.herokuapp.com/makeApex

 

Here's the code, complete with unit test that is failing

 

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

public class JSON2Apex {

	public class Data {
		public List<List<Detections>> detections;
	}

	public class Detections {
		public String language;
		public Boolean isReliable;
		public Double confidence;
	}

	public Data data;

	
	public static JSON2Apex parse(String json) {
		return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
	}
	
	static testMethod void testParse() {
		String json = '{'+
		' \"data\": {'+
		'  \"detections\": ['+
		'   ['+
		'    {'+
		'     \"language\": \"es\",'+
		'     \"isReliable\": false,'+
		'     \"confidence\": 0.6554622'+
		'    }'+
		'   ]'+
		'  ]'+
		' }'+
		'}';
		JSON2Apex obj = parse(json);
		System.assert(obj != null);
	}
}

Could this be a reserved word issue, or something having to do with inner classes? Any help would be appreciated.

 

Thanks,

Mike

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

It's not you, it's Force.com. A System.UnexpectedException is just that: an exception that was not expected to happen. Submit a case to support, and include the error number from one of your Run Tests output. I suspect that the cause is that a list of anonymously listed elements is not supported correctly. Best of luck with getting that fixed by support.

All Answers

AmitSahuAmitSahu

What line you are getting the error ?

 

MickleMickle

Here's the full debug log, the exception is being thrown as soon as I enter the parse method with the sample JSON.

 

25.0 APEX_CODE,FINE;APEX_PROFILING,FINE;DB,INFO;VALIDATION,INFO;WORKFLOW,FINEST
07:25:11.168 (168611000)|EXECUTION_STARTED
07:25:11.168 (168672000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE00000010Ddm|JSON2Apex.testParse
07:25:11.169 (169166000)|METHOD_ENTRY|[1]|01pE00000010Ddm|JSON2Apex.JSON2Apex()
07:25:11.169 (169297000)|METHOD_EXIT|[1]|JSON2Apex
07:25:11.169 (169377000)|METHOD_ENTRY|[34]|01pE00000010Ddm|JSON2Apex.parse(String)
07:25:11.188 (188402000)|METHOD_EXIT|[34]|01pE00000010Ddm|JSON2Apex.parse(String)
07:25:11.188 (188531000)|FATAL_ERROR|System.UnexpectedException: Salesforce System Error: 1070267378-232 (-37663348) (-37663348)

(System Code)
Class.JSON2Apex.parse: line 17, column 1
Class.JSON2Apex.testParse: line 34, column 1
07:25:11.188 (188629000)|FATAL_ERROR|System.UnexpectedException: Salesforce System Error: 1070267378-232 (-37663348) (-37663348)

(System Code)
Class.JSON2Apex.parse: line 17, column 1
Class.JSON2Apex.testParse: line 34, column 1
07:25:11.190 (188654000)|CUMULATIVE_LIMIT_USAGE
07:25:11.190|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Number of script statements: 3 out of 200000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10

07:25:11.190|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
07:25:11.190|STATIC_VARIABLE_LIST|
String:_sfdcAdditionalCodeLocations:0
double:MIN_NORMAL:0
double:POSITIVE_INFINITY:0
long:serialVersionUID:0
Boolean:TRUE:0
double:MIN_VALUE:0
int:SIZE:0
int[]:sizeTable:0
char[]:DigitOnes:0
char[]:DigitTens:0
double:NaN:0
String:_sfdcAdditionalCodeLocations:0
double:NEGATIVE_INFINITY:0
int:MIN_VALUE:0
int:SIZE:0
double:MAX_VALUE:0
long:serialVersionUID:0
int:MAX_EXPONENT:0
int:MIN_EXPONENT:0
Boolean:FALSE:0
String:_sfdcSuppressedCodeLocations:0
int:MAX_VALUE:0
char[]:digits:0
long:serialVersionUID:0
String:__sfdcInnerTypes:0
String:_sfdcAdditionalCodeLocations:0

07:25:11.190|CUMULATIVE_LIMIT_USAGE_END

07:25:11.188 (188829000)|CODE_UNIT_FINISHED|JSON2Apex.testParse
07:25:11.188 (188839000)|EXECUTION_FINISHED
07:25:11.439|CUMULATIVE_PROFILING_BEGIN
07:25:11.439|CUMULATIVE_PROFILING|No profiling information for SOQL operations
07:25:11.439|CUMULATIVE_PROFILING|No profiling information for SOSL operations
07:25:11.439|CUMULATIVE_PROFILING|No profiling information for DML operations
07:25:11.439|CUMULATIVE_PROFILING|method invocations|
Class.JSON2Apex.testParse: line 34, column 1: public static JSON2Apex parse(String): executed 1 time in 19 ms
External entry point: static testMethod void testParse(): executed 1 time in 19 ms
Class.JSON2Apex.parse: line 17, column 1: global static Object deserialize(String, system.Type): executed 1 time in 18 ms

07:25:11.439|CUMULATIVE_PROFILING_END

sfdcfoxsfdcfox

It's not you, it's Force.com. A System.UnexpectedException is just that: an exception that was not expected to happen. Submit a case to support, and include the error number from one of your Run Tests output. I suspect that the cause is that a list of anonymously listed elements is not supported correctly. Best of luck with getting that fixed by support.

This was selected as the best answer
MickleMickle

Thanks, sfdcfox.

 

That's what I figured, but wanted to know if were any known gotchas. I have a case logged with support and hopefully it can get resolved quickly.

 

:)

 

Pat PattersonPat Patterson

Hi Mike,

 

It does look like you've found a hole in our JSON support. I used the 'Create explicit parse code' option in JSON2Apex and edited the resulting code to come up with the following version that works:

 

//
// 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 Data {
        public List<List<Detection>> detections {get;set;}

        public Data(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'detections') {
                            detections = new List<List<Detection>>();
                            while (parser.nextToken() != JSONToken.END_ARRAY) {
                                List<Detection> innerList = new List<Detection>();
                                detections.add(innerList);
                                while (parser.nextToken() != JSONToken.END_ARRAY) {
                                    innerList.add(new Detection(parser));
                                }
                            }
                        } else {
                            System.debug(LoggingLevel.WARN, 'Data consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }
    
    public class Detection {
        public String language {get;set;}
        public Boolean isReliable {get;set;}
        public Double confidence {get;set;}

        public Detection(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'language') {
                            language = parser.getText();
                        } else if (text == 'isReliable') {
                            isReliable = parser.getBooleanValue();
                        } else if (text == 'confidence') {
                            confidence = parser.getDoubleValue();
                        } else {
                            System.debug(LoggingLevel.WARN, 'Detections consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }
    
    public Data data {get;set;}

    public JSON2Apex(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'data') {
                        data = new Data(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
    
    
    public static JSON2Apex parse(String json) {
        return new JSON2Apex(System.JSON.createParser(json));
    }
    
    // This test method should give 100% coverage
    static testMethod void testParse() {
        String json = '{ \"data\": {  \"detections\": [   [    {     \"language\": \"es\",     \"isReliable\": false,     \"confidence\": 0.6554622    }   ]  ] }}';
        JSON2Apex r = parse(json);
        System.assert(r != null);
        System.assert(r.data != null);
        System.assert(r.data.detections != null);
        System.assert(r.data.detections[0] != null);
        System.assert(r.data.detections[0][0] != null);
        System.assert(r.data.detections[0][0].language == 'es');
        System.assert(r.data.detections[0][0].isReliable == false);
        System.assert(r.data.detections[0][0].confidence == 0.6554622);
        
        json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
        Data objData = new Data(System.JSON.createParser(json));
        System.assert(objData != null);
        System.assert(objData.detections == null);

        json = '{\"TestAMissingObject\": { \"TestAMissingArray\": [ { \"TestAMissingProperty\": \"Some Value\" } ] } }';
        Detection objDetection = new Detection(System.JSON.createParser(json));
        System.assert(objDetection != null);
        System.assert(objDetection.language == null);
        System.assert(objDetection.isReliable == null);
        System.assert(objDetection.confidence == null);

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

 

Hope this is useful as a workaround. I'll bring this thread to the attention of the relevant R&D folks to increase the likelihood of a fix in a future release.

 

Cheers,

 

Pat