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

Object-based JSON serialization #103

Merged
merged 3 commits into from
Apr 14, 2015
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
21 changes: 20 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,24 @@ 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):
"""Produce a JSON-encoded SBP message.

"""
d = super( ((( m.identifier | classnameify ))), self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return ((( m.identifier | classnameify )))(sbp)
((*- endif *))
((* endif *))
((*- else *))
Expand Down
1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pylibftdi==0.14.2
pyserial==2.7
pytest==2.6.4
pytest-cov==1.8.1
pyyaml==3.11
tox==1.8.1
virtualenv==1.11.6
23 changes: 21 additions & 2 deletions python/sbp/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"""

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

# Automatically generated from piksi/yaml/swiftnav/sbp/acquisition.yaml
# with generate.py at 2015-04-12 20:54:10.838143. Please do not hand edit!
# with generate.py at 2015-04-14 12:12:07.025823. Please do not hand edit!


SBP_MSG_ACQ_RESULT = 0x0015
Expand Down Expand Up @@ -89,6 +90,24 @@ def to_binary(self):
c = Container(**exclude_fields(self))
self.payload = MsgAcqResult._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgAcqResult, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return MsgAcqResult(sbp)


msg_classes = {
Expand Down
59 changes: 57 additions & 2 deletions python/sbp/bootload.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
"""

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

# Automatically generated from piksi/yaml/swiftnav/sbp/bootload.yaml
# with generate.py at 2015-04-12 20:54:10.809448. Please do not hand edit!
# with generate.py at 2015-04-14 12:12:07.029893. Please do not hand edit!


SBP_MSG_BOOTLOADER_HANDSHAKE = 0x00B0
Expand Down Expand Up @@ -84,6 +85,24 @@ def to_binary(self):
c = Container(**exclude_fields(self))
self.payload = MsgBootloaderHandshake._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgBootloaderHandshake, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return MsgBootloaderHandshake(sbp)

SBP_MSG_BOOTLOADER_JUMP_TO_APP = 0x00B1
class MsgBootloaderJumpToApp(SBP):
Expand Down Expand Up @@ -133,6 +152,24 @@ def to_binary(self):
c = Container(**exclude_fields(self))
self.payload = MsgBootloaderJumpToApp._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgBootloaderJumpToApp, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return MsgBootloaderJumpToApp(sbp)

SBP_MSG_NAP_DEVICE_DNA = 0x00DD
class MsgNapDeviceDna(SBP):
Expand Down Expand Up @@ -186,6 +223,24 @@ def to_binary(self):
c = Container(**exclude_fields(self))
self.payload = MsgNapDeviceDna._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgNapDeviceDna, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return MsgNapDeviceDna(sbp)


msg_classes = {
Expand Down
77 changes: 75 additions & 2 deletions python/sbp/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"""

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

# Automatically generated from piksi/yaml/swiftnav/sbp/file_io.yaml
# with generate.py at 2015-04-12 20:54:10.812817. Please do not hand edit!
# with generate.py at 2015-04-14 12:12:07.031777. Please do not hand edit!


SBP_MSG_FILEIO_READ = 0x00A8
Expand Down Expand Up @@ -93,6 +94,24 @@ def to_binary(self):
c = Container(**exclude_fields(self))
self.payload = MsgFileioRead._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgFileioRead, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return MsgFileioRead(sbp)

SBP_MSG_FILEIO_READ_DIR = 0x00A9
class MsgFileioReadDir(SBP):
Expand Down Expand Up @@ -155,6 +174,24 @@ def to_binary(self):
c = Container(**exclude_fields(self))
self.payload = MsgFileioReadDir._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgFileioReadDir, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return MsgFileioReadDir(sbp)

SBP_MSG_FILEIO_REMOVE = 0x00AC
class MsgFileioRemove(SBP):
Expand Down Expand Up @@ -206,6 +243,24 @@ def to_binary(self):
c = Container(**exclude_fields(self))
self.payload = MsgFileioRemove._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgFileioRemove, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return MsgFileioRemove(sbp)

SBP_MSG_FILEIO_WRITE = 0x00AD
class MsgFileioWrite(SBP):
Expand Down Expand Up @@ -267,6 +322,24 @@ def to_binary(self):
c = Container(**exclude_fields(self))
self.payload = MsgFileioWrite._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgFileioWrite, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

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

"""
d = json.loads(data)
sbp = SBP.from_json_dict(d)
return MsgFileioWrite(sbp)


msg_classes = {
Expand Down
Loading