Пример #1
0
        public void ExtensionDataGetterCanBeIteratedMultipleTimes()
        {
            DefaultContractResolver resolver = new DefaultContractResolver();
            JsonObjectContract      contract = (JsonObjectContract)resolver.ResolveContract(typeof(ClassWithExtensionData));

            ClassWithExtensionData myClass = new ClassWithExtensionData
            {
                Data = new Dictionary <string, object>
                {
                    { "SomeField", "Field" },
                }
            };

            ExtensionDataGetter getter = contract.ExtensionDataGetter;

            IEnumerable <KeyValuePair <object, object> > dictionaryData = getter(myClass).ToDictionary(kv => kv.Key, kv => kv.Value);

            Assert.IsTrue(dictionaryData.Any());
            Assert.IsTrue(dictionaryData.Any());

            IEnumerable <KeyValuePair <object, object> > extensionData = getter(myClass);

            Assert.IsTrue(extensionData.Any());
            Assert.IsTrue(extensionData.Any()); // second test fails if the enumerator returned isn't reset
        }
Пример #2
0
    // Token: 0x06000997 RID: 2455 RVA: 0x000372A0 File Offset: 0x000354A0
    private void method_19(JsonWriter jsonWriter_0, object object_0, JsonObjectContract jsonObjectContract_0, JsonProperty jsonProperty_0, JsonContainerContract jsonContainerContract_0, JsonProperty jsonProperty_1)
    {
        this.method_17(jsonWriter_0, jsonObjectContract_0, object_0);
        this.list_0.Add(object_0);
        this.method_21(jsonWriter_0, object_0, jsonObjectContract_0, jsonProperty_0, jsonContainerContract_0, jsonProperty_1);
        int top = jsonWriter_0.Top;

        for (int i = 0; i < jsonObjectContract_0.Properties.Count; i++)
        {
            JsonProperty jsonProperty = jsonObjectContract_0.Properties[i];
            try
            {
                JsonContract jsonContract_;
                object       object_;
                if (this.method_20(jsonWriter_0, object_0, jsonObjectContract_0, jsonProperty_0, jsonProperty, out jsonContract_, out object_))
                {
                    jsonProperty.method_3(jsonWriter_0);
                    this.method_9(jsonWriter_0, object_, jsonContract_, jsonProperty, jsonObjectContract_0, jsonProperty_0);
                }
            }
            catch (Exception exception_)
            {
                if (!base.method_4(object_0, jsonObjectContract_0, jsonProperty.PropertyName, null, jsonWriter_0.xEuXaafal7(), exception_))
                {
                    throw;
                }
                this.method_39(jsonWriter_0, top);
            }
        }
        ExtensionDataGetter extensionDataGetter = jsonObjectContract_0.ExtensionDataGetter;
        IEnumerable <KeyValuePair <object, object> > enumerable = (extensionDataGetter != null) ? extensionDataGetter(object_0) : null;

        if (enumerable != null)
        {
            foreach (KeyValuePair <object, object> keyValuePair in enumerable)
            {
                JsonContract jsonContract_2 = this.method_7(keyValuePair.Key);
                JsonContract jsonContract_3 = this.method_7(keyValuePair.Value);
                bool         flag;
                string       text = this.method_38(jsonWriter_0, keyValuePair.Key, jsonContract_2, out flag);
                text = ((jsonObjectContract_0.ExtensionDataNameResolver != null) ? jsonObjectContract_0.ExtensionDataNameResolver(text) : text);
                if (this.method_11(keyValuePair.Value, null, jsonContract_3, jsonObjectContract_0, jsonProperty_0))
                {
                    jsonWriter_0.WritePropertyName(text);
                    this.method_14(jsonWriter_0, keyValuePair.Value);
                }
                else if (this.method_13(jsonWriter_0, keyValuePair.Value, null, jsonContract_3, jsonObjectContract_0, jsonProperty_0))
                {
                    jsonWriter_0.WritePropertyName(text);
                    this.method_9(jsonWriter_0, keyValuePair.Value, jsonContract_3, null, jsonObjectContract_0, jsonProperty_0);
                }
            }
        }
        jsonWriter_0.WriteEndObject();
        this.list_0.RemoveAt(this.list_0.Count - 1);
        this.method_18(jsonWriter_0, jsonObjectContract_0, object_0);
    }
 public static ClearExtensionData RegisterExtensionDataGetter(ExtensionDataGetter getter)
 {
     _currentExtensionGetter += getter;
     return(new ClearExtensionData(null, getter));
 }
 public ClearExtensionData(ExtensionDataSetter setter, ExtensionDataGetter getter)
 {
     _setter = setter;
     _getter = getter;
 }
Пример #5
0
        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;
            }
        }