1

Keith C's answer builds the following "config" output:

   config: {option1: {isActive: true, isRequired: true},
             option2: {isActive: true, isRequired: true}
         }

by looping customobject data, building the structure in apex, and serializing it:

    //The loop in Keith's answer below builds the "config" output
// by serializing nested maps:
         Map<String, Object> m = new Map<String, Object>();
        for (options__C opt:options) {
            m.put(opt.Name__c, new Map<String, Boolean>{
                    'isActive' => opt.isActive__c,
                    'isRequired' => opt.isRequired__c
                    });
        }
        return JSON.serialize(m);

That works fine, but how would we modify Keith's answer to build the following different "config" structure?: In other words using Apex and a for (options__C opt:options) {...} loop , how would we build the following "config" structure?:

config: {
        allSettings : [
            {'isActive': opt.isActive__c, 'isRequired':opt.isRequired__c}, //options[0]
            {'isActive': opt.isActive__c, 'isRequired':opt.isRequired__c}, //options[1]
            {'isActive': opt.isActive__c, 'isRequired':opt.isRequired__c} //options[2]
        ]
    }

My attempt:

    for (options__C opt:options) {
    //options.size() == 3
//unsure how to build this into the 'allSettings' structure...
    }
1

Serializing via maps can get very confusing and convoluted very quickly.

Any time I need to come up with a specific format for serialization, I either write the Apex classes by hand or head on over to Json2Apex and plug my structure in, and have it generate the Apex classes I need based on the JSON structure I give it.

You can try it for yourself: Json2Apex

If you plug in the desired structure above you should get something similar to:

 public class ConfigContainer{
    public Config config {get; set;}
}

public class Config{
    public Setting[] allSettings {get; set;}
}

public class Setting{
    public boolean isActive {get; set;}
    public boolean isRequired {get; set;}
}

When you have the desired structure, then it's just a matter of plugging it in and generating the correct objects:

public class MyOptionsGenerator{

    public static String getMyOptionsString(){

        ConfigContainer cc = new ConfigContainer();
        cc.config = new Config();

        cc.config.allSettings = new List<Setting>();
        for(Options__c opt : options){
            cc.config.allSettings.add(new Setting(opt));
        }

        //Serialize the return type.
        return JSON.serialize(cc);
   }

    public class ConfigContainer{
        public Config config {get; set;}
    }

    public class Config{
        public List<Setting> allSettings {get; set;}
    }

    public class Setting{
        public boolean isActive {get; set;}
        public boolean isRequired {get; set;}

        public Setting(Options__c opt){
            this.isActive = opt.IsActive__c;
            this.isRequired = opt.isRequired__c;
        }
    }
}
  • Hi James, I havent tested this yet but it looks accurate and does address my question so I am accepting the answer. Thank you sir! – Peter Noges Apr 16 '18 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.