예제 #1
0
        private static void WriteJson(IJsonWrapper obj, JsonWriter writer)
        {
            if (obj == null)
            {
                writer.WriteNull();
                return;
            }

            if (obj.IsString)
            {
                writer.Write(obj.GetString());
                return;
            }

            if (obj.IsBoolean)
            {
                writer.Write(obj.GetBoolean());
                return;
            }

            if (obj.IsFloat)
            {
                writer.Write(obj.GetFloat());
                return;
            }

            if (obj.IsDouble)
            {
                writer.Write(obj.GetDouble());
                return;
            }

            if (obj.IsInt)
            {
                writer.Write(obj.GetInt());
                return;
            }

            if (obj.IsLong)
            {
                writer.Write(obj.GetLong());
                return;
            }

            if (obj.IsArray)
            {
                writer.WriteArrayStart();
                foreach (object elem in (IList)obj)
                {
                    WriteJson((JsonData)elem, writer);
                }
                writer.WriteArrayEnd();

                return;
            }

            if (obj.IsObject)
            {
                writer.WriteObjectStart();

                foreach (DictionaryEntry entry in ((IDictionary)obj))
                {
                    writer.WritePropertyName((string)entry.Key);
                    WriteJson((JsonData)entry.Value, writer);
                }
                writer.WriteObjectEnd();

                return;
            }
        }
예제 #2
0
        private static void WriteJson(IJsonWrapper wrapper, JsonWriter writer)
        {
            if (wrapper == null && !JsonWriter.SkipNullMember)
            {
                writer.Write(null);
                return;
            }

            if (wrapper.IsString)
            {
                writer.Write(wrapper.GetString());
                return;
            }

            if (wrapper.IsBoolean)
            {
                writer.Write(wrapper.GetBoolean());
                return;
            }

            if (wrapper.IsDouble)
            {
                writer.Write(wrapper.GetDouble());
                return;
            }

            if (wrapper.IsInt)
            {
                writer.Write(wrapper.GetInt());
                return;
            }
            if (wrapper.IsFloat)
            {
                writer.Write(wrapper.GetFloat());
                return;
            }
            if (wrapper.IsLong)
            {
                writer.Write(wrapper.GetLong());
                return;
            }

            if (wrapper.IsArray)
            {
                writer.WriteArrayStart();
                foreach (var elem in (IList)wrapper)
                {
                    WriteJson((JsonData)elem, writer);
                }
                writer.WriteArrayEnd();
                return;
            }

            if (wrapper.IsObject)
            {
                if (JsonWriter.SkipNullMember && wrapper.Count <= 0)
                {
                    return;
                }

                writer.WriteObjectStart();

                foreach (DictionaryEntry entry in (IDictionary)wrapper)
                {
                    if (JsonWriter.SkipNullMember)
                    {
                        var type = ((JsonData)entry.Value).GetJsonType();
                        if (type == JsonType.None)
                        {
                            continue;
                        }
                        if (type == JsonType.Array && ((IList)entry.Value).Count <= 0)
                        {
                            continue;
                        }
                    }
                    writer.WritePropertyName((string)entry.Key);
                    WriteJson((JsonData)entry.Value, writer);
                }
                writer.WriteObjectEnd();

                return;
            }

            if (wrapper.GetJsonType() == JsonType.None)
            {
                writer.WriteObjectStart();
                writer.WriteObjectEnd();
            }
        }