Advertisement
YellowAfterlife

String array access upon a typedef from a JSON in Haxe

Oct 12th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 0.58 KB | None | 0 0
  1. package ;
  2. import haxe.Json;
  3.  
  4. abstract StringKey({}) from {} {
  5.     @:extern @:arrayAccess public inline function arrayAccess(key:String):Dynamic {
  6.         return Reflect.field(this, key);
  7.     }
  8.     @:extern @:arrayAccess public inline function arrayWrite<T>(key:String, value:T):T {
  9.         Reflect.setField(this, key, value);
  10.         return value;
  11.     }
  12. }
  13.  
  14. typedef Some = {
  15.     sk: StringKey,
  16. }
  17.  
  18. class Main {
  19.    
  20.     static function main() {
  21.         var aVal:Int = Std.int(Math.random() * 9 + 1);
  22.         var a:Some = Json.parse("{ \"sk\": { \"a\": " + aVal + ", \"b\": 3 } }");
  23.         trace(a);
  24.         trace(a.sk["a"]);
  25.     }
  26.    
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement