Skip to content

Commit 7ce74f4

Browse files
#1527 Fix JSON parsing issue when skipping missing component
1 parent c79a34c commit 7ce74f4

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

distr/flecs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41069,6 +41069,8 @@ const char* flecs_json_deser_components(
4106941069
goto error;
4107041070
}
4107141071

41072+
json = flecs_parse_ws_eol(json);
41073+
4107241074
json = flecs_json_skip_object(json + 1, token, desc);
4107341075
if (!json) {
4107441076
goto error;
@@ -41088,6 +41090,8 @@ const char* flecs_json_deser_components(
4108841090
goto error;
4108941091
}
4109041092

41093+
json = flecs_parse_ws_eol(json);
41094+
4109141095
json = flecs_json_skip_object(json + 1, token, desc);
4109241096
if (!json) {
4109341097
goto error;

src/addons/json/deserialize.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ const char* flecs_json_deser_components(
406406
goto error;
407407
}
408408

409+
json = flecs_parse_ws_eol(json);
410+
409411
json = flecs_json_skip_object(json + 1, token, desc);
410412
if (!json) {
411413
goto error;
@@ -425,6 +427,8 @@ const char* flecs_json_deser_components(
425427
goto error;
426428
}
427429

430+
json = flecs_parse_ws_eol(json);
431+
428432
json = flecs_json_skip_object(json + 1, token, desc);
429433
if (!json) {
430434
goto error;

test/meta/project.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,11 @@
632632
"ser_deser_named_child_to_different_table",
633633
"ser_deser_with_child_tgt",
634634
"ser_deser_with_child_tgt_no_child",
635-
"deser_invalid_entity_name"
635+
"deser_invalid_entity_name",
636+
"deser_unknown_component_w_spaces",
637+
"deser_unknown_component_no_spaces",
638+
"deser_unknown_component_w_spaces_strict",
639+
"deser_unknown_component_no_spaces_strict"
636640
]
637641
}, {
638642
"id": "SerializeToJson",

test/meta/src/DeserializeFromJson.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6126,3 +6126,113 @@ void DeserializeFromJson_ser_deser_with_child_tgt_no_child(void) {
61266126

61276127
ecs_fini(world);
61286128
}
6129+
6130+
void DeserializeFromJson_deser_unknown_component_w_spaces(void) {
6131+
ecs_world_t *world = ecs_init();
6132+
6133+
ECS_COMPONENT(world, Position);
6134+
6135+
ecs_struct(world, {
6136+
.entity = ecs_id(Position),
6137+
.members = {
6138+
{"x", ecs_id(ecs_i32_t)},
6139+
{"y", ecs_id(ecs_i32_t)},
6140+
}
6141+
});
6142+
6143+
const char *r = ecs_world_from_json(world,
6144+
"{\"results\":[{\"name\":\"e\", \"components\":{\"Position\": {\"x\": 10, \"y\": 20}, \"DoesntExist\": {\"pos\":{\"x\":20, \"y\":30}}}}]}", NULL);
6145+
test_assert(r != NULL);
6146+
test_assert(r[0] == 0);
6147+
6148+
ecs_entity_t e = ecs_lookup(world, "e");
6149+
test_assert(e != 0);
6150+
6151+
const Position *p = ecs_get(world, e, Position);
6152+
test_assert(p != NULL);
6153+
test_int(p->x, 10);
6154+
test_int(p->y, 20);
6155+
6156+
ecs_fini(world);
6157+
}
6158+
6159+
void DeserializeFromJson_deser_unknown_component_no_spaces(void) {
6160+
ecs_world_t *world = ecs_init();
6161+
6162+
ECS_COMPONENT(world, Position);
6163+
6164+
ecs_struct(world, {
6165+
.entity = ecs_id(Position),
6166+
.members = {
6167+
{"x", ecs_id(ecs_i32_t)},
6168+
{"y", ecs_id(ecs_i32_t)},
6169+
}
6170+
});
6171+
6172+
const char *r = ecs_world_from_json(world,
6173+
"{\"results\":[{\"name\":\"e\", \"components\":{\"Position\":{\"x\": 10, \"y\": 20}, \"DoesntExist\":{\"pos\":{\"x\":20, \"y\":30}}}}]}", NULL);
6174+
test_assert(r != NULL);
6175+
test_assert(r[0] == 0);
6176+
6177+
ecs_entity_t e = ecs_lookup(world, "e");
6178+
test_assert(e != 0);
6179+
6180+
const Position *p = ecs_get(world, e, Position);
6181+
test_assert(p != NULL);
6182+
test_int(p->x, 10);
6183+
test_int(p->y, 20);
6184+
6185+
ecs_fini(world);
6186+
}
6187+
6188+
void DeserializeFromJson_deser_unknown_component_w_spaces_strict(void) {
6189+
ecs_world_t *world = ecs_init();
6190+
6191+
ECS_COMPONENT(world, Position);
6192+
6193+
ecs_struct(world, {
6194+
.entity = ecs_id(Position),
6195+
.members = {
6196+
{"x", ecs_id(ecs_i32_t)},
6197+
{"y", ecs_id(ecs_i32_t)},
6198+
}
6199+
});
6200+
6201+
ecs_from_json_desc_t desc = {
6202+
.strict = true
6203+
};
6204+
6205+
ecs_log_set_level(-4);
6206+
6207+
const char *r = ecs_world_from_json(world,
6208+
"{\"results\":[{\"name\":\"e\", \"components\":{\"Position\": {\"x\": 10, \"y\": 20}, \"DoesntExist\": {\"pos\":{\"x\":20, \"y\":30}}}}]}", &desc);
6209+
test_assert(r == NULL);
6210+
6211+
ecs_fini(world);
6212+
}
6213+
6214+
void DeserializeFromJson_deser_unknown_component_no_spaces_strict(void) {
6215+
ecs_world_t *world = ecs_init();
6216+
6217+
ECS_COMPONENT(world, Position);
6218+
6219+
ecs_struct(world, {
6220+
.entity = ecs_id(Position),
6221+
.members = {
6222+
{"x", ecs_id(ecs_i32_t)},
6223+
{"y", ecs_id(ecs_i32_t)},
6224+
}
6225+
});
6226+
6227+
ecs_from_json_desc_t desc = {
6228+
.strict = true
6229+
};
6230+
6231+
ecs_log_set_level(-4);
6232+
6233+
const char *r = ecs_world_from_json(world,
6234+
"{\"results\":[{\"name\":\"e\", \"components\":{\"Position\":{\"x\": 10, \"y\": 20}, \"DoesntExist\":{\"pos\":{\"x\":20, \"y\":30}}}}]}", &desc);
6235+
test_assert(r == NULL);
6236+
6237+
ecs_fini(world);
6238+
}

test/meta/src/main.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ void DeserializeFromJson_ser_deser_named_child_to_different_table(void);
606606
void DeserializeFromJson_ser_deser_with_child_tgt(void);
607607
void DeserializeFromJson_ser_deser_with_child_tgt_no_child(void);
608608
void DeserializeFromJson_deser_invalid_entity_name(void);
609+
void DeserializeFromJson_deser_unknown_component_w_spaces(void);
610+
void DeserializeFromJson_deser_unknown_component_no_spaces(void);
611+
void DeserializeFromJson_deser_unknown_component_w_spaces_strict(void);
612+
void DeserializeFromJson_deser_unknown_component_no_spaces_strict(void);
609613

610614
// Testsuite 'SerializeToJson'
611615
void SerializeToJson_struct_bool(void);
@@ -3362,6 +3366,22 @@ bake_test_case DeserializeFromJson_testcases[] = {
33623366
{
33633367
"deser_invalid_entity_name",
33643368
DeserializeFromJson_deser_invalid_entity_name
3369+
},
3370+
{
3371+
"deser_unknown_component_w_spaces",
3372+
DeserializeFromJson_deser_unknown_component_w_spaces
3373+
},
3374+
{
3375+
"deser_unknown_component_no_spaces",
3376+
DeserializeFromJson_deser_unknown_component_no_spaces
3377+
},
3378+
{
3379+
"deser_unknown_component_w_spaces_strict",
3380+
DeserializeFromJson_deser_unknown_component_w_spaces_strict
3381+
},
3382+
{
3383+
"deser_unknown_component_no_spaces_strict",
3384+
DeserializeFromJson_deser_unknown_component_no_spaces_strict
33653385
}
33663386
};
33673387

@@ -5091,7 +5111,7 @@ static bake_test_suite suites[] = {
50915111
"DeserializeFromJson",
50925112
NULL,
50935113
NULL,
5094-
134,
5114+
138,
50955115
DeserializeFromJson_testcases
50965116
},
50975117
{

0 commit comments

Comments
 (0)