Created
December 10, 2011 01:54
-
-
Save jdonaldson/1454243 to your computer and use it in GitHub Desktop.
jsonhxdef is a ruby script that helps you quickly define a typedef from some arbitary json
This file contains 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
typedef JsonObj = { | |
glossary:Glossary, | |
} | |
typedef Glossary = { | |
title:String, | |
GlossDiv:GlossDiv, | |
} | |
typedef GlossDiv = { | |
title:String, | |
GlossList:GlossList, | |
} | |
typedef GlossList = { | |
GlossEntry:GlossEntry, | |
} | |
typedef GlossEntry = { | |
GlossTerm:String, | |
GlossSee:String, | |
Abbrev:String, | |
Acronym:String, | |
ID:String, | |
GlossDef:GlossDef, | |
SortAs:String, | |
} | |
typedef GlossDef = { | |
para:String, | |
GlossSeeAlso:Array<String>, | |
} |
This file contains 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 ruby | |
# jsonhxdef: Turns some well-specified json into a haxe typedef | |
# Author: Justin Donaldson [email protected] | |
# | |
# Usage: ./jsonhxdef.rb input.json | |
# (output is written to stdout) | |
require 'rubygems' | |
require 'json' | |
def hash_equal(h1,h2) | |
return false if h1.length != h2.length | |
h1.each{|key,value| | |
return false if not h2.key?(key) | |
return false if h1[key] != h2[key] | |
} | |
return true | |
end | |
file_name = ARGV[0] | |
raise "No file argument" if File.name.nil? | |
raise "File does not exist" if not File.exists?(file_name) | |
str = IO.read(file_name) | |
json = JSON.parse(str) | |
objs = {} | |
unique_objs = {} | |
objs['JsonHxDef'] = json | |
while(not objs.empty?) | |
obkey = objs.keys[0] | |
obj = objs[obkey] | |
fobj = {} | |
obj.each{|key,value| | |
type = '' | |
arr_depth = 0 | |
if value.is_a? Array | |
while value.is_a? Array | |
value = value[0] | |
arr_depth +=1 | |
type = key.split(/[^a-zA-Z]/).map{|x| x.capitalize}.join("") | |
p type | |
end | |
if value.class == Hash | |
objs[type] = value | |
if arr_depth > 0 | |
type = "Array" + ("<" * arr_depth) + type + (">" * arr_depth) | |
end | |
fobj[key] = type | |
next | |
end | |
end | |
if value.is_a? Float | |
type = "Float" | |
elsif value.is_a? Integer | |
type = "Int" | |
elsif value.is_a?(TrueClass) || value.is_a?(FalseClass) | |
type = "Bool" | |
elsif value.is_a? String | |
type = "String" | |
elsif value.is_a? Hash | |
type = key.split(/[^a-zA-Z]/).map{|x| x.capitalize}.join("") | |
objs[type] = value | |
end | |
if arr_depth > 0 | |
type = "Array" + ("<" * arr_depth) + type + (">" * arr_depth) | |
end | |
fobj[key] = type | |
} | |
objs.delete(obkey) | |
while(unique_objs.key?(obkey)) | |
obkey +='X' | |
end | |
unique_objs[obkey]=true | |
print "\ntypedef " + obkey + " = {\n" | |
fobj.each{|key,value| | |
print "\t" + key + ":" + value + ",\n" | |
} | |
print "}\n" | |
end |
This file contains 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
{ | |
"glossary": { | |
"title": "example glossary", | |
"GlossDiv": { | |
"title": "S", | |
"GlossList": { | |
"GlossEntry": { | |
"ID": "SGML", | |
"SortAs": "SGML", | |
"GlossTerm": "Standard Generalized Markup Language", | |
"Acronym": "SGML", | |
"Abbrev": "ISO 8879:1986", | |
"GlossDef": { | |
"para": "A meta-markup language, used to create markup languages such as DocBook.", | |
"GlossSeeAlso": ["GML", "XML"] | |
}, | |
"GlossSee": "markup" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment