Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Patch clears original data #1090

Closed
soundprojects opened this issue May 15, 2018 · 3 comments
Closed

Merge Patch clears original data #1090

soundprojects opened this issue May 15, 2018 · 3 comments
Labels
kind: bug solution: invalid the issue is not related to the library

Comments

@soundprojects
Copy link

soundprojects commented May 15, 2018

  • What is the issue you have?
    I have two objects which I want to merge using the merge_patch() function. An example of my objects:

A {c : [{a : "1", b: "2"}], d: "5"}

B{c: [{a: "2"}]}

result:

C{c:[{a:"2"}], d:"5"}

When merging B into A, the library seems to delete value A{c[{b}]}.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

  • What is the expected behavior?
    I was expecting for all the original values to remain because they are not defined in the patch object

  • And what is the actual behavior instead?
    The original values that are not defined are removed

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?
    latest version

  • If you experience a compilation error: can you compile and run the unit tests?

@soundprojects
Copy link
Author

soundprojects commented May 15, 2018

Small Example:




	json j1 = {
	{ "name", "Niels" },
	{ "object",{ "currency", "USD" },{ "value", 42.99 }}
	};

	json j2 = {
	{ "object",{ "currency", "EUR" }}
	};

	j1.merge_patch(j2);

	std::cout << j1.dump() << std::endl;`


result:
{"object":["currency","EUR"]}

@pboettch
Copy link
Contributor

{ "object",{ "currency", "USD" },{ "value", 42.99 }} is giving you an array of objects indexed by the key object. Hence when you merge-patch it with another array: { "currency", "EUR" } it gets replaced.

There is no bug here, you need an extra {} around the elements for object to get nested objects.

You can easily see it in your output: the brackets [] around "currency" and "EUR" are indication an array not an object.

@soundprojects
Copy link
Author

Right! Thank you.

So just to sum things up: If you want to merge two objects, keep in account that everything that is constructed as an array will be replaced when setting a different array.

To make things work if you don't want this: change your array to std::map.

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label May 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

3 participants