Created
March 6, 2014 21:18
-
-
Save tav/9399862 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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