Note that there are some explanatory texts on larger screens.

plurals
  1. PODelphi SuperObject sorting values in a different order than added
    primarykey
    data
    text
    <p>I'm using SuperObject to produce JSON. The server I'm working with has some specifications for sorting data results (the fact that this is related to sorting data has nothing to do with my actual question about sorting). The thing is, the server expects these values to be listed in order of how to sort, so for example...</p> <pre> "sort": { "first_sort_field": 1, "second_sort_field": 1, "third_sort_field": -1, "fourth_sort_field": 1 } </pre> <p>1 means ascending and -1 means descending. But that's not the important part. What's important is that these values in the <code>sort</code> object must be organized in this manner.</p> <p>To produce this object, I'm doing this:</p> <pre><code>var O, O2: ISuperObject; X: Integer; //more begin O:= SO; //main object //more O2:= SO; //sub object for X := 0 to FSort.Count - 1 do begin case FSort[X].Direction of sdAscending: O2.I[FSort[X].FieldName]:= 1; sdDescending: O2.I[FSort[X].FieldName]:= -1; end; end; O.O['sort']:= O2; //more end; </code></pre> <p>The problem arises when I use SuperObject to serialize this "sort" object. The values seem to be re-arranged, so for example the JSON above would actually come out something like this:</p> <pre> "sort": { "first_sort_field": 1, "fourth_sort_field": 1 "second_sort_field": 1, "third_sort_field": -1, } </pre> <p>Which is a different order than I intended. This causes the server to return the response data sorted in a different manner than intended.</p> <p>The question is, how can I make SuperObject serialize the data in the order which I added it rather than its own order? I thought that it might be sorting the values in ABC order, but when combining different types of values (string, integer, object, array, etc.) they're not in ABC order. I'd like to force SuperObject to serialize the data in the order which I added it.</p> <p>The only solution I can see is to serialize this object manually by concatenating strings. But I'd like to avoid that if at all possible - that's why I'm using SuperObject in the first place.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload