3

I have to send JSON request to external system. I have created a wrapper object and defined the variable. However, When I am serializing the wrapper class object , it's sorted the JSON request. I don't want JSON to be sorted.

Public class Wrarpper {
   string c;
   string A; 
   string B;

} 
Wrapper c  = new Wrapper ();
Json.serialize(C); 

When I serialize the Object...... JSON is coming C B A.... please help how to avoid sorting ... thanks.

  • 4
    Agreed its annoying and hard to understand how anyone would choose reverse alphabetic as an ordering as its not very people friendly. But in JSON "an object is an unordered set of name/value pairs" so the ordering is not significant as far as the meaning of the JSON is concerned so should not be a problem for any JSON parser. – Keith C Oct 29 '14 at 22:25
  • Thanks Keith. Is there any way to stop it ? I don't it to be sorted. – amidstCloud Oct 29 '14 at 22:29
  • The ordering is built into the JSON.serialize method so no if you stick to that (very convenient) method. But you can hand-write a one-off or maybe somewhat generic serializer using the JSONGenerator class if you want to tie down the order but the code is pretty ugly and easy to get wrong for anything but simple cases. – Keith C Oct 29 '14 at 22:34
3

For fully custom serialization:

@isTest
private class WrapperTest {

   public class Wrapper {
       string c;
       string A; 
       string B;
       public String toJson() {
           JSONGenerator g = JSON.createGenerator(false);
           g.writeStartObject();
           g.writeStringField('c', c);
           g.writeStringField('A', A);
           g.writeStringField('b', B);
           g.writeEndObject();
           return g.getAsString();
       }
   }

   @isTest
   static void test() {
        Wrapper w  = new Wrapper();
        w.c = 'cc';
        w.A = 'AA';
        w.B = 'BB';
        System.assertEquals('{"c":"cc","A":"AA","b":"BB"}', w.toJson());
   }
}
2

If you don't want it to be sorted, you have to write your own manual JSON emitter. This is non-trivial, but you can force the elements to have the appearance you want. Please note that this design is far slower (CPU time) than letting JSON do its own thing. In 99.9% of the cases you use JSON, the order doesn't matter. The only good reason for manual output is because you're afraid the receiving end can't handle the output. Such a framework might look like:

public interface JSONSerializable {
    string toJSON();
}

Then you can write a helper class:

public class JSON2 {
    public static string serialize(JSONSerializable[] items) {
        String[] result = new String[] { '[' };
        for(JSONSerializable item: items) {
            result.add(item.toJSON);
            result.add(',');
        }
        if(result.size()>1) result.remove(result.size()-1);
        result.add(']');
        return String.join(result, '');
    }
}

That will let you control the serialization, but each class must implement its own serializer this way.

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.