Ejemplo n.º 1
0
        private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType)
        {
            if (this.MemberMapCache.ContainsKey(objectType))
            {
                // map was stored in cache
                return(this.MemberMapCache[objectType]);
            }

            // create a new map
            Dictionary <string, MemberInfo> memberMap = new Dictionary <string, MemberInfo>();

            // load properties into property map
            //PropertyInfo[] properties = objectType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            IList <PropertyInfo> properties = GetAllProperties(objectType);

            foreach (PropertyInfo info in properties)
            {
                if (!info.CanRead || !info.CanWrite)
                {
                    continue;
                }

                if (JsonIgnoreAttribute.IsJsonIgnore(info))
                {
                    continue;
                }

                string jsonName = JsonNameAttribute.GetJsonName(info);
                if (String.IsNullOrEmpty(jsonName))
                {
                    string name = NameResolver.CSharpToJson(info.Name);
                    memberMap[name] = info;
//					memberMap[info.Name] = info;
                }
                else
                {
                    memberMap[jsonName] = info;
                }
            }

            // load public fields into property map
//			FieldInfo[] fields = objectType.GetFields();
//			FieldInfo[] fields = objectType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public );
            IList <FieldInfo> fields = GetAllFields(objectType);

            foreach (FieldInfo info in fields)
            {
//				if (!info.IsPublic)
//				{
//					continue;
//				}

                if (info.IsPrivate)
                {
                    int breakhere = 0;
                    ++breakhere;
                    //continue;
                }

                if (JsonIgnoreAttribute.IsJsonIgnore(info))
                {
                    continue;
                }

                string jsonName = JsonNameAttribute.GetJsonName(info);
                if (String.IsNullOrEmpty(jsonName))
                {
                    string name = NameResolver.CSharpToJson(info.Name);
                    memberMap[name] = info;
//					memberMap[info.Name] = info;
                }
                else
                {
                    memberMap[jsonName] = info;
                }
            }

            // store in cache for repeated usage
            this.MemberMapCache[objectType] = memberMap;

            return(memberMap);
        }
Ejemplo n.º 2
0
        protected virtual void WriteObject(object value, Type type)
        {
            bool appendDelim = false;

            this.Writer.Write(JsonReader.OperatorObjectStart);

            this.depth++;
            if (this.depth > this.settings.MaxDepth)
            {
                throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, this.settings.MaxDepth));
            }
            try
            {
                if (!String.IsNullOrEmpty(this.settings.TypeHintName))
                {
                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    this.WriteObjectProperty(this.settings.TypeHintName, type.FullName + ", " + type.Assembly.GetName().Name);
                }

#if JSONFX_SERIALIZE_PROPERTIES
                bool anonymousType = type.IsGenericType && type.Name.StartsWith(JsonWriter.AnonymousTypePrefix);
                // serialize public properties
                IList <PropertyInfo> properties = TypeCoercionUtility.GetAllProperties(type);
//				PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    if (!property.CanRead)
                    {
                        continue;
                    }

                    if (!property.CanWrite && !anonymousType)
                    {
                        continue;
                    }

                    if (this.IsIgnored(type, property, value))
                    {
                        continue;
                    }

                    if (property.PropertyType == typeof(Action))
                    {
                        continue;
                    }

                    object propertyValue = property.GetValue(value, null);
                    if (this.IsDefaultValue(property, propertyValue))
                    {
                        continue;
                    }

                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    // use Attributes here to control naming
                    string propertyName = JsonNameAttribute.GetJsonName(property);
                    if (String.IsNullOrEmpty(propertyName))
                    {
                        propertyName = NameResolver.CSharpToJson(property.Name);
//						propertyName = property.Name;
                    }

                    this.WriteObjectProperty(propertyName, propertyValue);
                }
#endif

                // serialize public fields
                IList <FieldInfo> fields = TypeCoercionUtility.GetAllFields(type);
//				FieldInfo[] fields = type.GetFields();
                foreach (FieldInfo field in fields)
                {
//					if (!field.IsPublic || field.IsStatic)
//					{
//						continue;
//					}

                    if (field.IsPrivate)
                    {
                        int breakhere = 1;
                        ++breakhere;
                    }

                    if (/*field.IsPrivate ||*/ field.IsStatic)
                    {
                        continue;
                    }

                    if (this.IsIgnored(type, field, value))
                    {
                        continue;
                    }

                    if (field.FieldType == typeof(Action))
                    {
                        continue;
                    }

                    object fieldValue = field.GetValue(value);
                    if (this.IsDefaultValue(field, fieldValue))
                    {
                        continue;
                    }

                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                        this.WriteLine();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    // use Attributes here to control naming
                    string fieldName = JsonNameAttribute.GetJsonName(field);
                    if (String.IsNullOrEmpty(fieldName))
                    {
                        fieldName = NameResolver.CSharpToJson(field.Name);
//						fieldName = field.Name;
                    }

                    this.WriteObjectProperty(fieldName, fieldValue);
                }
            }
            finally
            {
                this.depth--;
            }

            if (appendDelim)
            {
                this.WriteLine();
            }
            this.Writer.Write(JsonReader.OperatorObjectEnd);
        }