-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add colno/end_colno and end_linno properties to JSON5Token. restructu…
…re JSONObject to store keys and values separately This change removes `KeyValuePair` as a Node object. Instead, the underlying data model will store a list of keys and list of values in separate attributes. A new attribute for `JSONOBject` `key_value_pairs` which will be a named tuple (typing.NamedTuple) that provides the key and value attributes. This change brings more congruity with how the stdlib `ast` module handles Python dictionaries, to which Python users may be more accustomed. It also maintains, by interface, the same structure for JSON5 members (https://spec.json5.org/#prod-JSON5MemberList) with the new key_value_pairs attribute (although it itself is not a Node).
- Loading branch information
1 parent
d11ae38
commit 52164f2
Showing
6 changed files
with
110 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import ast | ||
|
||
import pytest | ||
|
||
import json5.loader | ||
import json5.model | ||
|
||
TEST_TEXT = '''\ | ||
{ | ||
"string_on_same_line": "string on same line", | ||
"multiline_dq_string": "this line has a \ | ||
continuation", | ||
"leadingDecimalPoint": .8675309 , | ||
"andTrailing": 8675309., | ||
"trailingComma": 'in objects', | ||
"backwardsCompatible": "with JSON", | ||
} | ||
''' | ||
|
||
model = json5.loads(TEST_TEXT, loader=json5.loader.ModelLoader()) | ||
tree = ast.parse(TEST_TEXT) | ||
ast_nodes = [ | ||
node for node in list(ast.walk(tree)) if not isinstance(node, (ast.Expr, ast.Load, ast.Module, ast.UnaryOp)) | ||
] | ||
json5_nodes = [ | ||
node | ||
for node in list(json5.model.walk(model)) | ||
if not isinstance(node, (json5.model.TrailingComma, json5.model.JSONText)) | ||
] | ||
|
||
assert len(ast_nodes) == len(json5_nodes) | ||
|
||
|
||
@pytest.mark.parametrize('ast_node, json5_node', list(zip(ast_nodes, json5_nodes))) | ||
@pytest.mark.parametrize( | ||
'attr_name', | ||
[ | ||
'col_offset', | ||
'end_col_offset', | ||
'lineno', | ||
'end_lineno', | ||
], | ||
) | ||
def test_node_attribute_accuracy(attr_name: str, ast_node, json5_node): | ||
assert getattr(json5_node, attr_name) == getattr( | ||
ast_node, attr_name | ||
), f'{attr_name} did not match {ast_node!r}, {json5_node!r}' |