Ejemplo n.º 1
0
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            EnsureReflectionObject(value.GetType());

            DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;

            string keyName  = (string)_reflectionObject.GetValue(value, KeyPropertyName);
            object keyValue = _reflectionObject.GetValue(value, ValuePropertyName);

            Type keyValueType = (keyValue != null) ? keyValue.GetType() : null;

            writer.WriteStartObject();
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyPropertyName) : KeyPropertyName);
            writer.WriteValue(keyName);
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(TypePropertyName) : TypePropertyName);
            writer.WriteValue((keyValueType != null) ? keyValueType.FullName : null);

            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValuePropertyName) : ValuePropertyName);

            if (keyValueType != null)
            {
                string valueJson;
                if (JsonSerializerInternalWriter.TryConvertToString(keyValue, keyValueType, out valueJson))
                {
                    writer.WriteValue(valueJson);
                }
                else
                {
                    writer.WriteValue(keyValue);
                }
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
Ejemplo n.º 2
0
        // Token: 0x060014A5 RID: 5285 RVA: 0x0006DB84 File Offset: 0x0006BD84
        public override void WriteJson(JsonWriter writer, [Nullable(2)] object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }
            EntityKeyMemberConverter.EnsureReflectionObject(value.GetType());
            DefaultContractResolver defaultContractResolver = serializer.ContractResolver as DefaultContractResolver;
            string value2 = (string)EntityKeyMemberConverter._reflectionObject.GetValue(value, "Key");
            object value3 = EntityKeyMemberConverter._reflectionObject.GetValue(value, "Value");
            Type   type   = (value3 != null) ? value3.GetType() : null;

            writer.WriteStartObject();
            writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Key") : "Key");
            writer.WriteValue(value2);
            writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Type") : "Type");
            writer.WriteValue((type != null) ? type.FullName : null);
            writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Value") : "Value");
            if (type != null)
            {
                string value4;
                if (JsonSerializerInternalWriter.TryConvertToString(value3, type, out value4))
                {
                    writer.WriteValue(value4);
                }
                else
                {
                    writer.WriteValue(value3);
                }
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteEndObject();
        }
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            try
            {
                IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper <IEntityKeyMember>(value);
                Type             keyType         = (entityKeyMember.Value != null) ? entityKeyMember.Value.GetType() : null;

                writer.WriteStartObject();
                writer.WritePropertyName("Key");
                writer.WriteValue(entityKeyMember.Key);
                writer.WritePropertyName("Type");
                writer.WriteValue((keyType != null) ? keyType.FullName : null);

                writer.WritePropertyName("Value");

                if (keyType != null)
                {
                    string valueJson;
                    if (JsonSerializerInternalWriter.TryConvertToString(entityKeyMember.Value, keyType, out valueJson))
                    {
                        writer.WriteValue(valueJson);
                    }
                    else
                    {
                        writer.WriteValue(entityKeyMember.Value);
                    }
                }
                else
                {
                    writer.WriteNull();
                }

                writer.WriteEndObject();
            }
            catch { }
        }