You are not logged in.
Hi ab,
i played around with the new yaml parser (thanks, zen010101 and ab for the addition)
i try to do some helper to migrate yaml/json -> ini-files and back.
For saving json/yaml in the ini need to flatten the nested objects...
So i catched this ...
Doc.InitJson('{"a":{"b":1,"c":1},d:3}');
Doc.FlattenFromNestedObjects('.');
CheckEqual(Doc.ToJson, '{"a.b":1,"a.c":1,"d":3}');I assume that “FlattenFromNestedObjects” completely flattens the nested structures.
So that this test would succeed.
Doc.InitJson('{"a":{"b":{"bb":2},"c":1},d:3}');
Doc.FlattenFromNestedObjects('.');
CheckEqual(Doc.ToJson, '{"a.b.bb":2,"a.c":1,"d":3}');But at the moment it only nesting the root - level:
{"a":{"b":{"bb":2},"c":1},d:3} -> {"a.b":{"bb":2},"a.c":1,"d":3}
So this test also fails.
Doc.InitJson('{"a":{"b":{"bb":2},"c":1},d:3}');
Doc.FlattenFromNestedObjects('.');
CheckNotEqual(Doc.ToJson, '{"a.b":{"bb":2},"a.c":1,"d":3}');Is this expected?
Thank you.
Tobias
Offline
Yes, it only flatten the root level.
It was on purpose, because it was designed and used in a specific case of a generic single level key/value property dictionary context.
For INI, you don't need to 'flatten' the nested objects.
You could use dedicated sections per object.
This is what mORMot IniToObject() does - and TSynJsonFileSettings.
Offline
Hi, ab
a simple "FlattenFromNestedObjects" defined like this, will do the job
function TDocVariantData.FlattenFullFromNestedObjects(aSepChar: AnsiChar = '.';
aNestedArrayStartIndex: PtrInt = -1): boolean;
begin
result := FlattenFromNestedObjects(aSepChar,aNestedArrayStartIndex);
if result then
while FlattenFromNestedObjects(aSepChar,aNestedArrayStartIndex) do;
end;Use case is for example to save a nested json as csv.
Maybe it can also be integrated in FlattenFromNestedObjects via an additional optional param (flattenfull:boolean=false)
Feel free to integrate it.
Of course a simple "while doc.FlattenFromNestedObjects do;" also do the job in application code.
Offline
Documented as such:
https://github.com/synopse/mORMot2/commit/13cd97f66
Offline