public IList<FilterDef> FullTextFilters(Type type) { return AttributeUtil .GetAttributes<FullTextFilterDefAttribute>(type, false) .Select(CreateFilterDefinition) .ToList(); }
public DocumentMapping(Type mappedClass) { this.MappedClass = mappedClass; this.ClassBridges = new List<ClassBridgeMapping>(); this.Fields = new List<FieldMapping>(); this.Embedded = new List<EmbeddedMapping>(); this.ContainedIn = new List<ContainedInMapping>(); this.FullTextFilterDefinitions = new List<FilterDef>(); }
public ObjectSubclassInfo(Type type, ConstructorInfo constructor) { TypeInfo = type.GetTypeInfo(); ClassName = GetClassName(TypeInfo); Constructor = constructor; PropertyMappings = type.GetProperties() .Select(prop => Tuple.Create(prop, prop.GetCustomAttribute<ParseFieldNameAttribute>(true))) .Where(t => t.Item2 != null) .Select(t => Tuple.Create(t.Item1, t.Item2.FieldName)) .ToDictionary(t => t.Item1.Name, t => t.Item2); }
public bool IsTypeValid(String className, Type type) { ObjectSubclassInfo subclassInfo = null; mutex.EnterReadLock(); registeredSubclasses.TryGetValue(className, out subclassInfo); mutex.ExitReadLock(); return subclassInfo == null ? type == typeof(ParseObject) : subclassInfo.TypeInfo == type.GetTypeInfo(); }
public EntityPropertyMappingInfo(EntityPropertyMappingAttribute attribute, Type definingType, ClientType actualPropertyType) { Debug.Assert(attribute != null, "attribute != null"); Debug.Assert(definingType != null, "definingType != null"); Debug.Assert(actualPropertyType != null, "actualPropertyType != null"); this.attribute = attribute; this.definingType = definingType; this.actualPropertyType = actualPropertyType; Debug.Assert(!string.IsNullOrEmpty(attribute.SourcePath), "Invalid source path"); this.segmentedSourcePath = attribute.SourcePath.Split('/'); }
public void RegisterSubclass(Type type) { TypeInfo typeInfo = type.GetTypeInfo(); if (!typeInfo.IsSubclassOf(typeof(ParseObject))) { throw new ArgumentException("Cannot register a type that is not a subclass of ParseObject"); } String className = ObjectSubclassInfo.GetClassName(typeInfo); try { // Perform this as a single independent transaction, so we can never get into an // intermediate state where we *theoretically* register the wrong class due to a // TOCTTOU bug. mutex.EnterWriteLock(); ObjectSubclassInfo previousInfo = null; if (registeredSubclasses.TryGetValue(className, out previousInfo)) { if (typeInfo.IsAssignableFrom(previousInfo.TypeInfo)) { // Previous subclass is more specific or equal to the current type, do nothing. return; } else if (previousInfo.TypeInfo.IsAssignableFrom(typeInfo)) { // Previous subclass is parent of new child, fallthrough and actually register // this class. /* Do nothing */ } else { throw new ArgumentException( "Tried to register both " + previousInfo.TypeInfo.FullName + " and " + typeInfo.FullName + " as the ParseObject subclass of " + className + ". Cannot determine the right class " + "to use because neither inherits from the other." ); } } ConstructorInfo constructor = type.FindConstructor(); if (constructor == null) { throw new ArgumentException("Cannot register a type that does not implement the default constructor!"); } registeredSubclasses[className] = new ObjectSubclassInfo(type, constructor); } finally { mutex.ExitWriteLock(); } Action toPerform = null; if (registerActions.TryGetValue(className, out toPerform)) { toPerform(); } }
internal Object InstantiateObject(Type objectType) { /*if (TCU.GetTypeInfo(objectType).IsInterface || TCU.GetTypeInfo(objectType).IsAbstract || TCU.GetTypeInfo(objectType).IsValueType) { throw new JsonTypeCoercionException( String.Format(TypeCoercionUtility.ErrorCannotInstantiate, new System.Object[] {objectType.FullName})); } ConstructorInfo ctor = objectType.GetConstructor(Type.EmptyTypes); if (ConstructorInfo.Equals (ctor, null)) { throw new JsonTypeCoercionException ( String.Format (TypeCoercionUtility.ErrorDefaultCtor, new System.Object[] { objectType.FullName })); } Object result; try { // always try-catch Invoke() to expose real exception result = ctor.Invoke(null); } catch (TargetInvocationException ex) { if (ex.InnerException != null) { throw new JsonTypeCoercionException(ex.InnerException.Message, ex.InnerException); } throw new JsonTypeCoercionException("Error instantiating " + objectType.FullName, ex); }*/ return System.Activator.CreateInstance (objectType); //return result; }
public IList<IClassBridgeDefinition> ClassBridges(Type type) { return AttributeUtil.GetAttributes<ClassBridgeAttribute>(type); }
public IIndexedDefinition Indexed(Type type) { return AttributeUtil.GetAttribute<IndexedAttribute>(type); }
public abstract Dictionary<string,object> WriteJson (Type type, object value);
public void Write (JsonWriter writer, Type type, object value) { Dictionary<string,object> dict = WriteJson (type,value); writer.Write (dict); }
/** Returns the converter for the specified type */ public virtual JsonConverter GetConverter (Type type) { for (int i=0;i<converters.Count;i++) if (converters[i].CanConvert (type)) return converters[i]; return null; }
internal object CoerceType(Type targetType, object value) { bool isNullable = TypeCoercionUtility.IsNullable(targetType); if (value == null) { if (!allowNullValueTypes && TypeCoercionUtility.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 (TypeCoercionUtility.GetTypeInfo(targetType).IsAssignableFrom(TypeCoercionUtility.GetTypeInfo(actualType))) { return value; } if (TypeCoercionUtility.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 TypeCoercionUtility.GetTypeInfo(targetType).GetFields()) { string jsonName = JsonNameAttribute.GetJsonName(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); } } if (value is IDictionary) { Dictionary<string, MemberInfo> memberMap; return this.CoerceType(targetType, (IDictionary)value, out memberMap); } if (TypeCoercionUtility.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(TypeCoercionUtility.GetTypeInfo(targetType)) && TypeCoercionUtility.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(TypeCoercionUtility.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 (targetType == typeof(int)) { unchecked { if (value is long) { value = (int)((long)value % int.MaxValue); } else if (!(value is string)) { value = Convert.ToInt32(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); } }
/// <summary> /// Helper method to set value of either property or field /// </summary> /// <param name="result"></param> /// <param name="memberType"></param> /// <param name="memberInfo"></param> /// <param name="value"></param> internal void SetMemberValue(Object result, Type memberType, MemberInfo memberInfo, object value) { try { if (memberInfo is PropertyInfo) { // set value of public property ((PropertyInfo) memberInfo).SetValue( result, this.CoerceType(memberType, value), null); } else if (memberInfo is FieldInfo) { // set value of public field ((FieldInfo) memberInfo).SetValue( result, this.CoerceType(memberType, value)); } } catch (Exception e) { throw; } // all other values are ignored }
/** 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 = TypeCoercionUtility.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 (JsonIgnoreAttribute.IsJsonIgnore (info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName (info); if (String.IsNullOrEmpty (jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // load public fields into property map FieldInfo[] fields = TypeCoercionUtility.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(JsonMemberAttribute), false).Length == 0 #endif ) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore (info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName (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; }
/** 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 (TypeCoercionUtility.GetTypeInfo(typeof(IDictionary)).IsAssignableFrom(TypeCoercionUtility.GetTypeInfo(objectType))) { return null; } else { return this.CreateMemberMap(objectType); } }
public void GetMemberWritingMap (Type objectType, JsonWriterSettings settings, out KeyValuePair<string,FieldInfo>[] outFields, out KeyValuePair<string,PropertyInfo>[] outProps) { if ( writingMaps == null ) { writingMaps = new Dictionary<Type,KeyValuePair< KeyValuePair<string,FieldInfo>[] , KeyValuePair<string,PropertyInfo>[] >> (); } KeyValuePair< KeyValuePair<string,FieldInfo>[] , KeyValuePair<string,PropertyInfo>[]> pair; if (writingMaps.TryGetValue (objectType, out pair)) { outFields = pair.Key; outProps = pair.Value; return; } bool anonymousType = objectType.IsGenericType && objectType.Name.StartsWith(JsonWriter.AnonymousTypePrefix); Type tp = objectType; if (fieldList == null) fieldList = new List<KeyValuePair<string, FieldInfo>> (); if (propList == null) propList = new List<KeyValuePair<string, PropertyInfo>> (); fieldList.Clear (); propList.Clear (); while (tp != null) { FieldInfo[] fields = tp.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); for (int j = 0; j < fields.Length; j++ ) { FieldInfo field = fields[j]; if (field.IsStatic || (!field.IsPublic && field.GetCustomAttributes (typeof(JsonMemberAttribute), true).Length == 0)) { //if (Settings.DebugMode) // Console.WriteLine ("Cannot serialize " + field.Name + " : not public or is static (and does not have a JsonMember attribute)"); continue; } if (settings.IsIgnored (objectType, field, null)) { //if (Settings.DebugMode) // Console.WriteLine ("Cannot serialize " + field.Name + " : ignored by settings"); continue; } // use Attributes here to control naming string fieldName = JsonNameAttribute.GetJsonName (field); if (String.IsNullOrEmpty (fieldName)) fieldName = field.Name; fieldList.Add (new KeyValuePair<string, FieldInfo> (fieldName, field)); } PropertyInfo[] properties = tp.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); for (int j = 0; j < properties.Length; j++ ) { PropertyInfo property = properties[j]; //Console.WriteLine (property.Name); if (!property.CanRead) { //if (Settings.DebugMode) // Console.WriteLine ("Cannot serialize "+property.Name+" : cannot read"); continue; } if (!property.CanWrite && !anonymousType) { //if (Settings.DebugMode) // Console.WriteLine ("Cannot serialize "+property.Name+" : cannot write"); continue; } if (settings.IsIgnored(objectType, property, null)) { //if (Settings.DebugMode) // Console.WriteLine ("Cannot serialize "+property.Name+" : is ignored by settings"); continue; } if (property.GetIndexParameters ().Length != 0) { //if (Settings.DebugMode) // Console.WriteLine ("Cannot serialize "+property.Name+" : is indexed"); continue; } // use Attributes here to control naming string propertyName = JsonNameAttribute.GetJsonName(property); if (String.IsNullOrEmpty(propertyName)) propertyName = property.Name; propList.Add ( new KeyValuePair<string, PropertyInfo>(propertyName, property)); } tp = tp.BaseType; } outFields = fieldList.ToArray(); outProps = propList.ToArray(); pair = new KeyValuePair< KeyValuePair<string,FieldInfo>[] , KeyValuePair<string,PropertyInfo>[]> ( outFields, outProps ); writingMaps[objectType] = pair; }
internal Object InstantiateObject(Type objectType, out Dictionary<string, MemberInfo> memberMap) { Object o = InstantiateObject (objectType); memberMap = GetMemberMap (objectType); return o; }
public void UnregisterSubclass(Type type) { mutex.EnterWriteLock(); registeredSubclasses.Remove(GetClassName(type)); mutex.ExitWriteLock(); }
private object CoerceType(Type targetType, IDictionary value, out Dictionary<string, MemberInfo> memberMap) { object newValue = this.InstantiateObject(targetType, out memberMap); if (memberMap != null) { // copy any values into new object foreach (object key in value.Keys) { MemberInfo memberInfo; Type memberType = TypeCoercionUtility.GetMemberInfo(memberMap, key as String, out memberInfo); this.SetMemberValue(newValue, memberType, memberInfo, value[key]); } } return newValue; }
/** Test if this converter can convert the specified type */ public abstract bool CanConvert (Type t);
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 && TypeCoercionUtility.GetTypeInfo(paramList[0].ParameterType).IsAssignableFrom(TypeCoercionUtility.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 = TypeCoercionUtility.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) && TypeCoercionUtility.GetTypeInfo(paramType).IsAssignableFrom (TypeCoercionUtility.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 = TypeCoercionUtility.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); } }
public object Read (JsonReader reader, Type type, Dictionary<string,object> value) { return ReadJson (type, value); }
private Array CoerceArray(Type elementType, IEnumerable value) { //ArrayList target = new ArrayList(); int count = 0; foreach (object item in value) { count++; } Array arr = Array.CreateInstance (elementType, new int[] {count}); int i=0; foreach (object item in value) { //target.Add(this.CoerceType(elementType, item)); arr.SetValue ( this.CoerceType(elementType, item), new int[] {i} ); i++; } return arr;//target.ToArray(elementType); }
public abstract object ReadJson (Type type, Dictionary<string,object> value);
private static bool IsNullable(Type type) { return TypeCoercionUtility.GetTypeInfo(type).IsGenericType && (typeof(Nullable<>).Equals (type.GetGenericTypeDefinition())); }
/// <summary> /// If a Type Hint is present then this method attempts to /// use it and move any previously parsed data over. /// </summary> /// <param name="result">the previous result</param> /// <param name="typeInfo">the type info string to use</param> /// <param name="objectType">reference to the objectType</param> /// <param name="memberMap">reference to the memberMap</param> /// <returns></returns> internal object ProcessTypeHint( IDictionary result, string typeInfo, out Type objectType, out Dictionary<string, MemberInfo> memberMap) { if (String.IsNullOrEmpty(typeInfo)) { objectType = null; memberMap = null; return result; } Type hintedType = Type.GetType(typeInfo, false); if (Type.Equals (hintedType, null)) { objectType = null; memberMap = null; return result; } objectType = hintedType; return this.CoerceType(hintedType, result, out memberMap); }
public String GetClassName(Type type) { return ObjectSubclassInfo.GetClassName(type.GetTypeInfo()); }
/// <summary> /// Deserializes a data object of Type <param name="type">type</param> from the <param name="input">input</param> /// </summary> /// <param name="output"></param> /// <param name="data"></param> public object Deserialize(TextReader input, Type type) { return new JsonReader(input, this.Settings).Deserialize(type); }
internal object ProcessTypeHint ( object result, string typeInfo, out Type objectType, out Dictionary<string, MemberInfo> memberMap ) { Type hintedType = Type.GetType(typeInfo, false); if (Type.Equals (hintedType, null)) { objectType = null; memberMap = null; return result; } objectType = hintedType; if (result != null && Type.Equals (result.GetType (), hintedType)) { memberMap = GetMemberMap(objectType); return result; } return InstantiateObject (objectType, out memberMap); }