Repository navigation
Add create parameter to set_pointer for auto-creating missing paths - #84
stefankoegl wants to merge 1 commit into
Conversation
With create=True, set_pointer and JsonPointer.set create missing intermediate members as dicts instead of raising JsonPointerException. Missing list elements can be created by referencing the end of a list with "-" or an index equal to its length. The missing structure is built detached and attached in one step, so doc is left unchanged if the pointer can't be applied. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGUKSut5p4NSKEm3mD5Ft3
There was a problem hiding this comment.
Ten standalone creation checks passed for escaped keys, empty keys, numeric/“-” dictionary keys, list-end append forms, inplace=False, rejected gaps/leading-zero/negative indices, and unchanged documents after invalid paths. I used Python 3.12.14 against commit ead884a.
One error-consistency edge case outside ordinary dict/list JSON is an immutable Sequence:
set_pointer({"items": ()}, "/items/0/leaf", 7, create=True)
# AttributeError: tuple has no attribute appendThe new index==len branch maps 0 to “-”, and _set_child calls append without checking mutability or wrapping that failure. /items/0 behaves the same. Could immutable append targets raise JsonPointerException consistently with other invalid assignment targets? Both tuple reproductions left the document unchanged. I used fetched PR source with standalone assertions, not the full test suite.
Summary
This PR adds a
createparameter to theset_pointer()function andJsonPointer.set()method that enables automatic creation of missing intermediate members when setting values in nested structures.Key Changes
create=Falseparameter toset_pointer()function andJsonPointer.set()methodcreate=True, missing intermediate dictionary members are automatically created instead of raising aJsonPointerException"-"or an index equal to the array lengthset()method to extract helper methods:_get_create_part(): Maps array indices at the end of a sequence to"-"for appending_is_missing(): Checks if a part doesn't exist but could be added_set_child(): Extracted common logic for setting values in parent containerscreateparameter usageImplementation Details
create=Falseas the defaultinplace=Trueandinplace=Falsemodeshttps://claude.ai/code/session_01WGUKSut5p4NSKEm3mD5Ft3