From b41f7c704096e45972a9d974af9b0c24228dee24 Mon Sep 17 00:00:00 2001 From: HeLihua Date: Wed, 17 May 2017 17:51:42 +0800 Subject: [PATCH 1/2] fix failed to decode a string contains unicode white space char begins like BOM --- Assets/Plugins/MiniJSON.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Plugins/MiniJSON.cs b/Assets/Plugins/MiniJSON.cs index 2716e74..da504d0 100644 --- a/Assets/Plugins/MiniJSON.cs +++ b/Assets/Plugins/MiniJSON.cs @@ -385,7 +385,7 @@ protected static int getLastIndexOfNumber( char[] json, int index ) protected static void eatWhitespace( char[] json, ref int index ) { for( ; index < json.Length; index++ ) - if( " \t\n\r".IndexOf( json[index] ) == -1 ) + if( " \t\n\r\u200B\uFEFF".IndexOf( json[index] ) == -1 ) { break; } From 9e8bcfed72e3946a41e433e7ce1aef682ab0456a Mon Sep 17 00:00:00 2001 From: HeLihua Date: Wed, 17 May 2017 17:55:01 +0800 Subject: [PATCH 2/2] add support to decode string contains emoji char --- Assets/Plugins/MiniJSON.cs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/Assets/Plugins/MiniJSON.cs b/Assets/Plugins/MiniJSON.cs index da504d0..a8b1539 100644 --- a/Assets/Plugins/MiniJSON.cs +++ b/Assets/Plugins/MiniJSON.cs @@ -327,10 +327,37 @@ protected static string parseString( char[] json, ref int index ) s += "&#x" + new string( unicodeCharArray ) + ";"; /* -uint codePoint = UInt32.Parse(new string(unicodeCharArray), NumberStyles.HexNumber); -// convert the integer codepoint to a unicode char and add to string -s += Char.ConvertFromUtf32((int)codePoint); -*/ + uint codePoint = UInt32.Parse(new string(unicodeCharArray), NumberStyles.HexNumber); + // convert the integer codepoint to a unicode char and add to string + try + { + s += Char.ConvertFromUtf32((int)codePoint); + // skip 4 chars + index += 4; + } + catch (ArgumentOutOfRangeException ex) + { + if (remainingLength >= 10) + { + try + { + char[] highSurrogateCharArray = unicodeCharArray; + char[] lowSurrogateCharArray = new char[4]; + // skip highSurrogateCharArray and \u, like E1EC\u total 6 char = 4 HEX chars + \u + Array.Copy(json, index + 6, lowSurrogateCharArray, 0, 4); + int highSurrogateCharCodePoint = Int32.Parse(new string(highSurrogateCharArray), NumberStyles.HexNumber); + int lowSurrogateCharCodePoint = Int32.Parse(new string(lowSurrogateCharArray), NumberStyles.HexNumber); + s += ("" + Convert.ToChar(highSurrogateCharCodePoint) + Convert.ToChar(lowSurrogateCharCodePoint)); + // skip a unicode char array like \uE1EC , total 6 char = \u + 4 HEX chars + index += (4 + 6); + } + catch(Exception e) + { + index += 4; + } + } + } + */ // skip 4 chars index += 4;