cesanta/frozen

json_scanf: only first object gets parsed

Rio6 opened this issue · 1 comments

Rio6 commented

I'm trying to parse a json string looks like this

{
    "a": { "a": 1 },
    "b": { "b": 2 }
}

The code I used follows

    char buff[] = "{\"a\":{\"a\":1},\"b\":{\"b\":2}}";
    int len = sizeof(buff);

    int a = 0, b = 0;
    json_scanf(buff, len, "{a:{a:%d},b:{b:%d}}", &a, &b);
    printf("a %d b %d\n", a, b);

outputs

a 1 b 0

Only a got parsed. Any idea on how to also parse b?

I have the same issue. I can write my data out like this:

	json_fprintf(_configLoaded,
		"{"
		"background:{ red:%f, green:%f, blue:%f },"
		"content:{ x:%d, y:%d, w:%d, h:%d },"
		"window:{ x:%d, y:%d, w:%d, h:%d },"
		"lastProject:%Q, notes:%Q, showNotepad:%B, style:%d, version:%d"
		"}",
		__config.background.red, __config.background.green, __config.background.blue,
		__config.content.x, __config.content.y, __config.content.w, __config.content.h,
		__config.window.x, __config.window.y, __config.window.w, __config.window.h,
		__config.lastProject, __config.notes.text, __config.showNotepad, __config.style, __config.version
	);

But reading requires:

		json_scanf(buffer, strlen(buffer), "{ background:{ red:%f, green:%f, blue:%f } }", &__config.background.red, &__config.background.green, &__config.background.blue);
		json_scanf(buffer, strlen(buffer), "{ content:{ x:%d, y:%d, w:%d, h:%d } }", &__config.content.x, &__config.content.y, &__config.content.w, &__config.content.h);
		json_scanf(buffer, strlen(buffer), "{ window:{ x:%d, y:%d, w:%d, h:%d } }", &__config.window.x, &__config.window.y, &__config.window.w, &__config.window.h);
		json_scanf(buffer, strlen(buffer), "{ lastProject:%Q, notes:%Q, showNotepad:%B, style:%d, version:%d }", &__config.lastProject, &__config.notes.text, &__config.showNotepad, &__config.style, &__config.version);

Otherwise, only "background" is filled in.