Esempio n. 1
0
        private Schema CreateDictionarySchema(JsonDictionaryContract dictionaryContract)
        {
            var keyType   = dictionaryContract.DictionaryKeyType ?? typeof(object);
            var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);

            if (keyType.IsEnum)
            {
                return(new Schema
                {
                    type = "object",
                    properties = Enum.GetNames(keyType).ToDictionary(
                        (name) => dictionaryContract.PropertyNameResolver(name),
                        (name) => CreateInlineSchema(valueType)
                        )
                });
            }
            else
            {
                return(new Schema
                {
                    type = "object",
                    additionalProperties = CreateInlineSchema(valueType)
                });
            }
        }
Esempio n. 2
0
        private void method_34(JsonWriter jsonWriter_0, IDictionary idictionary_0, JsonDictionaryContract jsonDictionaryContract_0, JsonProperty jsonProperty_0, JsonContainerContract jsonContainerContract_0, JsonProperty jsonProperty_1)
        {
            Interface16 interface2 = idictionary_0 as Interface16;
            object      obj2       = (interface2 != null) ? interface2.Object_0 : idictionary_0;

            this.method_15(jsonWriter_0, jsonDictionaryContract_0, obj2);
            this.list_0.Add(obj2);
            this.method_19(jsonWriter_0, obj2, jsonDictionaryContract_0, jsonProperty_0, jsonContainerContract_0, jsonProperty_1);
            if (jsonDictionaryContract_0.JsonContract_0 == null)
            {
                jsonDictionaryContract_0.JsonContract_0 = base.jsonSerializer_0.icontractResolver_0.ResolveContract(jsonDictionaryContract_0.DictionaryValueType ?? typeof(object));
            }
            if (jsonDictionaryContract_0.JsonContract_2 == null)
            {
                jsonDictionaryContract_0.JsonContract_2 = base.jsonSerializer_0.icontractResolver_0.ResolveContract(jsonDictionaryContract_0.DictionaryKeyType ?? typeof(object));
            }
            int top = jsonWriter_0.Top;

            foreach (DictionaryEntry entry in idictionary_0)
            {
                bool   flag;
                string arg = this.method_35(jsonWriter_0, entry, jsonDictionaryContract_0.JsonContract_2, out flag);
                arg = (jsonDictionaryContract_0.PropertyNameResolver != null) ? jsonDictionaryContract_0.PropertyNameResolver(arg) : arg;
                try
                {
                    object       obj3     = entry.Value;
                    JsonContract contract = jsonDictionaryContract_0.JsonContract_1 ?? this.method_5(obj3);
                    if (this.method_9(obj3, null, contract, jsonDictionaryContract_0, jsonProperty_0))
                    {
                        jsonWriter_0.WritePropertyName(arg, flag);
                        this.method_12(jsonWriter_0, obj3);
                    }
                    else if (this.method_11(jsonWriter_0, obj3, null, contract, jsonDictionaryContract_0, jsonProperty_0))
                    {
                        jsonWriter_0.WritePropertyName(arg, flag);
                        this.method_7(jsonWriter_0, obj3, contract, null, jsonDictionaryContract_0, jsonProperty_0);
                    }
                }
                catch (Exception exception)
                {
                    if (!base.method_2(obj2, jsonDictionaryContract_0, arg, null, jsonWriter_0.String_0, exception))
                    {
                        throw;
                    }
                    this.method_36(jsonWriter_0, top);
                }
            }
            jsonWriter_0.WriteEndObject();
            this.list_0.RemoveAt(this.list_0.Count - 1);
            this.method_16(jsonWriter_0, jsonDictionaryContract_0, obj2);
        }
        private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
        {
            IWrappedDictionary wrappedDictionary = values as IWrappedDictionary;
            object underlyingDictionary = wrappedDictionary != null ? wrappedDictionary.UnderlyingDictionary : values;

            OnSerializing(writer, contract, underlyingDictionary);
            _serializeStack.Add(underlyingDictionary);

            WriteObjectStart(writer, underlyingDictionary, contract, member, collectionContract, containerProperty);

            if (contract.ItemContract == null)
                contract.ItemContract = Serializer._contractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object));

            if (contract.KeyContract == null)
                contract.KeyContract = Serializer._contractResolver.ResolveContract(contract.DictionaryKeyType ?? typeof(object));

            int initialDepth = writer.Top;

            foreach (DictionaryEntry entry in values)
            {
                bool escape;
                string propertyName = GetPropertyName(writer, entry.Key, contract.KeyContract, out escape);

                propertyName = (contract.PropertyNameResolver != null)
                    ? contract.PropertyNameResolver(propertyName)
                    : propertyName;

                try
                {
                    object value = entry.Value;
                    JsonContract valueContract = contract.FinalItemContract ?? GetContractSafe(value);

                    if (ShouldWriteReference(value, null, valueContract, contract, member))
                    {
                        writer.WritePropertyName(propertyName, escape);
                        WriteReference(writer, value);
                    }
                    else
                    {
                        if (!CheckForCircularReference(writer, value, null, valueContract, contract, member))
                            continue;

                        writer.WritePropertyName(propertyName, escape);

                        SerializeValue(writer, value, valueContract, null, contract, member);
                    }
                }
                catch (Exception ex)
                {
                    if (IsErrorHandled(underlyingDictionary, contract, propertyName, null, writer.ContainerPath, ex))
                        HandleError(writer, initialDepth);
                    else
                        throw;
                }
            }

            writer.WriteEndObject();

            _serializeStack.RemoveAt(_serializeStack.Count - 1);

            OnSerialized(writer, contract, underlyingDictionary);
        }