Exemplo n.º 1
0
        public static Task <DeserializeResult> TrySimpleDeserialize(string jsonString)
        {
            return(Task.Run <DeserializeResult>(async() =>
            {
                jsonString = jsonString.Trim();

                int firstBrace = jsonString.IndexOfAny(new char[] { '{', '[' });

                if (firstBrace >= 0)
                {
                    foreach (string str in new string[] { jsonString, CSEscape.Unescape(jsonString) })
                    {
                        Dictionary <string, object> dictionary = TryStrictDeserialize(str);
                        if (dictionary != null)
                        {
                            return new DeserializeResult(dictionary);
                        }
                    }
                }

                if (firstBrace != 0)
                {
                    DeserializeResult wrappedResult = await TrySimpleDeserialize("{" + jsonString + "}");
                    if (wrappedResult != null)
                    {
                        return wrappedResult;
                    }
                }

                return null;
            }));
        }
Exemplo n.º 2
0
        public static Task <DeserializeResult> TryDeserialize(string jsonString)
        {
            return(Task.Run <DeserializeResult>(async() =>
            {
                jsonString = jsonString.Trim();

                DeserializeResult result = await TrySimpleDeserialize(jsonString);
                if (result.IsSuccessful())
                {
                    return result;
                }

                if (jsonString.StartsWith("\"") && jsonString.EndsWith("\""))
                {
                    string quoteEscaped = jsonString.Substring(1, jsonString.Length - 2).Replace("\"\"", "\"");
                    result = await TrySimpleDeserialize(quoteEscaped);
                    if (result.IsSuccessful())
                    {
                        return result;
                    }
                }

                int firstBrace = jsonString.IndexOfAny(new char[] { '{' });

                if (firstBrace >= 0)
                {
                    foreach (string str in new string[] { jsonString, CSEscape.Unescape(jsonString) })
                    {
                        Dictionary <string, object> dictionary = TryStrictDeserialize(str);
                        if (dictionary != null)
                        {
                            return new DeserializeResult(dictionary);
                        }

                        result = TryTrimmedDeserialize(str, "{", "}", "{0}");
                        if (result.IsSuccessful())
                        {
                            return result;
                        }
                    }
                }

                if (firstBrace != 0)
                {
                    DeserializeResult wrappedResult = await TryDeserialize("{" + jsonString + "}");
                    if (wrappedResult.IsSuccessful())
                    {
                        return wrappedResult;
                    }
                }

                return null;
            }));
        }