mormot2 IDocList&IDocDict
mormot2 IDocList&IDocDict
uses mormot.core.variants
mormot2模仿Python Lists and Dictionaries JSON操作,封装了IDocList&IDocDict俩个新的接口。
IDocList (别名IDocArray) 存储a list of elements;
IDocDict (别名IDocObject) 存储a dictionary of key:value pairs.
例程一
var list: IDocList; dict: IDocDict; v: variant; i: integer; begin // creating a new list/array from items list := DocList([1, 2, 3, 'four', 1.0594631]); // double are allowed by default // iterating over the list for v in list do Listbox1.Items.Add(v); // convert from variant to string // or a sub-range of the list (with Python-like negative indexes) for i in list.Range(0, -3) do Listbox2.Items.Add(IntToStr(i)); // [1, 2] as integer // search for the existence of some elements assert(list.Exists(2)); assert(list.Exists('four')); // a list of objects, from JSON, with an intruder list := DocList('[{"a":0,"b":20},{"a":1,"b":21},"to be ignored",{"a":2,"b":22}]'); // enumerate all objects/dictionaries, ignoring non-objects elements for dict in list.Objects do begin if dict.Exists('b') then ListBox2.Items.Add(dict['b']); if dict.Get('a', i) then ListBox3.Items.Add(IntToStr(i)); end; // delete one element list.Del(1); assert(list.Json = '[{"a":0,"b":20},"to be ignored",{"a":2,"b":22}]'); // extract one element if list.PopItem(v, 1) then assert(v = 'to be ignored'); // convert to a JSON string Label1.Caption := list.ToString; // display '[{"a":0,"b":20},{"a":2,"b":22}]' end;
例程二:
var v: variant; f: TDocDictFields; list, list2: IDocList; dict: IDocDict; begin list := DocList('[{"a":10,"b":20},{"a":1,"b":21},{"a":11,"b":20}]'); // sort a list/array by the nested objects field(s) list.SortByKeyValue(['b', 'a']); assert(list.Json = '[{"a":10,"b":20},{"a":11,"b":20},{"a":1,"b":21}]'); // enumerate a list/array with a conditional expression for dict in list.Objects('b<21') do assert(dict.I['b'] < 21); // another enumeration with a variable as conditional expression for dict in list.Objects('a=', 10) do assert(dict.I['a'] = 10); // create a new IDocList from a conditional expression list2 := list.Filter('b =', 20); assert(list2.Json = '[{"a":10,"b":20},{"a":11,"b":20}]'); // direct access to the internal TDocVariantData storage assert(list.Value^.Count = 3); assert(list.Value^.Kind = dvArray); assert(dict.Value^.Kind = dvObject); // TDocVariantData from a variant intermediary v := list.AsVariant; assert(_Safe(v)^.Count = 3); v := dict.AsVariant; assert(_Safe(v)^.Count = 2); // high-level Python-like methods if list.Len > 0 then while list.PopItem(v) do begin assert(list.Count(v) = 0); // count the number of appearances assert(not list.Exists(v)); Listbox1.Items.Add(v.a); // late binding dict := DocDictFrom(v); // transtyping from variant to IDocDict assert(dict.Exists('a') and dict.Exists('b')); // enumerate the key:value elements of this dictionary for f in dict do begin Listbox2.Items.Add(f.Key); Listbox3.Items.Add(f.Value); end; end; // create from any complex "compact" JSON // (note the key names are not "quoted") list := DocList('[{ab:1,cd:{ef:"two"}}]'); // we still have the late binding magic working assert(list[0].ab = 1); assert(list[0].cd.ef = 'two'); // create a dictionary from key:value pairs supplied from code dict := DocDict(['one', 1, 'two', 2, 'three', _Arr([5, 6, 7, 'huit'])]); assert(dict.Len = 3); // one dictionary with 3 elements assert(dict.Json = '{"one":1,"two":2,"three":[5,6,7,"huit"]}'); // convert to JSON with nice formatting (line feeds and spaces) Memo1.Caption := dic.ToString(jsonHumanReadable); // sort by key names dict.Sort; assert(dict.Json = '{"one":1,"three":[5,6,7,"huit"],"two":2}'); // note that it will ensure faster O(log(n)) key lookup after Sort: // (beneficial for performance on objects with a high number of keys) assert(dict['two'] = 2); // default lookup as variant value assert(dict.I['two'] = 2); // explicit conversion to integer end;
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/18310253