Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion json_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def unflatten(data):
current = current[bit]
# Now deal with $type suffixes:
if _types_re.match(lastkey):
lastkey, lasttype = lastkey.rsplit("$", 2)
# rsplit on "$" once so a key that itself contains "$" (e.g.
# "foo$bar") round-trips intact: "foo$bar$int" -> ("foo$bar",
# "int") rather than crashing on a 3-element unpack.
lastkey, lasttype = lastkey.rsplit("$", 1)
value = {
"int": int,
"float": float,
Expand Down
9 changes: 9 additions & 0 deletions test_json_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
"foo.[0].phones._$!<home>!$_": "555-555-5555",
},
),
# A key that contains "$" and has a typed value used to crash
# unflatten with `ValueError: too many values to unpack` because the
# type-suffix split was greedy and consumed the whole key.
("dollar_sign_in_key_with_typed_value", {"foo$bar": 5}, {"foo$bar$int": "5"}),
(
"multiple_dollar_signs_in_key_with_typed_value",
{"_$home$_": True},
{"_$home$_$bool": "True"},
),
("empty_object", {}, {"$empty": "{}"}),
(
"nested_empty_objects",
Expand Down