コード例 #1
0
        private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(values.UnderlyingDictionary, base.Serializer.Context);
            SerializeStack.Add(values.UnderlyingDictionary);
            writer.WriteStartObject();
            bool?isReference = contract.IsReference;

            if ((!isReference.HasValue) ? HasFlag(base.Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects) : isReference.Value)
            {
                writer.WritePropertyName("$id");
                writer.WriteValue(base.Serializer.ReferenceResolver.GetReference(this, values.UnderlyingDictionary));
            }
            if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
            {
                WriteTypeProperty(writer, values.UnderlyingDictionary.GetType());
            }
            JsonContract          collectionValueContract2 = base.Serializer.ContractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object));
            int                   top        = writer.Top;
            IDictionaryEnumerator enumerator = values.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    DictionaryEntry entry        = (DictionaryEntry)enumerator.Current;
                    string          propertyName = GetPropertyName(entry);
                    propertyName = ((contract.PropertyNameResolver == null) ? propertyName : contract.PropertyNameResolver(propertyName));
                    try
                    {
                        object       value        = entry.Value;
                        JsonContract contractSafe = GetContractSafe(value);
                        if (ShouldWriteReference(value, null, contractSafe))
                        {
                            writer.WritePropertyName(propertyName);
                            WriteReference(writer, value);
                        }
                        else if (CheckForCircularReference(value, null, contract))
                        {
                            writer.WritePropertyName(propertyName);
                            SerializeValue(writer, value, contractSafe, null, collectionValueContract2);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!IsErrorHandled(values.UnderlyingDictionary, contract, propertyName, ex))
                        {
                            throw;
                        }
                        HandleError(writer, top);
                    }
                }
            }
            finally
            {
                (enumerator as IDisposable)?.Dispose();
            }
            writer.WriteEndObject();
            SerializeStack.RemoveAt(SerializeStack.Count - 1);
            contract.InvokeOnSerialized(values.UnderlyingDictionary, base.Serializer.Context);
        }
コード例 #2
0
        private object CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, ConstructorInfo constructorInfo, string id)
        {
            ValidationUtils.ArgumentNotNull(constructorInfo, "constructorInfo");
            Type underlyingType = contract.UnderlyingType;
            IDictionary <JsonProperty, object>  dictionary  = ResolvePropertyAndConstructorValues(contract, reader, underlyingType);
            IDictionary <ParameterInfo, object> dictionary2 = ((IEnumerable <ParameterInfo>)constructorInfo.GetParameters()).ToDictionary((Func <ParameterInfo, ParameterInfo>)((ParameterInfo p) => p), (Func <ParameterInfo, object>)((ParameterInfo p) => null));
            IDictionary <JsonProperty, object>  dictionary3 = new Dictionary <JsonProperty, object>();

            foreach (KeyValuePair <JsonProperty, object> item in dictionary)
            {
                ParameterInfo key = dictionary2.ForgivingCaseSensitiveFind((KeyValuePair <ParameterInfo, object> kv) => kv.Key.Name, item.Key.UnderlyingName).Key;
                if (key != null)
                {
                    dictionary2[key] = item.Value;
                }
                else
                {
                    dictionary3.Add(item);
                }
            }
            object obj = constructorInfo.Invoke(dictionary2.Values.ToArray());

            if (id != null)
            {
                base.Serializer.ReferenceResolver.AddReference(this, id, obj);
            }
            contract.InvokeOnDeserializing(obj, base.Serializer.Context);
            foreach (KeyValuePair <JsonProperty, object> item2 in dictionary3)
            {
                JsonProperty key2  = item2.Key;
                object       value = item2.Value;
                if (ShouldSetPropertyValue(item2.Key, item2.Value))
                {
                    key2.ValueProvider.SetValue(obj, value);
                }
                else if (!key2.Writable && value != null)
                {
                    JsonContract jsonContract = base.Serializer.ContractResolver.ResolveContract(key2.PropertyType);
                    if (jsonContract is JsonArrayContract)
                    {
                        JsonArrayContract jsonArrayContract = jsonContract as JsonArrayContract;
                        object            value2            = key2.ValueProvider.GetValue(obj);
                        if (value2 != null)
                        {
                            IWrappedCollection wrappedCollection  = jsonArrayContract.CreateWrapper(value2);
                            IWrappedCollection wrappedCollection2 = jsonArrayContract.CreateWrapper(value);
                            IEnumerator        enumerator3        = wrappedCollection2.GetEnumerator();
                            try
                            {
                                while (enumerator3.MoveNext())
                                {
                                    object current3 = enumerator3.Current;
                                    wrappedCollection.Add(current3);
                                }
                            }
                            finally
                            {
                                IDisposable disposable;
                                if ((disposable = (enumerator3 as IDisposable)) != null)
                                {
                                    disposable.Dispose();
                                }
                            }
                        }
                    }
                    else if (jsonContract is JsonDictionaryContract)
                    {
                        JsonDictionaryContract jsonDictionaryContract = jsonContract as JsonDictionaryContract;
                        object value3 = key2.ValueProvider.GetValue(obj);
                        if (value3 != null)
                        {
                            IWrappedDictionary    wrappedDictionary  = jsonDictionaryContract.CreateWrapper(value3);
                            IWrappedDictionary    wrappedDictionary2 = jsonDictionaryContract.CreateWrapper(value);
                            IDictionaryEnumerator enumerator4        = wrappedDictionary2.GetEnumerator();
                            try
                            {
                                while (enumerator4.MoveNext())
                                {
                                    DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator4.Current;
                                    wrappedDictionary.Add(dictionaryEntry.Key, dictionaryEntry.Value);
                                }
                            }
                            finally
                            {
                                IDisposable disposable2;
                                if ((disposable2 = (enumerator4 as IDisposable)) != null)
                                {
                                    disposable2.Dispose();
                                }
                            }
                        }
                    }
                }
            }
            contract.InvokeOnDeserialized(obj, base.Serializer.Context);
            return(obj);
        }
        private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(values.UnderlyingDictionary, base.Serializer.Context);
            this.SerializeStack.Add(values.UnderlyingDictionary);
            writer.WriteStartObject();
            bool?isReference = contract.IsReference;
            bool flag        = (!isReference.get_HasValue()) ? this.HasFlag(base.Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects) : isReference.get_Value();

            if (flag)
            {
                writer.WritePropertyName("$id");
                writer.WriteValue(base.Serializer.ReferenceResolver.GetReference(this, values.UnderlyingDictionary));
            }
            if (this.ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
            {
                this.WriteTypeProperty(writer, values.UnderlyingDictionary.GetType());
            }
            JsonContract          collectionValueContract2 = base.Serializer.ContractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object));
            int                   top        = writer.Top;
            IDictionaryEnumerator enumerator = values.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    DictionaryEntry entry = (DictionaryEntry)enumerator.get_Current();
                    string          text  = this.GetPropertyName(entry);
                    text = ((contract.PropertyNameResolver == null) ? text : contract.PropertyNameResolver.Invoke(text));
                    try
                    {
                        object       value        = entry.get_Value();
                        JsonContract contractSafe = this.GetContractSafe(value);
                        if (this.ShouldWriteReference(value, null, contractSafe))
                        {
                            writer.WritePropertyName(text);
                            this.WriteReference(writer, value);
                        }
                        else if (this.CheckForCircularReference(value, default(ReferenceLoopHandling?), contract))
                        {
                            writer.WritePropertyName(text);
                            this.SerializeValue(writer, value, contractSafe, null, collectionValueContract2);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!base.IsErrorHandled(values.UnderlyingDictionary, contract, text, ex))
                        {
                            throw;
                        }
                        this.HandleError(writer, top);
                    }
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            writer.WriteEndObject();
            this.SerializeStack.RemoveAt(this.SerializeStack.get_Count() - 1);
            contract.InvokeOnSerialized(values.UnderlyingDictionary, base.Serializer.Context);
        }