From 62c02435e3149752827faf33b4847114a9655fc8 Mon Sep 17 00:00:00 2001 From: Max Liu Date: Fri, 28 Sep 2012 17:40:07 -0700 Subject: [PATCH] Fix Asian characters in JSON issue in php version use native json_decode instead and wrap objects and array in haxe objects and haxe array --- source/haxe/afung/mangaWeb3/server/Utility.hx | 92 ++++++++++++++----- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/source/haxe/afung/mangaWeb3/server/Utility.hx b/source/haxe/afung/mangaWeb3/server/Utility.hx index a873a8e..c665812 100644 --- a/source/haxe/afung/mangaWeb3/server/Utility.hx +++ b/source/haxe/afung/mangaWeb3/server/Utility.hx @@ -1,5 +1,6 @@ package afung.mangaWeb3.server; -import haxe.Json; + +import php.Lib; /** * ... @@ -8,29 +9,72 @@ import haxe.Json; class Utility { - public static function ParseJson(jsonString:String):Dynamic - { - try - { - var json:Dynamic = Json.parse(jsonString); - switch(Type.typeof(json)) - { - case TObject: - return json; - default: - return null; - } - } - catch (e:Dynamic) - { - return null; - } - } - - public static function Md5(input:String):String - { - return untyped __call__("md5", input); - } + public static function ParseJson(jsonString:String):Dynamic + { + try + { + var json:Dynamic = untyped __call__("json_decode", jsonString); + if (!untyped __call__("is_object", json)) + { + return null; + } + + json = ProcessJson(json); + return json; + } + catch (e:Dynamic) + { + return null; + } + } + + private static function ProcessJson(json:Dynamic):Dynamic + { + var fields:Array = Reflect.fields(json); + var obj = { }; + + for (fieldName in fields) + { + var field:Dynamic = Reflect.field(json, fieldName); + + if (untyped __call__("is_object", field)) + { + field = ProcessJson(field); + } + else if (untyped __call__("is_array", field)) + { + var haxeArray:Array = Lib.toHaxeArray(field); + ProcessJsonArray(haxeArray); + field = haxeArray; + } + + Reflect.setField(obj, fieldName, field); + } + + return obj; + } + + private static function ProcessJsonArray(haxeArray:Array):Void + { + for (i in 0...haxeArray.length) + { + if (untyped __call__("is_object", haxeArray[i])) + { + ProcessJson(haxeArray[i]); + } + else if (untyped __call__("is_array", haxeArray[i])) + { + var haxeArray2:Array = Lib.toHaxeArray(haxeArray[i]); + ProcessJsonArray(haxeArray2); + haxeArray[i] = haxeArray2; + } + } + } + + public static function Md5(input:String):String + { + return untyped __call__("md5", input); + } public static function ArrayContains(array:Array, value:Dynamic):Bool {