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

Expand JSON Serialization to entire object #97

Closed
mfine opened this issue Apr 13, 2015 · 5 comments
Closed

Expand JSON Serialization to entire object #97

mfine opened this issue Apr 13, 2015 · 5 comments

Comments

@mfine
Copy link
Contributor

mfine commented Apr 13, 2015

We need full JSON serialization of the SBP messages. Presently, just the SBP base message is being JSON serialized. Some binary fields will need to be base64 encoded.

@mfine
Copy link
Contributor Author

mfine commented Apr 13, 2015

  1. Why? Presently, only the base SBP message is serialized in JSON - we need the full message format fully serialized, while adequately handling binary fields which need to be base64-encoded.
  2. How? Expand the python templates to support to_json_dict and from_json_dict methods for each message, with an augmented yaml spec indicating which fields need base64-encoding.
  3. Testing Add serialization/deserialization exercising for each message to the suite of tests.

@fnoble
Copy link
Contributor

fnoble commented Apr 13, 2015

Do we need to specify which fields need to be base64 encoded or is it clear from specifying a u8 array vs a string?

@mfine
Copy link
Contributor Author

mfine commented Apr 13, 2015

There's been problems with the strings in the past, since they're actually 20-byte arrays that are null terminated - the extra bytes screw up the serialization. I'll try and see if there's an easy way to eliminate augmenting the specs - but not sure yet.

@mfine
Copy link
Contributor Author

mfine commented Apr 13, 2015

For posterity, here's the hack I did during the hackathon to find serialization issues:

+def todict(obj, classkey=None):
+    if isinstance(obj, dict):
+        data = {}
+        for (k, v) in obj.items():
+            data[k] = todict(v, classkey)
+        return data
+    elif hasattr(obj, "_ast"):
+        return todict(obj._ast())
+    elif hasattr(obj, "__iter__"):
+        return [todict(v, classkey) for v in obj]
+    elif hasattr(obj, "__dict__"):
+        if hasattr(obj, "payload"):
+            obj.payload = base64.standard_b64encode(obj.payload)
+        if hasattr(obj, "name"):
+            obj.name = base64.standard_b64encode(obj.name)
+        data = dict([(key, todict(value, classkey))
+            for key, value in obj.__dict__.iteritems() 
+            if not callable(value) and not key.startswith('_')])
+        if classkey is not None and hasattr(obj, "__class__"):
+            data[classkey] = obj.__class__.__name__
+        return data
+    else:
+        return obj

@mfine
Copy link
Contributor Author

mfine commented Apr 14, 2015

Completed. Note, did not have to augment the message yamls to identity areas that needed base64 encoding.

@mfine mfine closed this as completed Apr 14, 2015
@imh imh removed the 5 - Done label Apr 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants