#1 Re: mORMot 1 » Sorting content in Variants & TDocVariant ? » 2015-02-15 10:48:47

RIL

Ah, the wrapper makes sense. It would be useful to add this info to the documentation on this page: http://synopse.info/files/html/Synopse% … ml#TITL_80

The examples are very good, but finding out how to add data as Pairs, Objects and Arrays (both variants and strings added to existing variants) took much reading and trying (tiiime) for a newbie. smile

#2 Re: mORMot 1 » Sorting content in Variants & TDocVariant ? » 2015-02-15 09:07:34

RIL
ab wrote:

I've added the TDocVariantData.SortByName method.

Would something similar be possible also for TDocVariant? (All my existing code uses TDocVariant). I'm a bit lost on these variants still. smile

Btw, it seems like the "Exchg" procedure was redeclared (patching only this "SortByName", "Compare" & "Exchg" won't compile).

// Rolf Lampa

#3 Re: mORMot 1 » Sorting content in Variants & TDocVariant ? » 2015-02-15 08:23:36

RIL
ab wrote:

I've added the TDocVariantData.SortByName method.
See http://synopse.info/fossil/info/b38f4de17d

Your v2 is not a valid JSON content.  I suppose there are some [   ]  missing to make it a JSON array.

Thank you very much for the SortByName!

And yes, the brackets were missing:

v2 := _Json( '[{ "B":"Bee"},{"C":"Sii"},{"A":"Ei"}]' );

I actually collect my lists (via RTTI) one by one, returning them as string, and then add them as JSON arrays to the owning object, like so:

    vMain, vTV: Variant;
begin
    // Retrieving RTTI Context etc.
    ...
    // Add an empty Array
    vMain.Add('TaggedValues', _Arr([]));

    props := c.GetIndexedProperty('TaggedValues');
    Cnt := c.GetProperty('TaggedValuesCount').GetValue(aInstance).AsInteger;
    for i := 0 to Cnt - 1 do
    begin
        Obj := props.GetValue(aInstance, [i]).AsObject;
        vTV := _Json( GetTaggedValuesAsJsonStr(Obj) ); // '[{"name":"val"},{...}]'
        // 
        // vTV.SortArray; // <-- Sort before nesting the list into vMain
        // 
        vMain._('TaggedValues').Add( vTV );
    end;

By producing consistent locations of objects I will be able to track any changes in model info (using diff), and for this reason I happily "take the pain" to handle each list/array individually in order to be able to sort them.

// Rolf Lampa

#4 mORMot 1 » Sorting content in Variants & TDocVariant ? » 2015-02-15 05:33:46

RIL
Replies: 6

I'm using JSON format for exporting / importing UML model info (from EA) and for file-diff purposes I'd like to be able to sort JSON pairs and arrays (I can then test-compare Export.txt with Import.txt, for example).

But I'm not experienced with the SynCommon.pas unit and have a problem finding out how to sort the two examples below :

    v, v2: Variant;
begin
    TDocVariant.New(v);
    TDocVariant.New(v2);

    v := _Json( '{ "B":"Bee","C":"Sii","A":"Ei" }' );
    // How do I sort v in order to get 
    //          '{ "A":"Ei","B":"Bee","C":"Sii" }' ?

    v2 := _Json( '{ "B":"Bee"},{"C":"Sii"},{"A":"Ei"}' );
    // How do I sort v2 in order to get :
    //          '{"A":"Ei"},{"B":"Bee"},{"C":"Sii"}' ?

I have tried dirty hacks like copying the content to TStrings, but I keep loosing data (and with big models such hacks will not perform).

What would be the fastest and simplest way to sort by name, ascending?

// Rolf Lampa

Board footer

Powered by FluxBB