Пример #1
0
    private static bool SerializeValue(IJsonSerializerStrategy jsonSerializerStrategy, object value, StringBuilder builder)
    {
        bool   flag = true;
        string text = value as string;

        if (text != null)
        {
            flag = SimpleJsonTool.SerializeString(text, builder);
        }
        else
        {
            IDictionary <string, object> dictionary = value as IDictionary <string, object>;
            if (dictionary != null)
            {
                flag = SimpleJsonTool.SerializeObject(jsonSerializerStrategy, dictionary.Keys, dictionary.Values, builder);
            }
            else
            {
                IDictionary <string, string> dictionary2 = value as IDictionary <string, string>;
                if (dictionary2 != null)
                {
                    flag = SimpleJsonTool.SerializeObject(jsonSerializerStrategy, dictionary2.Keys, dictionary2.Values, builder);
                }
                else
                {
                    IEnumerable enumerable = value as IEnumerable;
                    if (enumerable != null)
                    {
                        flag = SimpleJsonTool.SerializeArray(jsonSerializerStrategy, enumerable, builder);
                    }
                    else if (SimpleJsonTool.IsNumeric(value))
                    {
                        flag = SimpleJsonTool.SerializeNumber(value, builder);
                    }
                    else if (value is bool)
                    {
                        builder.Append((!(bool)value) ? "false" : "true");
                    }
                    else if (value == null)
                    {
                        builder.Append("null");
                    }
                    else
                    {
                        object value2;
                        flag = jsonSerializerStrategy.TrySerializeNonPrimitiveObject(value, out value2);
                        if (flag)
                        {
                            SimpleJsonTool.SerializeValue(jsonSerializerStrategy, value2, builder);
                        }
                    }
                }
            }
        }
        return(flag);
    }
Пример #2
0
    public static string SerializeObject(object json, IJsonSerializerStrategy jsonSerializerStrategy)
    {
        StringBuilder stringBuilder = GetStringBuilder();
        bool          flag          = SimpleJsonTool.SerializeValue(jsonSerializerStrategy, json, stringBuilder);

        string res = (!flag) ? null : stringBuilder.ToString();

        RecycleStringBuilder(stringBuilder);
        return(res);
    }
Пример #3
0
    private static bool SerializeObject(IJsonSerializerStrategy jsonSerializerStrategy, IEnumerable keys, IEnumerable values, StringBuilder builder)
    {
        builder.Append("{");
        IEnumerator enumerator  = keys.GetEnumerator();
        IEnumerator enumerator2 = values.GetEnumerator();
        bool        flag        = true;
        bool        result;

        while (enumerator.MoveNext() && enumerator2.MoveNext())
        {
            object current  = enumerator.Current;
            object current2 = enumerator2.Current;
            if (!flag)
            {
                builder.Append(",");
            }
            string text = current as string;
            if (text != null)
            {
                SimpleJsonTool.SerializeString(text, builder);
            }
            else if (!SimpleJsonTool.SerializeValue(jsonSerializerStrategy, current2, builder))
            {
                result = false;
                return(result);
            }
            builder.Append(":");
            if (SimpleJsonTool.SerializeValue(jsonSerializerStrategy, current2, builder))
            {
                flag = false;
                continue;
            }
            result = false;
            return(result);
        }
        builder.Append("}");
        result = true;
        return(result);
    }
Пример #4
0
    private static bool SerializeArray(IJsonSerializerStrategy jsonSerializerStrategy, IEnumerable anArray, StringBuilder builder)
    {
        builder.Append("[");
        bool        flag       = true;
        IEnumerator enumerator = anArray.GetEnumerator();
        bool        result;

        try
        {
            while (enumerator.MoveNext())
            {
                object current = enumerator.Current;
                if (!flag)
                {
                    builder.Append(",");
                }
                if (!SimpleJsonTool.SerializeValue(jsonSerializerStrategy, current, builder))
                {
                    result = false;
                    return(result);
                }
                flag = false;
            }
        }
        finally
        {
            IDisposable disposable;
            if ((disposable = (enumerator as IDisposable)) != null)
            {
                disposable.Dispose();
            }
        }
        builder.Append("]");
        result = true;
        return(result);
    }