Skip to content

Commit 727927b

Browse files
stewegrjarry
authored andcommitted
data/context: adds json_null parsing option
This patch adds options to support JSON 'null' values Signed-off-by: Stefan Gula <[email protected]>
1 parent 407fab3 commit 727927b

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

cffi/cdefs.h

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ LY_ERR lyd_print_all(struct ly_out *, const struct lyd_node *, LYD_FORMAT, uint3
315315
#define LYD_PARSE_LYB_MOD_UPDATE ...
316316
#define LYD_PARSE_NO_STATE ...
317317
#define LYD_PARSE_STORE_ONLY ...
318+
#define LYD_PARSE_JSON_NULL ...
318319
#define LYD_PARSE_ONLY ...
319320
#define LYD_PARSE_OPAQ ...
320321
#define LYD_PARSE_OPTS_MASK ...

libyang/context.py

+6
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ def parse_data(
520520
validate_present: bool = False,
521521
validate_multi_error: bool = False,
522522
store_only: bool = False,
523+
json_null: bool = False,
523524
) -> Optional[DNode]:
524525
if self.cdata is None:
525526
raise RuntimeError("context already destroyed")
@@ -531,6 +532,7 @@ def parse_data(
531532
ordered=ordered,
532533
strict=strict,
533534
store_only=store_only,
535+
json_null=json_null,
534536
)
535537
validation_flgs = validation_flags(
536538
no_state=no_state,
@@ -589,6 +591,7 @@ def parse_data_mem(
589591
validate_present: bool = False,
590592
validate_multi_error: bool = False,
591593
store_only: bool = False,
594+
json_null: bool = False,
592595
) -> Optional[DNode]:
593596
return self.parse_data(
594597
fmt,
@@ -604,6 +607,7 @@ def parse_data_mem(
604607
validate_present=validate_present,
605608
validate_multi_error=validate_multi_error,
606609
store_only=store_only,
610+
json_null=json_null,
607611
)
608612

609613
def parse_data_file(
@@ -620,6 +624,7 @@ def parse_data_file(
620624
validate_present: bool = False,
621625
validate_multi_error: bool = False,
622626
store_only: bool = False,
627+
json_null: bool = False,
623628
) -> Optional[DNode]:
624629
return self.parse_data(
625630
fmt,
@@ -635,6 +640,7 @@ def parse_data_file(
635640
validate_present=validate_present,
636641
validate_multi_error=validate_multi_error,
637642
store_only=store_only,
643+
json_null=json_null,
638644
)
639645

640646
def __iter__(self) -> Iterator[Module]:

libyang/data.py

+3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def parser_flags(
116116
ordered: bool = False,
117117
strict: bool = False,
118118
store_only: bool = False,
119+
json_null: bool = False,
119120
) -> int:
120121
flags = 0
121122
if lyb_mod_update:
@@ -132,6 +133,8 @@ def parser_flags(
132133
flags |= lib.LYD_PARSE_STRICT
133134
if store_only:
134135
flags |= lib.LYD_PARSE_STORE_ONLY
136+
if json_null:
137+
flags |= lib.LYD_PARSE_JSON_NULL
135138
return flags
136139

137140

tests/test_data.py

+6
Original file line numberDiff line numberDiff line change
@@ -1131,3 +1131,9 @@ def test_merge_builtin_plugins_only(self):
11311131
self.assertIsInstance(dnode, DLeaf)
11321132
self.assertEqual(dnode.value(), "test")
11331133
dnode.free()
1134+
1135+
def test_dnode_parse_json_null(self):
1136+
JSON = """{"yolo-nodetypes:ip-address": null}"""
1137+
dnode = self.ctx.parse_data_mem(JSON, "json", json_null=True)
1138+
dnode_names = [d.name() for d in dnode.siblings()]
1139+
self.assertFalse("ip-address" in dnode_names)

0 commit comments

Comments
 (0)