Skip to content

Instantly share code, notes, and snippets.

@louisbl
Forked from back2dos/JsonMap.hx
Created June 6, 2013 08:05
Show Gist options
  • Save louisbl/5720021 to your computer and use it in GitHub Desktop.
Save louisbl/5720021 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);
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++])
}
}
}
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