-
-
Save louisbl/5720021 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
package ; | |
abstract JsonMap<T>({ }) from {} { | |
public function new() this = {}; | |
public function exists(key:String) return Reflect.hasField(this, key); | |
public function get(key:String) return Reflect.field(this, key); | |
public function set(key:String, value:T) Reflect.setField(this, key, value); | |
public function keys():Array<String> return Reflect.fields(this); | |
public function remove(key:String) return Reflect.deleteField(this, key); | |
public function iterator():Iterator<T> { | |
var i = 0, | |
keys = keys(this); | |
return { | |
hasNext : function () return i < keys.length, | |
next: function () return get(this, keys[i++]) | |
} | |
} | |
} |
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
package ; | |
class Usage { | |
static function main() { | |
var obj: { h : JsonMap<String> } = haxe.Json.parse('{ "h": { "key1" : "value1" } }'); | |
trace(obj.h.exists('key1'));//true | |
trace(obj.h.exists('key2'));//false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment