public IDisposable RegisterForExtensionData(ExtensionDataSetter setter) { if (currentExtensionData != null) throw new InvalidOperationException("Cannot add a data setter because on is already added"); currentExtensionData = setter; return clearExtensionData; }
/// <inheritdoc cref="DefaultContractResolver.ResolveContract(Type)"/> public override JsonContract ResolveContract(Type type) { JsonContract contract = base.ResolveContract(type); if (contract is JsonObjectContract objContract) { if (objContract.ExtensionDataSetter != null) { ExtensionDataSetter oldSetter = objContract.ExtensionDataSetter; objContract.ExtensionDataSetter = (o, key, value) => { if (whitelist.Contains(key)) { oldSetter(o, key, value); } else { int a = 0; a++; } }; } } return(contract); }
public IDisposable RegisterForExtensionData(ExtensionDataSetter setter) { if (currentExtensionData != null) { throw new InvalidOperationException("Cannot add a data setter because on is already added"); } currentExtensionData = setter; return(clearExtensionData); }
/// <summary> /// Converts a BlittableJsonReaderObject to an entity. /// </summary> /// <param name="entityType"></param> /// <param name="id">The id.</param> /// <param name="document">The document found.</param> /// <returns>The converted entity</returns> public object ConvertToEntity(Type entityType, string id, ref BlittableJsonReaderObject document, bool trackEntity) { try { if (entityType == typeof(BlittableJsonReaderObject)) { return(document); } _session.OnBeforeConversionToEntityInvoke(id, entityType, ref document); var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(entityType); var entity = defaultValue; ExtensionDataSetter dataSetter = null; if (trackEntity) { dataSetter = RegisterMissingProperties; } using (DefaultRavenContractResolver.RegisterExtensionDataSetter(dataSetter)) { var documentType = _session.Conventions.GetClrType(id, document); if (documentType != null) { var type = Type.GetType(documentType); if (type != null && entityType.IsAssignableFrom(type)) { entity = _session.Conventions.DeserializeEntityFromBlittable(type, document); } } if (Equals(entity, defaultValue)) { entity = _session.Conventions.DeserializeEntityFromBlittable(entityType, document); } } if (id != null) { _session.GenerateEntityIdOnTheClient.TrySetIdentity(entity, id); } _session.OnAfterConversionToEntityInvoke(id, document, entity); return(entity); } catch (Exception ex) { throw new InvalidOperationException($"Could not convert document {id} to entity of type {entityType}", ex); } }
private ExtensionDataSetter method_3(Type type_0) { ExtensionDataSetter setter = null; foreach (Type type in this.method_10(type_0)) { IList <MemberInfo> list = new List <MemberInfo>(); list.smethod_1 <MemberInfo>(type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)); list.smethod_1 <MemberInfo>(type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)); using (IEnumerator <MemberInfo> enumerator2 = list.GetEnumerator()) { MemberInfo current; while (enumerator2.MoveNext()) { current = enumerator2.Current; MemberTypes types = current.smethod_1(); if (((types == MemberTypes.Property) || (types == MemberTypes.Field)) && current.IsDefined(typeof(JsonExtensionDataAttribute), false)) { Type type3; Type type2 = Class194.smethod_20(current); if (!Class194.smethod_14(type2, typeof(IDictionary <,>), out type3)) { goto Label_01AF; } Type type4 = type3.GetGenericArguments()[0]; Type type5 = type3.GetGenericArguments()[1]; if (Class194.smethod_12(type2, typeof(IDictionary <,>))) { type2 = typeof(Dictionary <,>).MakeGenericType(new Type[] { type4, type5 }); } if (!type4.IsAssignableFrom(typeof(string)) || !type5.IsAssignableFrom(typeof(JToken))) { goto Label_01AF; } Class126 class2 = new Class126(); MethodInfo method = type2.GetMethod("Add", new Type[] { type4, type5 }); class2.func_0 = Class139.Class166_0.method_0 <object>(current); class2.action_0 = Class139.Class166_0.method_1 <object>(current); class2.func_1 = Class139.Class166_0.ns20.Class166. <object>(type2); class2.delegate3_0 = Class139.Class166_0.ns20.Class166. <object>(method); setter = new ExtensionDataSetter(class2.method_0); } } continue; Label_01AF: throw new JsonException("Invalid extension data attribute on '{0}'. Member '{1}' type must implement IDictionary<string, JToken>.".smethod_1(CultureInfo.InvariantCulture, smethod_4(current.DeclaringType), current.Name)); } } return(setter); }
public static ClearExtensionData RegisterExtensionDataSetter(ExtensionDataSetter setter) { _currentExtensionSetter += setter; return(new ClearExtensionData(setter, null)); }
public ClearExtensionData(ExtensionDataSetter setter, ExtensionDataGetter getter) { _setter = setter; _getter = getter; }
private static void SetExtensionDataDelegates(JsonObjectContract contract, MemberInfo member) { JsonExtensionDataAttribute attribute = ReflectionUtils.GetAttribute <JsonExtensionDataAttribute>(member); if (attribute == null) { return; } Type type = ReflectionUtils.GetMemberUnderlyingType(member); Type type2; ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary <, >), out type2); Type type3 = type2.GetGenericArguments()[0]; Type type4 = type2.GetGenericArguments()[1]; bool isJTokenValueType = typeof(JToken).IsAssignableFrom(type4); if (ReflectionUtils.IsGenericDefinition(type, typeof(IDictionary <, >))) { type = typeof(Dictionary <, >).MakeGenericType(new Type[] { type3, type4 }); } MethodInfo method = type.GetMethod("Add", new Type[] { type3, type4 }); Func <object, object> getExtensionDataDictionary = JsonTypeReflector.ReflectionDelegateFactory.CreateGet <object>(member); Action <object, object> setExtensionDataDictionary = JsonTypeReflector.ReflectionDelegateFactory.CreateSet <object>(member); Func <object> createExtensionDataDictionary = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor <object>(type); MethodCall <object, object> setExtensionDataDictionaryValue = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall <object>(method); ExtensionDataSetter extensionDataSetter = delegate(object o, string key, object value) { object obj = getExtensionDataDictionary(o); if (obj == null) { obj = createExtensionDataDictionary(); setExtensionDataDictionary(o, obj); } if (isJTokenValueType && !(value is JToken)) { value = JToken.FromObject(value); } setExtensionDataDictionaryValue(obj, new object[] { key, value }); }; Type type5 = typeof(DefaultContractResolver.DictionaryEnumerator <, >).MakeGenericType(new Type[] { type3, type4 }); ConstructorInfo method2 = type5.GetConstructors().First <ConstructorInfo>(); MethodCall <object, object> createEnumerableWrapper = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall <object>(method2); ExtensionDataGetter extensionDataGetter = delegate(object o) { object obj = getExtensionDataDictionary(o); if (obj == null) { return(null); } return((IEnumerable <KeyValuePair <object, object> >)createEnumerableWrapper(null, new object[] { obj })); }; if (attribute.ReadData) { contract.ExtensionDataSetter = extensionDataSetter; } if (attribute.WriteData) { contract.ExtensionDataGetter = extensionDataGetter; } }
public object FromBlittable(Type type, ref BlittableJsonReaderObject json, string id, bool trackEntity) { try { if (type == typeof(BlittableJsonReaderObject)) { return(json); } _session.OnBeforeConversionToEntityInvoke(id, type, ref json); var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(type); var entity = defaultValue; ExtensionDataSetter dataSetter = null; if (trackEntity) { dataSetter = RegisterMissingProperties; } using (DefaultRavenContractResolver.RegisterExtensionDataSetter(dataSetter)) { var documentTypeAsString = _session.Conventions.GetClrType(id, json); if (documentTypeAsString != null) { var documentType = Type.GetType(documentTypeAsString); if (documentType != null && type.IsAssignableFrom(documentType)) { entity = _session.Conventions.Serialization.DeserializeEntityFromBlittable(documentType, json); } } if (Equals(entity, defaultValue)) { entity = _session.Conventions.Serialization.DeserializeEntityFromBlittable(type, json); } } if (id != null) { _session.GenerateEntityIdOnTheClient.TrySetIdentity(entity, id); } _session.OnAfterConversionToEntityInvoke(id, json, entity); return(entity); } catch (Exception ex) { string jsonAsString = TryReadBlittableAsString(json); throw new InvalidOperationException($"Could not convert document {id} to entity of type {type}. Json: {jsonAsString}", ex); } string TryReadBlittableAsString(BlittableJsonReaderObject jsonToRead) { var jsString = string.Empty; using (var memoryStream = new MemoryStream()) { try { jsonToRead.WriteJsonToAsync(memoryStream).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); } catch { // ignore } memoryStream.Position = 0; try { using (var sr = new StreamReader(memoryStream)) { jsString = sr.ReadToEnd(); } } catch { // ignore } } return(jsString); } }