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
Paul Dyson.ax812Paul Dyson.ax812 

More JSON.deserialize problems

We've migrated all our bespoke JSON handling code over to native. This all works fine in our development orgs but as soon as we try to package it we get lots of test failures; mostly just internal errors, sometimes JSONExceptions ("don't know the type of the Apex object to deserialize"). We're still somewhat baffled by this but have managed to at least recreate some of the problem.

 

With a smple class like:

 

public class JSONTest {
    public class JSONElement {
        public String one {get; set;}
        public List<String> two {get; set;}
        public List<JSONElement> three {get; set;}
    }
    
    public JSONTest() {
    }
    
    public String getJSON() {
        JSONElement e = new JSONElement();
        e.one = 'Test String';
        e.two = new String[] {'A', 'B'};
        
        JSONElement e1 = new JSONElement();
        JSONElement e2 = new JSONElement();
        e1.one = 'e1';
        e2.two = new String[] {'C', 'D', 'E'};
        
        e.three = new JSONElement[] {e1, e2};
        
        JSONElement[] es = new JSONElement[] {e, e1, e2};
        
        return System.JSON.serialize(es);
    }
}

 

and a test class (doesn't do any asserts, just exercises the behaviour):

 

@IsTest
private class JSONTestTest {
    public static testmethod void testGetJSON() {
        JSONTest t = new JSONTest();
        System.JSON.deserialize(t.getJSON(), JSONTest.JSONElement[].class);        
    }
}

 we get an internal salesforce error (Internal Salesforce Error: 807469011-10609 (503254483) (503254483)) in our packaging org but not in our development orgs. As far as we can tell the crucial but is trying to deserialise a collection, if getJSON() just returns a single JSONTest.JSONElement everything works fine (even though the JSONElement has a list of itself), make getJSON return a list of JSONTesst.JSONElements and it blows up. However it appears to be the deserialize only, if we don't deserialise and use debug to display the return value from getJSON:

 

[{"two":["A","B"],"three":[{"two":null,"three":null,"one":"e1"},{"two":["C","D","E"],"three":null,"one":null}],"one":"Test String"},{"two":null,"three":null,"one":"e1"},{"two":["C","D","E"],"three":null,"one":null}]

 which is valid JSON and looks correct for the code above.

 

Unfortunately we currently have so many tests failing we can't just comment them out and upload regardless.

 

SuperfellSuperfell

Seems like you're hitting the same problem as this thread

Paul Dyson.ax812Paul Dyson.ax812

Perhaps, although the circumstances in which we're seeing it a very different; ours work fine in dev orgs but not in managed package release org.