Created
August 19, 2012 09:09
-
-
Save ligustah/3393886 to your computer and use it in GitHub Desktop.
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
module tojsons | |
import json | |
local orig_toJSON = json.toJSON | |
local function obj2Table(value : instance) { | |
local t = {} | |
foreach(k, v; fieldsOf(value)) { | |
if(isInstance(v)) | |
v = obj2Table(v) | |
t[k] = v | |
} | |
return t | |
} | |
json.toJSON = function(value : table | array | instance, pretty : bool = false) { | |
if(isInstance(value)) { | |
value = obj2Table(value) | |
} | |
return orig_toJSON(value, pretty) | |
} | |
function main() { | |
class A { | |
a | |
b | |
c | |
function constructor() { | |
:a = 1 | |
:b = 3 | |
} | |
} | |
class B { | |
d | |
e | |
f | |
function constructor() { | |
:e = "bla" | |
:f = A() | |
} | |
} | |
local b = B() | |
writeln $ json.toJSON $ b, true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment