Json.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C)2005-2019 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. import php.*;
  24. import haxe.format.JsonPrinter;
  25. @:coreApi
  26. class Json {
  27. public static inline function parse(text:String):Dynamic {
  28. #if haxeJSON
  29. return haxe.format.JsonParser.parse(text);
  30. #else
  31. return phpJsonDecode(text);
  32. #end
  33. }
  34. public static inline function stringify(value:Dynamic, ?replacer:(key:Dynamic, value:Dynamic) -> Dynamic, ?space:String):String {
  35. #if haxeJSON
  36. return JsonPrinter.print(value, replacer, space);
  37. #else
  38. return phpJsonEncode(value, replacer, space);
  39. #end
  40. }
  41. static function phpJsonDecode(json:String):Dynamic {
  42. var value = Global.json_decode(json);
  43. if (value == null && Global.json_last_error() != Const.JSON_ERROR_NONE) {
  44. throw Global.json_last_error_msg();
  45. }
  46. return convertAfterDecode(value);
  47. }
  48. static function convertAfterDecode(value:Dynamic):Dynamic {
  49. if (Global.is_object(value)) {
  50. var result = new NativeAssocArray();
  51. var data = Syntax.array(value);
  52. Syntax.foreach(data, function(fieldName:String, fieldValue:Dynamic) {
  53. result[fieldName] = convertAfterDecode(fieldValue);
  54. });
  55. return Boot.createAnon(result);
  56. }
  57. if (Global.is_array(value)) {
  58. var result = new NativeIndexedArray();
  59. Syntax.foreach(value, function(index:Int, item:Dynamic) {
  60. result[index] = convertAfterDecode(item);
  61. });
  62. return @:privateAccess Array.wrap(result);
  63. }
  64. return value;
  65. }
  66. static function phpJsonEncode(value:Dynamic, ?replacer:(key:Dynamic, value:Dynamic) -> Dynamic, ?space:String):String {
  67. if (null != replacer || null != space) {
  68. return JsonPrinter.print(value, replacer, space);
  69. }
  70. var json = Global.json_encode(convertBeforeEncode(value));
  71. if (Global.json_last_error() != Const.JSON_ERROR_NONE) {
  72. return throw Global.json_last_error_msg();
  73. }
  74. return json;
  75. }
  76. static function convertBeforeEncode(value:Dynamic):Dynamic {
  77. if (Std.isOfType(value, Array)) {
  78. var result = new NativeIndexedArray();
  79. Syntax.foreach(value.arr, function(index:Int, item:Dynamic) {
  80. result[index] = convertBeforeEncode(item);
  81. });
  82. return result;
  83. }
  84. if (Global.is_object(value)) {
  85. var result = {};
  86. Syntax.foreach(value, function(fieldName:String, fieldValue:Dynamic) {
  87. Syntax.setField(result, fieldName, convertBeforeEncode(fieldValue));
  88. });
  89. return result;
  90. }
  91. if (Global.is_float(value) && !Global.is_finite(value)) {
  92. return null;
  93. }
  94. return value;
  95. }
  96. }