Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP JSON serialization #101

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 15 additions & 1 deletion generator/sbpg/targets/resources/sbp_construct_template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"""

from construct import *
import json
from sbp import SBP
from sbp.utils import fmt_repr, exclude_fields
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict
import six

((*- for i in include *))
Expand Down Expand Up @@ -144,6 +145,19 @@ class ((( m.identifier | classnameify )))(SBP):
c = Container(**exclude_fields(self))
self.payload = ((( m.identifier | classnameify )))._parser.build(c)
return self.pack()

def to_json(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the __dict__, this doesn't get the parent objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does (I had filtered them originally with exclude_fields).

"""Produce a JSON-encoded SBP message.

"""
return json.dumps(walk_json_dict(self.__dict__))

@staticmethod
def from_json(data):
"""Given a JSON-encoded message, build an object.

"""
return ((( m.identifier | classnameify )))(**json.loads(data))
((*- endif *))
((* endif *))
((*- else *))
Expand Down
17 changes: 16 additions & 1 deletion python/sbp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@

EXCLUDE = ['sender', 'msg_type', 'crc', 'length', 'preamble', 'payload']


def exclude_fields(obj, exclude=EXCLUDE):
"""
Return dict of object without parent attrs.
"""
return {k: v for k, v in obj.__dict__.items() if k not in exclude}

def walk_json_dict(coll):
"""
Flatten a parsed SBP object into a dicts and lists, which are
compatible for JSON output.

Parameters
----------
coll : dict

"""
if isinstance(coll, dict):
return dict((k, walk_json_dict(v)) for (k, v) in coll.iteritems())
elif hasattr(coll, '__iter__'):
return [walk_json_dict(dict(seq.items())) for seq in coll]
else:
return coll

def fmt_repr(obj):
"""Print a orphaned string representation of an object without the
Expand Down
4 changes: 4 additions & 0 deletions python/tests/sbp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def _assert_msg_roundtrip(msg, raw_packet):
"""
assert base64.standard_b64encode(msg.to_binary()) == raw_packet

def _assert_json(msg):
assert msg.from_json(msg.to_json()) == msg

def _assert_sane_package(pkg_name, pkg):
"""
Sanity check the package collection of tests before actually
Expand Down Expand Up @@ -136,3 +139,4 @@ def assert_package(test_filename, pkg_name):
_assert_sbp(sbp, test_case['sbp'])
_assert_msg(dispatch(sbp), test_case['msg'])
_assert_msg_roundtrip(dispatch(sbp), test_case['raw_packet'])
_assert_json(dispatch(sbp))