Skip to content

Instantly share code, notes, and snippets.

@tav
Created March 6, 2014 21:18
Show Gist options
  • Save tav/9399862 to your computer and use it in GitHub Desktop.
Save tav/9399862 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import sys
import json
struct = ['type x ']
out = struct.append
def handle_elem(elem):
if isinstance(elem, list):
out('[]')
handle_seq(elem)
elif isinstance(elem, dict):
handle_obj(elem)
elif elem == 'string':
out('string\n')
elif elem == 'number':
out('int\n')
def handle_seq(seq):
for elem in seq:
handle_elem(elem)
def handle_obj(obj):
out('struct {\n')
for key in sorted(obj):
out('%s ' % key)
value = obj[key]
handle_elem(obj[key])
out('}\n')
schema = json.loads(sys.stdin.read().strip())
handle_obj(schema)
print ''.join(struct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment