Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/objdictgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from objdictgen.node import Node
from objdictgen.nodemanager import NodeManager

__version__ = "3.5"
__version__ = "3.5.1a1"

# Shortcuts
LoadFile = Node.LoadFile
Expand Down
5 changes: 3 additions & 2 deletions src/objdictgen/jsonod.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def rearrage_for_json(obj: TODObjJson, node: "Node", objtypes_i2s: dict[int, str

# Add __type when rich format
if info and "type" not in sub:
sub["__type"] = objtypes_i2s.get(info["type"], info["type"])
sub["__type"] = f'@@"{objtypes_i2s.get(info["type"], info["type"])}" // {info["type"]}@@'

# Replace value
if rich and "value" in sub:
Expand All @@ -744,7 +744,8 @@ def rearrage_for_json(obj: TODObjJson, node: "Node", objtypes_i2s: dict[int, str
# Replace numeric type with string value
if rich and "type" in each:
# FIXME: The cast is to ensure mypy is able keep track
each["type"] = objtypes_i2s.get(cast(int, each["type"]), each["type"])
n = objtypes_i2s.get(cast(int, each["type"]), each["type"])
each["type"] = f'@@"{n}" // {each["type"]}@@'

# Rearrage order of 'sub' and 'each'
obj["sub"] = [
Expand Down
54 changes: 54 additions & 0 deletions tests/od/jsonod-comments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"$id": "od data",
"$version": "1",
"$description": "Canfestival object dictionary data",
"$tool": "odg 3.5",
"$date": "2024-08-13T11:36:24.609168",
"name": "JSON comments",
"description": "Test for JSON comments",
"type": "slave",
"id": 0,
"profile": "None",
"default_string_size": 24,
"dictionary": [
{
"index": "0x1003", // 4099
"name": "Pre-defined Error Field",
"struct": "array",
"group": "built-in",
"mandatory": false,
"profile_callback": true,
"each": {
"name": "Standard Error Field",
"type": "UNSIGNED32", // 7
"access": "ro",
"pdo": false,
"nbmin": 1,
"nbmax": 254
},
"sub": [
{
"name": "Number of Errors",
"type": "UNSIGNED8", // 5
"access": "rw",
"pdo": false
},
{
// "name": "Standard Error Field"
// "type": "UNSIGNED32" // 7
"value": 0
},
{
// "name": "Standard Error Field"
// "type": "UNSIGNED32" // 7
"value": 0
},
{
// "name": "Standard Error Field"
// "type": "UNSIGNED32" // 7
"value": 0
}
]
}
]
}
17 changes: 17 additions & 0 deletions tests/test_jsonod.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,20 @@ def test_jsonod_timezone():
if m:
print(m[1])
assert m[1] == cmp + "+" + tz


def test_jsonod_comments(odpath):
""" Test that the json file exports comments correctly. """

fname = odpath / "jsonod-comments.json"
m1 = Node.LoadFile(fname)
with open(fname, "r") as f:
od = f.read()

out = generate_jsonc(m1, compact=False, sort=False, internal=False, validate=True)

for a, b in zip(od.splitlines(), out.splitlines()):
print(a)
if '"$date"' in a or '"$tool"' in a:
continue
assert a == b