+ Start a Discussion
stephanie_zstephanie_z 

Unable to use JSON.deseralize to deseralize an object which contains a list of another object

Hi,

 

I am deseralizing the json string into an object which contains a list of another object, and getting this error:

 

Internal Salesforce Error: 17692538-4726 (1351590007) (1351590007) 



My code has been working up until this Wednesday. Was there any change that has been applied to cs2 env? This the test code I have:

 

public with sharing class JSONTest {
    public class InvoiceStatement {
        Long invoiceNumber;
        Datetime statementDate;
        Decimal totalPrice;
       
        public InvoiceStatement(Long i, Datetime dt, Decimal price)
        {
            invoiceNumber = i;
            statementDate = dt;
            totalPrice = price;
        }
    }
   
    public class InvoiceStatementResponse {
        Integer errorCode;
        List<InvoiceStatement> statements;
       
        public InvoiceStatementResponse(Integer errorCode) {
            this.errorCode = errorCode;
            statements = new List<InvoiceStatement>();
        }
    }
   
    public static void SerializeRoundtrip() {
        Datetime dt = Datetime.now();
        // Create a few invoices.
   
        InvoiceStatement inv1 = new InvoiceStatement(1,Datetime.valueOf(dt),1000);
        InvoiceStatement inv2 = new InvoiceStatement(2,Datetime.valueOf(dt),500);
        // Add the invoices to a list.
   
        List<InvoiceStatement> invoices = new List<InvoiceStatement>();
        invoices.add(inv1);
        invoices.add(inv2);
        
         InvoiceStatementResponse resp = new InvoiceStatementResponse(0);
         resp.statements = invoices;
                 
        String JSONString = JSON.serialize(resp);
        System.debug('Serialized list of invoices into JSON format: ' + JSONString);
       
        InvoiceStatementResponse dresp = (InvoiceStatementResponse)JSON.deserialize(JSONString, InvoiceStatementResponse.class);
        System.debug('dresp=' + dresp);
    }
   
    private static testmethod void mytest() {
        JSONTest.SerializeRoundtrip();
    }
   
}

 

Any help is appreciated.

 

 



Paul Dyson.ax812Paul Dyson.ax812

We're seeing the same in a whole load of tests preventing us uploading a new version of our managed package

rungerrunger

Okay, I see the problem.  Thanks for the report, we'll get this fixed ASAP.

It's a bug in the toString() of certain list types.  If you comment out the system.debug() method on line 44:

 

System.debug('dresp=' + dresp);

 

Your test should pass.

stephanie_zstephanie_z

Thanks Rich. That error went away after commenting out the ebug statement. I also notice System.assertEquals returns the same internal error. The equals method probably has the same problem. It would be great if this can be fixed as well.

Dan Blackhall.ax1171Dan Blackhall.ax1171

I am having what I think is the same issue. I am trying to serialise a List of sobjects and their children. for example:

 

 

List<ParentObject__c> objects = [SELECT Id, Name, (SELECT Id, Name FROM ChildObjects__r) FROM ParentObject__c];

//Serialization is working correctly String json = System.JSON.serialize(objects);
//Deserialization is not List<ParentObject__c> deserialized = (List<ParentObject__c>)System.JSON.deserialize(json, List<ParentObject__c>.class);

 

If I try System.debug(deserialized) I get a JSONException system

If I omit the debug statement as suggested, I get a different exception Malformed JSON: Expected [ at the beginning of List/Set

 

Is there an eta on when this will be fixed?

rungerrunger

This is a separate issue, specifically with parent/child sobject relationships and the list of lists that results from this particular type of query.

Thanks for the report, I'm looking into it now.

rungerrunger

Okay, this fix should make it into the upcoming major release.

Dan Blackhall.ax1171Dan Blackhall.ax1171

Great, thanks for the update Rich

slonghislonghi

I am getting the same error: Content cannot be displayed: Malformed JSON: Expected '[' at the beginning of List/Set

 

Has this been fixed?