#1 2024-11-19 12:25:28

tfopc
Member
Registered: 2024-01-08
Posts: 11

IDocDict.Update - nested objects are overriden - no merge on childs?

Hi,
I thought I could update the JSON object with an object that only contains part of the values. Unfortunately, the child objects are overwritten with the update object child element and the existing elements are lost.

Sample:
Json to update:

d1 := DocDict('
{
	"name": "Mustermann",
	"address": {
		"city": "Musterstadt",
		"street": "Lindenallee"
	}
}');

Json Update input.

d2 := DocDict('{
	"surname": "Max",
	"address": {
		"postal_code": "12345"
	}
}');
d1.Update(d2);

Output:

{
    "name": "Mustermann",
    "address": {
        "postal_code": "12345"
    },
    "surname": "Max"
}

Expected:

{
    "name": "Mustermann",
    "address": {
        "city": "Musterstadt",
        "street": "Lindenallee"
        "postal_code": "12345"
    },
    "surname": "Max"
}

I have also tried "mDefault" as Model instead of mFastFloat.

Any chance to merge the json on all levels?

Thanks,
Tobias

Offline

#2 2024-11-19 13:54:21

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,654
Website

Re: IDocDict.Update - nested objects are overriden - no merge on childs?

Update() does actually ... update ... the IDocDict properties.
As expected.

What you want is more like a Merge() method.

Online

#3 2024-11-19 14:31:48

tfopc
Member
Registered: 2024-01-08
Posts: 11

Re: IDocDict.Update - nested objects are overriden - no merge on childs?

Hi Arnaud,

yes i want a merge.
Looking for "merge" in mormot.core.variants points me to:

    /// set a value, given its path
    // - path is defined as a dotted name-space, e.g. 'doc.glossary.title'
    // - aCreateIfNotExisting=true will force missing nested objects creation
    // - aMergeExisting=true will merge aValue object with any existing object
    // - returns nil if there is no item to be set at the supplied aPath
    // - returns the address of the found or created value in aValue
    // - you can set e.g. aPathDelim = '/' to search e.g. for 'parent/child'
    function SetValueByPath(const aPath: RawUtf8; const aValue: variant;
      aCreateIfNotExisting: boolean = false; aPathDelim: AnsiChar = '.';
      aMergeExisting: boolean = false): PVariant;

because of "aMergeExisting=true will merge aValue object with any existing object"

is this an option? - I tried it, but I got an exception. Also i don't realize how to "Set" the root Path.

Thank you.

Offline

#4 Yesterday 12:50:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,654
Website

Re: IDocDict.Update - nested objects are overriden - no merge on childs?

Please try with
https://github.com/synopse/mORMot2/commit/5dd229b1

I have added a new Merge() method to IDocDict.

Online

#5 Yesterday 13:39:02

tfopc
Member
Registered: 2024-01-08
Posts: 11

Re: IDocDict.Update - nested objects are overriden - no merge on childs?

Hi Arnaud,

perfect. Thank you very much.
So i was in the right direction.

Tested and verified!

Tobias

Offline

Board footer

Powered by FluxBB