Skip to content

Instantly share code, notes, and snippets.

@ligustah
Created August 19, 2012 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ligustah/3393886 to your computer and use it in GitHub Desktop.
Save ligustah/3393886 to your computer and use it in GitHub Desktop.
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