Last active
May 25, 2021 10:17
-
-
Save germandiagogomez/1762b0aecdb793b50b9c3e700308369a to your computer and use it in GitHub Desktop.
Dynamic Json object
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
class JsonObject { | |
var contents | |
def JsonObject(string jsonStr) { | |
this.contents = from_json(jsonStr) | |
} | |
def JsonObject(Map contents) { | |
this.contents = contents | |
} | |
def has_attrib(name) { | |
var c := this.contents | |
if (type_name(c) == "Map") { | |
var found = false | |
c.for_each(fun[name, found](elem) { | |
if (elem.first == name) { | |
found = true | |
} | |
}) | |
return found | |
} | |
else { | |
return false | |
} | |
} | |
def method_missing(string name, Vector v) { | |
var field := this.contents[name] | |
if (field.type_name() == "Map") { | |
return JsonObject(field) | |
} | |
else { | |
return field | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment