Json.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe;
  23. @:coreApi
  24. class Json {
  25. public static inline function parse( text : String ) : Dynamic {
  26. #if !haxeJSON
  27. return phpJsonDecode(text);
  28. #else
  29. return haxe.format.JsonParser.parse(text);
  30. #end
  31. }
  32. public static inline function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space:String ) : String {
  33. #if !haxeJSON
  34. return phpJsonEncode(value, replacer, space);
  35. #else
  36. return haxe.format.JsonPrinter.print(value, replacer, space);
  37. #end
  38. }
  39. static function phpJsonDecode(json:String):Dynamic {
  40. var val = untyped __call__("json_decode", json);
  41. if (val == null && untyped __php__("json_last_error() != JSON_ERROR_NONE")) {
  42. throw untyped __call__("json_last_error_msg");
  43. }
  44. return convertAfterDecode(val);
  45. }
  46. static function convertAfterDecode(val:Dynamic):Dynamic {
  47. var arr:php.NativeArray;
  48. if (untyped __call__("is_object", val)) {
  49. arr = phpMapArray(php.Lib.associativeArrayOfObject(val), convertAfterDecode);
  50. return untyped __call__("_hx_anonymous", arr);
  51. }
  52. else if (untyped __call__("is_array", val)) {
  53. arr = phpMapArray(val, convertAfterDecode);
  54. return php.Lib.toHaxeArray(arr);
  55. }
  56. else
  57. return val;
  58. }
  59. static function phpJsonEncode(val:Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space:String):String {
  60. if(null != replacer || null != space)
  61. return haxe.format.JsonPrinter.print(val, replacer, space);
  62. var json = untyped __call__("json_encode", convertBeforeEncode(val));
  63. if (untyped __physeq__(json, false))
  64. return throw "invalid json";
  65. else
  66. return json;
  67. }
  68. static function convertBeforeEncode(val:Dynamic):Dynamic {
  69. var arr:php.NativeArray;
  70. if (untyped __call__("is_object", val)) {
  71. switch (untyped __call__("get_class", val)) {
  72. case "_hx_anonymous", "stdClass" :
  73. arr = php.Lib.associativeArrayOfObject(val);
  74. if(untyped __php__('!{0}', arr)) return {};
  75. case "_hx_array" : arr = php.Lib.toPhpArray(val);
  76. case "Date" : return Std.string(val); //.split(" ").join("T"); //better with "T"?
  77. case "HList" : arr = php.Lib.toPhpArray(Lambda.array(val)); //convert List to array?
  78. case "_hx_enum" : return Type.enumIndex(val);
  79. case "StringMap", "IntMap" : arr = php.Lib.associativeArrayOfHash(val);
  80. default : arr = php.Lib.associativeArrayOfObject(val);
  81. }
  82. }
  83. else if (untyped __call__("is_array", val)) arr = val;
  84. else {
  85. if (untyped __call__("is_float",val) && !__call__("is_finite",val)) val = null;
  86. return val;
  87. }
  88. return phpMapArray(arr, convertBeforeEncode);
  89. }
  90. inline static function phpMapArray(arr:php.NativeArray, func:Dynamic->Dynamic):php.NativeArray {
  91. return untyped __call__("array_map", func, arr);
  92. }
  93. }