Skip to content

json_object_merge does not merge #1

Open
@ghost

Description

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "json.h"
#include "json-builder.h"

#define R(x...) #x

int main()
{
    const char *json1 = R({"x": 1, "y": 2});
    const char *json2 = R({"y": 3, "z": 4});

    char error[128];
    json_settings settings = {};
    settings.value_extra = json_builder_extra;
    json_value *obj1 = json_parse_ex(&settings, json1, strlen(json1), error);
    json_value *obj2 = json_parse_ex(&settings, json2, strlen(json1), error);

    json_object_merge(obj1, obj2);

    char *buf = malloc(json_measure(obj1));
    json_serialize(buf, obj1);

    printf("buf: `%s'\n", buf);
    return 0;
}
buf: `, "z": 4{ "y": 3, "z": 4 }'

That's odd, isn't it? I think, I was supposed to get {"x": 1, "y": 3, "z": 4"} here. Maybe the function should be rewritten completely using the recursive scheme. I suppose, it must be good to have an ability to merge e.g.

{"arr": [{"id": "1", "data": "A"}, {"id": "2", "data": "B"}]} <+>
{"arr": [{"id": "1", "extra": "@"}, {"id": "2", "data": "Z"}, {"id": "C"}]} ==
{"arr": [{"id": "1", "data": "A", "extra": "@"}, {"id": "2", "data": "Z"}, {"id": "3"}]}

I'll try to take a look on the sources in a couple of days and figure it out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions