Esempio n. 1
0
 private static void WriteValue(object obj, JsonWriter writer, bool writer_is_private, int depth)
 {
     if (depth > JsonMapper.max_nesting_depth)
     {
         JsonException.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);
     }
     else if (obj is IJsonWrapper)
     {
         if (writer_is_private)
         {
             writer.TextWriter.Write(((IJsonWrapper)obj).ToJson());
         }
         else
         {
             ((IJsonWrapper)obj).ToJson(writer);
         }
     }
     else if (obj is string)
     {
         writer.Write((string)obj);
     }
     else if (obj is double)
     {
         writer.Write((double)obj);
     }
     else if (obj is int)
     {
         writer.Write((int)obj);
     }
     else if (obj is bool)
     {
         writer.Write((bool)obj);
     }
     else if (obj is long)
     {
         writer.Write((long)obj);
     }
     else if (obj is Array)
     {
         writer.WriteArrayStart();
         foreach (object current in ((Array)obj))
         {
             JsonMapper.WriteValue(current, writer, writer_is_private, depth + 1);
         }
         writer.WriteArrayEnd();
     }
     else if (obj is IList)
     {
         writer.WriteArrayStart();
         foreach (object current in ((IList)obj))
         {
             JsonMapper.WriteValue(current, writer, writer_is_private, depth + 1);
         }
         writer.WriteArrayEnd();
     }
     else if (obj is IDictionary)
     {
         writer.WriteObjectStart();
         foreach (DictionaryEntry dictionaryEntry in ((IDictionary)obj))
         {
             writer.WritePropertyName((string)dictionaryEntry.Key);
             JsonMapper.WriteValue(dictionaryEntry.Value, writer, writer_is_private, depth + 1);
         }
         writer.WriteObjectEnd();
     }
     else
     {
         Type type = obj.GetType();
         if (JsonMapper.custom_exporters_table.ContainsKey(type))
         {
             ExporterFunc exporterFunc = JsonMapper.custom_exporters_table[type];
             exporterFunc(obj, writer);
         }
         else if (JsonMapper.base_exporters_table.ContainsKey(type))
         {
             ExporterFunc exporterFunc = JsonMapper.base_exporters_table[type];
             exporterFunc(obj, writer);
         }
         else if (obj is Enum)
         {
             Type underlyingType = Enum.GetUnderlyingType(type);
             if (underlyingType == typeof(long) || underlyingType == typeof(uint) || underlyingType == typeof(ulong))
             {
                 writer.Write((ulong)obj);
             }
             else
             {
                 writer.Write((int)obj);
             }
         }
         else
         {
             JsonMapper.AddTypeProperties(type);
             IList <PropertyMetadata> list = JsonMapper.type_properties[type];
             writer.WriteObjectStart();
             foreach (PropertyMetadata current2 in list)
             {
                 if (current2.IsField)
                 {
                     writer.WritePropertyName(current2.Info.Name);
                     JsonMapper.WriteValue(((FieldInfo)current2.Info).GetValue(obj), writer, writer_is_private, depth + 1);
                 }
                 else
                 {
                     PropertyInfo propertyInfo = (PropertyInfo)current2.Info;
                     if (propertyInfo.CanRead)
                     {
                         writer.WritePropertyName(current2.Info.Name);
                         JsonMapper.WriteValue(propertyInfo.GetValue(obj, null), writer, writer_is_private, depth + 1);
                     }
                 }
             }
             writer.WriteObjectEnd();
         }
     }
 }
Esempio n. 2
0
        private static void WriteValue(object obj, JsonWriter writer, bool writer_is_private, int depth)
        {
            if (depth > JsonMapper.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.TextWriter.Write(((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 float)
            {
                writer.Write((double)((float)obj));
                return;
            }
            if (obj is int)
            {
                writer.Write((int)obj);
                return;
            }
            if (obj is bool)
            {
                writer.Write((bool)obj);
                return;
            }
            if (obj is long)
            {
                writer.Write((long)obj);
                return;
            }
            if (obj is Array)
            {
                writer.WriteArrayStart();
                IEnumerator enumerator = ((Array)obj).GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        object obj2 = enumerator.Current;
                        JsonMapper.WriteValue(obj2, writer, writer_is_private, depth + 1);
                    }
                }
                finally
                {
                    IDisposable disposable;
                    if ((disposable = (enumerator as IDisposable)) != null)
                    {
                        disposable.Dispose();
                    }
                }
                writer.WriteArrayEnd();
                return;
            }
            if (obj is IList)
            {
                writer.WriteArrayStart();
                IEnumerator enumerator2 = ((IList)obj).GetEnumerator();
                try
                {
                    while (enumerator2.MoveNext())
                    {
                        object obj3 = enumerator2.Current;
                        JsonMapper.WriteValue(obj3, writer, writer_is_private, depth + 1);
                    }
                }
                finally
                {
                    IDisposable disposable2;
                    if ((disposable2 = (enumerator2 as IDisposable)) != null)
                    {
                        disposable2.Dispose();
                    }
                }
                writer.WriteArrayEnd();
                return;
            }
            if (obj is IDictionary)
            {
                writer.WriteObjectStart();
                IDictionaryEnumerator enumerator3 = ((IDictionary)obj).GetEnumerator();
                try
                {
                    while (enumerator3.MoveNext())
                    {
                        object          obj4            = enumerator3.Current;
                        DictionaryEntry dictionaryEntry = (DictionaryEntry)obj4;
                        writer.WritePropertyName((string)dictionaryEntry.Key);
                        JsonMapper.WriteValue(dictionaryEntry.Value, writer, writer_is_private, depth + 1);
                    }
                }
                finally
                {
                    IDisposable disposable3;
                    if ((disposable3 = (enumerator3 as IDisposable)) != null)
                    {
                        disposable3.Dispose();
                    }
                }
                writer.WriteObjectEnd();
                return;
            }
            Type type = obj.GetType();

            if (JsonMapper.custom_exporters_table.ContainsKey(type))
            {
                ExporterFunc exporterFunc = JsonMapper.custom_exporters_table[type];
                exporterFunc(obj, writer);
                return;
            }
            if (JsonMapper.base_exporters_table.ContainsKey(type))
            {
                ExporterFunc exporterFunc2 = JsonMapper.base_exporters_table[type];
                exporterFunc2(obj, writer);
                return;
            }
            if (obj is Enum)
            {
                Type underlyingType = Enum.GetUnderlyingType(type);
                if (underlyingType == typeof(long) || underlyingType == typeof(uint) || underlyingType == typeof(ulong))
                {
                    writer.Write((ulong)obj);
                }
                else
                {
                    writer.Write((int)obj);
                }
                return;
            }
            JsonMapper.AddTypeProperties(type);
            IList <PropertyMetadata> list = JsonMapper.type_properties[type];

            writer.WriteObjectStart();
            foreach (PropertyMetadata propertyMetadata in list)
            {
                if (propertyMetadata.IsField)
                {
                    writer.WritePropertyName(propertyMetadata.Info.Name);
                    JsonMapper.WriteValue(((FieldInfo)propertyMetadata.Info).GetValue(obj), writer, writer_is_private, depth + 1);
                }
                else
                {
                    PropertyInfo propertyInfo = (PropertyInfo)propertyMetadata.Info;
                    if (propertyInfo.CanRead)
                    {
                        writer.WritePropertyName(propertyMetadata.Info.Name);
                        JsonMapper.WriteValue(propertyInfo.GetValue(obj, null), writer, writer_is_private, depth + 1);
                    }
                }
            }
            writer.WriteObjectEnd();
        }