示例#1
0
        private static void WriteValue(object obj, JsonWriter writer,
                                       bool writer_is_private,
                                       int depth)
        {
            if (depth > max_nesting_depth)
            {
                throw new JsonException(
                          String.Format("Max allowed object depth reached while " +
                                        "trying to export from type {0}",
                                        obj.GetType()));
            }

            if (obj == null)
            {
                writer.Write(null);
                return;
            }

            if (obj is IJsonWrapper)
            {
                if (writer_is_private)
                {
                    writer.WriteJSONStr(((IJsonWrapper)obj).ToJson());
                }
                else
                {
                    ((IJsonWrapper)obj).ToJson(writer);
                }

                return;
            }

            if (obj is String)
            {
                writer.Write((string)obj);
                return;
            }

            if (obj is Double)
            {
                writer.Write((double)obj);
                return;
            }

            if (obj is Int32)
            {
                writer.Write((int)obj);
                return;
            }

            if (obj is Boolean)
            {
                writer.Write((bool)obj);
                return;
            }

            if (obj is Int64)
            {
                writer.Write((long)obj);
                return;
            }

            if (obj is Array)
            {
                writer.WriteArrayStart();

                Array arrayobj = (Array)obj;
                for (int i = 0; i < arrayobj.Length; ++i)
                {
                    WriteValue(arrayobj.GetValue(i), writer, writer_is_private, depth + 1);
                }

                writer.WriteArrayEnd();

                return;
            }

            if (obj is IList)
            {
                writer.WriteArrayStart();
                IList listobj = (IList)obj;
                for (int i = 0; i < listobj.Count; ++i)
                {
                    WriteValue(listobj[i], writer, writer_is_private, depth + 1);
                }
                writer.WriteArrayEnd();

                return;
            }

            if (obj is IDictionary)
            {
                writer.WriteObjectStart();
                foreach (DictionaryEntry entry in (IDictionary)obj)
                {
                    writer.WritePropertyName((string)entry.Key);
                    WriteValue(entry.Value, writer, writer_is_private,
                               depth + 1);
                }
                writer.WriteObjectEnd();

                return;
            }

            Type obj_type = obj.GetType();

            // See if there's a custom exporter for the object
            if (custom_exporters_table.ContainsKey(obj_type))
            {
                ExporterFunc exporter = custom_exporters_table[obj_type];
                exporter(obj, writer);

                return;
            }

            // If not, maybe there's a base exporter
            if (base_exporters_table.ContainsKey(obj_type))
            {
                ExporterFunc exporter = base_exporters_table[obj_type];
                exporter(obj, writer);

                return;
            }

            // Last option, let's see if it's an enum
            if (obj is Enum)
            {
                Type e_type = Enum.GetUnderlyingType(obj_type);

                if (e_type == typeof(long) ||
                    e_type == typeof(uint) ||
                    e_type == typeof(ulong))
                {
                    writer.Write((ulong)obj);
                }
                else
                {
                    writer.Write((int)obj);
                }

                return;
            }

            // Okay, so it looks like the input should be exported as an
            // object
            AddTypeProperties(obj_type);
            IList <PropertyMetadata> props = type_properties[obj_type];

            writer.WriteObjectStart();
            for (int i = 0; i < props.Count; ++i)
            {
                if (props[i].IsField)
                {
                    writer.WritePropertyName(props[i].Info.Name);
                    WriteValue(((FieldInfo)props[i].Info).GetValue(obj),
                               writer, writer_is_private, depth + 1);
                }
                else
                {
                    PropertyInfo p_info = (PropertyInfo)props[i].Info;

                    if (p_info.CanRead)
                    {
                        writer.WritePropertyName(props[i].Info.Name);
                        WriteValue(p_info.GetValue(obj, null),
                                   writer, writer_is_private, depth + 1);
                    }
                }
            }
            writer.WriteObjectEnd();
        }