/// <summary> /// Gets a value which indicates if should be ignored in xml serialization. /// </summary> /// <param name="value"></param> /// <returns></returns> public static bool IsXmlIgnore(object value) { if (value == null) { return(false); } Type type = value.GetType(); ICustomAttributeProvider provider = null; if (TCU.GetTypeInfo(type).IsEnum) { provider = TCU.GetTypeInfo(type).GetField(Enum.GetName(type, value)); } else { provider = value as ICustomAttributeProvider; } if (provider == null) { throw new ArgumentException(); } #if !UNITY3D return(provider.IsDefined(typeof(XmlIgnoreAttribute), true)); #else return(false); #endif }
/** Returns a member map if suitable for the object type. * Dictionary types will make this method return null */ public Dictionary <string, MemberInfo> GetMemberMap(Type objectType) { // don't incurr the cost of member map for dictionaries if (TCU.GetTypeInfo(typeof(IDictionary)).IsAssignableFrom(TCU.GetTypeInfo(objectType))) { return(null); } else { return(this.CreateMemberMap(objectType)); } }
/// <summary> /// Gets the name specified for use in serialization. /// </summary> /// <returns></returns> public static string GetPropertyName(object value) { if (value == null) { return(null); } Type type = value.GetType(); MemberInfo memberInfo = null; if (TCU.GetTypeInfo(type).IsEnum) { string name = Enum.GetName(type, value); if (String.IsNullOrEmpty(name)) { return(null); } memberInfo = TCU.GetTypeInfo(type).GetField(name); } else { memberInfo = value as MemberInfo; } if (MemberInfo.Equals(memberInfo, null)) { throw new ArgumentException(); } #if WINDOWS_STORE BTPropertyAttribute attribute = memberInfo.GetCustomAttribute <BTPropertyAttribute>(true); #else BTPropertyAttribute attribute = Attribute.GetCustomAttribute(memberInfo, typeof(BTPropertyAttribute)) as BTPropertyAttribute; #endif return(attribute != null ? attribute.PropertyName : null); }
private static bool IsNullable(Type type) { return(TCU.GetTypeInfo(type).IsGenericType&& (typeof(Nullable <>).Equals(type.GetGenericTypeDefinition()))); }
private object CoerceList(Type targetType, Type arrayType, IEnumerable value) { if (targetType.IsArray) { return(this.CoerceArray(targetType.GetElementType(), value)); } // targetType serializes as a JSON array but is not an array // assume is an ICollection / IEnumerable with AddRange, Add, // or custom Constructor with which we can populate it // many ICollection types take an IEnumerable or ICollection // as a constructor argument. look through constructors for // a compatible match. ConstructorInfo[] ctors = targetType.GetConstructors(); ConstructorInfo defaultCtor = null; foreach (ConstructorInfo ctor in ctors) { ParameterInfo[] paramList = ctor.GetParameters(); if (paramList.Length == 0) { // save for in case cannot find closer match defaultCtor = ctor; continue; } if (paramList.Length == 1 && TCU.GetTypeInfo(paramList[0].ParameterType).IsAssignableFrom(TCU.GetTypeInfo(arrayType))) { try { // invoke first constructor that can take this value as an argument return(ctor.Invoke( new object[] { value } )); } catch { // there might exist a better match continue; } } } if (ConstructorInfo.Equals(defaultCtor, null)) { throw new JsonTypeCoercionException( String.Format(TypeCoercionUtility.ErrorDefaultCtor, new System.Object[] { targetType.FullName })); } object collection; try { // always try-catch Invoke() to expose real exception collection = defaultCtor.Invoke(null); } catch (TargetInvocationException ex) { if (ex.InnerException != null) { throw new JsonTypeCoercionException(ex.InnerException.Message, ex.InnerException); } throw new JsonTypeCoercionException("Error instantiating " + targetType.FullName, ex); } // many ICollection types have an AddRange method // which adds all items at once #if WINDOWS_STORE /** \todo Not sure if this finds the correct methods */ MethodInfo method = TCU.GetTypeInfo(targetType).GetDeclaredMethod("AddRange"); #else MethodInfo method = TCU.GetTypeInfo(targetType).GetMethod("AddRange"); #endif ParameterInfo[] parameters = (MethodInfo.Equals(method, null)) ? null : method.GetParameters(); Type paramType = (parameters == null || parameters.Length != 1) ? null : parameters[0].ParameterType; if (!Type.Equals(paramType, null) && TCU.GetTypeInfo(paramType).IsAssignableFrom(TCU.GetTypeInfo(arrayType))) { try { // always try-catch Invoke() to expose real exception // add all members in one method method.Invoke( collection, new object[] { value }); } catch (TargetInvocationException ex) { if (ex.InnerException != null) { throw new JsonTypeCoercionException(ex.InnerException.Message, ex.InnerException); } throw new JsonTypeCoercionException("Error calling AddRange on " + targetType.FullName, ex); } return(collection); } else { // many ICollection types have an Add method // which adds items one at a time #if WINDOWS_STORE /** \todo Not sure if this finds the correct methods */ method = TCU.GetTypeInfo(targetType).GetDeclaredMethod("Add"); #else method = TCU.GetTypeInfo(targetType).GetMethod("Add"); #endif parameters = (MethodInfo.Equals(method, null)) ? null : method.GetParameters(); paramType = (parameters == null || parameters.Length != 1) ? null : parameters[0].ParameterType; if (!Type.Equals(paramType, null)) { // loop through adding items to collection foreach (object item in value) { try { // always try-catch Invoke() to expose real exception method.Invoke( collection, new object[] { this.CoerceType(paramType, item) }); } catch (TargetInvocationException ex) { if (ex.InnerException != null) { throw new JsonTypeCoercionException(ex.InnerException.Message, ex.InnerException); } throw new JsonTypeCoercionException("Error calling Add on " + targetType.FullName, ex); } } return(collection); } } try { // fall back to basics return(Convert.ChangeType(value, targetType)); } catch (Exception ex) { throw new JsonTypeCoercionException(String.Format("Error converting {0} to {1}", new System.Object[] { value.GetType().FullName, targetType.FullName }), ex); } }
internal object CoerceType(Type targetType, object value) { bool isNullable = TypeCoercionUtility.IsNullable(targetType); if (value == null) { if (!allowNullValueTypes && TCU.GetTypeInfo(targetType).IsValueType&& !isNullable) { throw new JsonTypeCoercionException(String.Format(TypeCoercionUtility.ErrorNullValueType, new System.Object[] { targetType.FullName })); } return(value); } if (isNullable) { // nullable types have a real underlying struct Type[] genericArgs = targetType.GetGenericArguments(); if (genericArgs.Length == 1) { targetType = genericArgs[0]; } } Type actualType = value.GetType(); if (TCU.GetTypeInfo(targetType).IsAssignableFrom(TCU.GetTypeInfo(actualType))) { return(value); } if (TCU.GetTypeInfo(targetType).IsEnum) { if (value is String) { if (!Enum.IsDefined(targetType, value)) { // if isn't a defined value perhaps it is the JsonName foreach (FieldInfo field in TCU.GetTypeInfo(targetType).GetFields()) { string jsonName = BTPropertyAttribute.GetPropertyName(field); if (((string)value).Equals(jsonName)) { value = field.Name; break; } } } return(Enum.Parse(targetType, (string)value)); } else { value = this.CoerceType(Enum.GetUnderlyingType(targetType), value); return(Enum.ToObject(targetType, value)); } } // Value is of the wrong type and it has been deserialized as an IDictionary. // Previously coercion was supported // but this generally just caused more problems than it solved, so type hints are recommended now. // More specifically this can cause annoying problems with tags when a tag is referencing some object // for which there is no type info for (e.g the field has been removed), that data would have been // deserialized as an IDictionary, but when the tag gets the information it will try to coerce it // which will often fail horribly if (value is IDictionary) { return(null); // Dictionary<string, MemberInfo> memberMap; // return this.CoerceType(targetType, (IDictionary)value, out memberMap); } if (TCU.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(TCU.GetTypeInfo(targetType)) && TCU.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(TCU.GetTypeInfo(actualType))) { return(this.CoerceList(targetType, actualType, (IEnumerable)value)); } if (value is String) { if (Type.Equals(targetType, typeof(DateTime))) { DateTime date; if (DateTime.TryParse( (string)value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.RoundtripKind | DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.NoCurrentDateDefault, out date)) { return(date); } } else if (Type.Equals(targetType, typeof(Guid))) { // try-catch is pointless since will throw upon generic conversion return(new Guid((string)value)); } else if (Type.Equals(targetType, typeof(Char))) { if (((string)value).Length == 1) { return(((string)value)[0]); } } else if (Equals(targetType, typeof(Uri))) { Uri uri; if (Uri.TryCreate((string)value, UriKind.RelativeOrAbsolute, out uri)) { return(uri); } } else if (Type.Equals(targetType, typeof(Version))) { // try-catch is pointless since will throw upon generic conversion return(new Version((string)value)); } } else if (Type.Equals(targetType, typeof(TimeSpan))) { return(new TimeSpan((long)this.CoerceType(typeof(Int64), value))); } #if !WINPHONE_8 TypeConverter converter = TypeDescriptor.GetConverter(targetType); if (converter.CanConvertFrom(actualType)) { return(converter.ConvertFrom(value)); } converter = TypeDescriptor.GetConverter(actualType); if (converter.CanConvertTo(targetType)) { return(converter.ConvertTo(value, targetType)); } #endif try { // fall back to basics return(Convert.ChangeType(value, targetType)); } catch (Exception ex) { throw new JsonTypeCoercionException( String.Format("Error converting {0} to {1}", new System.Object[] { value.GetType().FullName, targetType.FullName }), ex); } }
/** Creates a member map for the type */ private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType) { Dictionary <string, MemberInfo> memberMap; if (this.MemberMapCache.TryGetValue(objectType, out memberMap)) { // map was stored in cache return(memberMap); } // create a new map memberMap = new Dictionary <string, MemberInfo>(); // load properties into property map Type tp = objectType; while (tp != null) { PropertyInfo[] properties = TCU.GetTypeInfo(tp).GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < properties.Length; i++) { PropertyInfo info = properties[i]; if (!info.CanRead || !info.CanWrite) { continue; } if (BTIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = BTPropertyAttribute.GetPropertyName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // load public fields into property map FieldInfo[] fields = TCU.GetTypeInfo(tp).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo info in fields) { if (!info.IsPublic && #if WINDOWS_STORE info.GetCustomAttribute <JsonMemberAttribute>(false) == null #else info.GetCustomAttributes(typeof(BTPropertyAttribute), false).Length == 0 #endif ) { continue; } if (BTIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = BTPropertyAttribute.GetPropertyName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } tp = tp.BaseType; } // store in cache for repeated usage this.MemberMapCache[objectType] = memberMap; return(memberMap); }
protected virtual void Write(object value, bool isProperty, Type fieldType = null, bool skipConverters = false) { if (isProperty && this.settings.PrettyPrint) { this.Writer.Write(' '); } if (value == null) { this.Writer.Write(JsonReader.LiteralNull); return; } if (value is IJsonSerializable && !skipConverters) { try { if (isProperty) { this.depth++; if (this.depth > this.settings.MaxDepth) { throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, new System.Object[] { this.settings.MaxDepth })); } this.WriteLine(); } ((IJsonSerializable)value).WriteJson(this); } finally { if (isProperty) { this.depth--; } } return; } // must test enumerations before value types if (value is Enum) { this.Write((Enum)value); return; } // Type.GetTypeCode() allows us to more efficiently switch type // plus cannot use 'is' for ValueTypes Type type = value.GetType(); #if WINDOWS_STORE if (Type.Equals(type, typeof(bool))) { this.Write((Boolean)value); return; } else if (Type.Equals(type, typeof(System.Int32))) { this.Write((Int32)value); return; } else if (Type.Equals(type, typeof(System.Single))) { this.Write((Single)value); return; } else if (Type.Equals(type, typeof(System.String))) { this.Write((String)value); return; } else if (Type.Equals(type, typeof(byte))) { this.Write((Byte)value); return; } else if (Type.Equals(type, typeof(char))) { this.Write((Char)value); return; } else if (Type.Equals(type, typeof(DateTime))) { this.Write((DateTime)value); return; } else if (Type.Equals(type, typeof(DBNull)) || Type.Equals(type, null)) { this.Writer.Write(JsonReader.LiteralNull); return; } else if (Type.Equals(type, typeof(Decimal))) { // From MSDN: // Conversions from Char, SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, and UInt64 // to Decimal are widening conversions that never lose information or throw exceptions. // Conversions from Single or Double to Decimal throw an OverflowException // if the result of the conversion is not representable as a Decimal. this.Write((Decimal)value); return; } else if (Type.Equals(type, typeof(double))) { this.Write((Double)value); return; } else if (Type.Equals(type, typeof(System.Int16))) { this.Write((Int16)value); return; } else if (Type.Equals(type, typeof(System.Int64))) { this.Write((Int64)value); return; } else if (Type.Equals(type, typeof(System.SByte))) { this.Write((SByte)value); return; } else if (Type.Equals(type, typeof(System.UInt16))) { this.Write((UInt16)value); return; } else if (Type.Equals(type, typeof(System.UInt32))) { this.Write((UInt32)value); return; } else if (Type.Equals(type, typeof(System.UInt64))) { this.Write((UInt64)value); return; } else { // all others must be explicitly tested } #else // Faster to switch on typecode, but Windows Store does not support it switch (TP.GetTypeCode(type)) { case TypeCode.Boolean: { this.Write((Boolean)value); return; } case TypeCode.Byte: { this.Write((Byte)value); return; } case TypeCode.Char: { this.Write((Char)value); return; } case TypeCode.DateTime: { this.Write((DateTime)value); return; } case TypeCode.DBNull: case TypeCode.Empty: { this.Writer.Write(JsonReader.LiteralNull); return; } case TypeCode.Decimal: { // From MSDN: // Conversions from Char, SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, and UInt64 // to Decimal are widening conversions that never lose information or throw exceptions. // Conversions from Single or Double to Decimal throw an OverflowException // if the result of the conversion is not representable as a Decimal. this.Write((Decimal)value); return; } case TypeCode.Double: { this.Write((Double)value); return; } case TypeCode.Int16: { this.Write((Int16)value); return; } case TypeCode.Int32: { this.Write((Int32)value); return; } case TypeCode.Int64: { this.Write((Int64)value); return; } case TypeCode.SByte: { this.Write((SByte)value); return; } case TypeCode.Single: { this.Write((Single)value); return; } case TypeCode.String: { this.Write((String)value); return; } case TypeCode.UInt16: { this.Write((UInt16)value); return; } case TypeCode.UInt32: { this.Write((UInt32)value); return; } case TypeCode.UInt64: { this.Write((UInt64)value); return; } default: case TypeCode.Object: { // all others must be explicitly tested break; } } #endif if (!skipConverters) { JsonConverter converter = this.Settings.GetConverter(type); if (converter != null) { // Return if converter serialized the object if (converter.Write(this, depth, type, value)) { return; } } } if (value is Guid) { this.Write((Guid)value); return; } if (value is Uri) { this.Write((Uri)value); return; } if (value is TimeSpan) { this.Write((TimeSpan)value); return; } if (value is Version) { this.Write((Version)value); return; } // IDictionary test must happen BEFORE IEnumerable test // since IDictionary implements IEnumerable if (value is IDictionary) { try { if (isProperty) { this.depth++; if (this.depth > this.settings.MaxDepth) { throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, new System.Object[] { this.settings.MaxDepth })); } this.WriteLine(); } this.WriteObject((IDictionary)value); } finally { if (isProperty) { this.depth--; } } return; } //if (!Type.Equals (TCU.GetTypeInfo(type).GetInterface (JsonReader.TypeGenericIDictionary), null)) if (TCU.GetTypeInfo(typeof(IDictionary)).IsAssignableFrom(TCU.GetTypeInfo(value.GetType()))) { try { if (isProperty) { this.depth++; if (this.depth > this.settings.MaxDepth) { throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, new System.Object[] { this.settings.MaxDepth })); } this.WriteLine(); } this.WriteDictionary((IEnumerable)value); } finally { if (isProperty) { this.depth--; } } return; } if (value is IList && type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>)) { // Is List<T> var memberType = type.GetGenericArguments() [0]; try { if (isProperty) { this.depth++; if (this.depth > this.settings.MaxDepth) { throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, new System.Object[] { this.settings.MaxDepth })); } this.WriteLine(); } this.WriteArray((IEnumerable)value, memberType); } finally { if (isProperty) { this.depth--; } } return; } // IDictionary test must happen BEFORE IEnumerable test // since IDictionary implements IEnumerable if (value is IEnumerable) { #if !UNITY3D if (value is XmlNode) { this.Write((System.Xml.XmlNode)value); return; } #endif try { if (isProperty) { this.depth++; if (this.depth > this.settings.MaxDepth) { throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, new System.Object[] { this.settings.MaxDepth })); } this.WriteLine(); } this.WriteArray((IEnumerable)value, type.GetElementType()); } finally { if (isProperty) { this.depth--; } } return; } // structs and classes try { if (isProperty) { this.depth++; if (this.depth > this.settings.MaxDepth) { throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, new System.Object[] { this.settings.MaxDepth })); } this.WriteLine(); } this.WriteObject(value, type, true, fieldType); } finally { if (isProperty) { this.depth--; } } }
protected virtual void WriteObject(object value, Type type, bool serializePrivate, Type fieldType = null) { bool appendDelim = false; if (settings.HandleCyclicReferences && !TCU.GetTypeInfo(type).IsValueType) { int prevIndex = 0; if (this.previouslySerializedObjects.TryGetValue(value, out prevIndex)) { this.Writer.Write(JsonReader.OperatorObjectStart); this.WriteObjectProperty("@ref", prevIndex); this.WriteLine(); this.Writer.Write(JsonReader.OperatorObjectEnd); return; } else { this.previouslySerializedObjects.Add(value, this.previouslySerializedObjects.Count); } } this.Writer.Write(JsonReader.OperatorObjectStart); this.depth++; if (this.depth > this.settings.MaxDepth) { throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, new System.Object[] { this.settings.MaxDepth })); } try { if (!String.IsNullOrEmpty(this.settings.TypeHintName) && (!settings.TypeHintsOnlyWhenNeeded || depth <= 1 || (!Type.Equals(fieldType, type) && !Type.Equals(fieldType, null)))) { if (appendDelim) { this.WriteObjectPropertyDelim(); } else { appendDelim = true; } this.WriteObjectProperty(this.settings.TypeHintName, type.FullName + ", " + type.Assembly.GetName().Name); } // Set tag so that other objects can find it when deserializing // Note that this must be set after the type hint is serialized // To make sure the fields are read correctly when deserializing if (referenceHandler != null && referenceHandler.IsHandled(type)) { if (appendDelim) { this.WriteObjectPropertyDelim(); } else { appendDelim = true; } this.WriteObjectProperty("@tag", referenceHandler.GetReferenceID(value)); // Notify the reference handler that this value has now been serialized referenceHandler.MarkAsSerialized(value); } //Console.WriteLine ("Anon " + anonymousType); //Console.WriteLine (type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Length + " " + type.GetProperties().Length); // serialize public properties KeyValuePair <string, FieldInfo>[] fields; KeyValuePair <string, PropertyInfo>[] properties; this.settings.Coercion.GetMemberWritingMap(type, settings, out fields, out properties); for (int j = 0; j < properties.Length; j++) { PropertyInfo property = properties[j].Value; object propertyValue = property.GetValue(value, null); if (this.IsDefaultValue(property, propertyValue)) { if (Settings.DebugMode) { Console.WriteLine("Cannot serialize " + property.Name + " : is default value"); } continue; } if (appendDelim) { this.WriteObjectPropertyDelim(); } else { appendDelim = true; } string name = properties[j].Key; bool ownedRef = referenceHandler == null || !referenceHandler.IsHandled(property.PropertyType) || referenceHandler.IsOwnedRef(property); if (ownedRef || propertyValue == null) { // Reference is owned by this property, serialize it as normal this.WriteObjectProperty(name, propertyValue, property.PropertyType); } else { // Reference is not owned by this property, write a reference to it this.WriteObjectProperty(name, "@" + referenceHandler.GetReferenceID(propertyValue), typeof(string)); } } for (int j = 0; j < fields.Length; j++) { FieldInfo field = fields[j].Value; object fieldValue = field.GetValue(value); if (this.IsDefaultValue(field, fieldValue)) { if (Settings.DebugMode) { Console.WriteLine("Cannot serialize " + field.Name + " : is default value"); } continue; } if (appendDelim) { this.WriteObjectPropertyDelim(); } else { appendDelim = true; } string name = fields[j].Key; bool ownedRef = referenceHandler == null || !referenceHandler.IsHandled(field.FieldType) || referenceHandler.IsOwnedRef(field); if (ownedRef || fieldValue == null) { // Reference is owned by this field, serialize it as normal this.WriteObjectProperty(name, fieldValue, field.FieldType); } else { // Reference is not owned by this field, write a reference to it this.WriteObjectProperty(name, "@" + referenceHandler.GetReferenceID(fieldValue), typeof(string)); } } } finally { this.depth--; } if (appendDelim) { this.WriteLine(); } this.Writer.Write(JsonReader.OperatorObjectEnd); }