Last active
December 14, 2015 10:29
-
-
Save back2dos/5072589 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); | |
@:arrayAccess public function get(key:String) return Reflect.field(this, key); | |
@:arrayAccess public function set(key:String, value:T):T { | |
Reflect.setField(this, key, value); | |
return 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(); | |
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 | |
trace(obj.h['key1']);//value1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment