Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active December 14, 2015 10:29
Show Gist options
  • Save back2dos/5072589 to your computer and use it in GitHub Desktop.
Save back2dos/5072589 to your computer and use it in GitHub Desktop.
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++])
}
}
}
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