+ Start a Discussion
EagerToLearnEagerToLearn 

Wrapper Class with two inner classes (child) and how to pass to JSON Serialize and Deserialize

Hi,
At a high level, I have two different objects (not related) that I want to pass data from each to a customized service using JSON.  I am currently doing that with a single object but now I need to add a second. I have built what I thought could be a good visual example but keep in mind that I am not using account and contact but the user object and custom metadata setting as the two objects but this should give a concept that hopefully you could help me with a code snippet or something from mine below that could get an example done.
 

Could I get this to work where it provides a list of list or something like that and I pass it into the JSON serialized method; as mentioned I do this with a single list of users currently.  Again I just through something below together to give context but it uses accounts and contacts.

Bottomline, I need to pass to JSON serialize two different object records and deserialize them on the service side.  Any help, examples would be appreciated.


public class WrapperMain {
    public static List<AccountWrapper> awList = new List<AccountWrapper>();
    public static List<ContactWrapper> cwList = new List<ContactWrapper>();   
    
    {       
        for (Account a : [SELECT Name FROM Account LIMIT 1]) {
             AccountWrapper aw = new AccountWrapper();
            aw.Name = a.Name;
            awList.add(aw);
        }
        for (Contact c : [SELECT Name FROM Contact LIMIT 1]) {
            ContactWrapper cw = new ContactWrapper();
            cw.Name = c.Name;
            cwList.add(cw);
        }        
        
    }

    private class AccountWrapper {
       public String Name {get; set;}   
    }
    
    private class ContactWrapper {
        public String Name {get; set;}
    }
}

Best Answer chosen by EagerToLearn
EagerToLearnEagerToLearn
I thought that I posted the answer to my issues that I finally figured out but I am not seeing it!.
 
/* This class demonstrates two inner classes and
 * how to serialize then deserialize the content  
*/
public class WrapperMain {
    public List<AccountWrapper> awList = new List<AccountWrapper>();
    public List<ContactWrapper> cwList = new List<ContactWrapper>();  
    
    { 
		System.debug('WrapperMain');      
        for (Account a : [SELECT Name FROM Account LIMIT 2]) {
             AccountWrapper aw = new AccountWrapper();
            aw.Name = a.Name;
            awList.add(aw);
        }
        for (Contact c : [SELECT Name FROM Contact LIMIT 2]) {
        	ContactWrapper cw = new ContactWrapper();
            cw.Name = c.Name;
            cwList.add(cw);
        }  
    }

    public class AccountWrapper {
       public String Name {get; set;}   
    }
    
    public class ContactWrapper {
        public String Name {get; set;}
    }
}

public class WrapperProcess {
    WrapperMain wm = new WrapperMain();        
    
    public void WrapperProcess() {
        String serialized = JSON.serialize(wm, true);
		String serializedPretty = JSON.serializePretty(wm, true);
        System.debug('Pretty    : ' + serializedPretty);
        System.debug('Not Pretty: ' + serialized);       
        
		//Get the entire package
		WrapperMain  output = (WrapperMain) JSON.deserialize(serialized, WrapperMain.class);
        //Get just accounts
        List<WrapperMain.AccountWrapper> awList = output.awList;
        //Get just contacts
        List<WrapperMain.ContactWrapper> cwlist = output.cwList;
        
        System.debug('output:  ' + output);
        System.debug('aw.size: ' + awList.size());
        System.debug('aw:      ' + awList);
        System.debug('cw.size: ' + cwList.size());
        System.debug('cw:      ' + cwList);        
        
    }
}