示例#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)
        {
            DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;

            IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper <IEntityKeyMember>(value);
            Type             keyType         = (entityKeyMember.Value != null) ? entityKeyMember.Value.GetType() : null;

            writer.WriteStartObject();
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyPropertyName) : KeyPropertyName);
            writer.WriteValue(entityKeyMember.Key);
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(TypePropertyName) : TypePropertyName);
            writer.WriteValue((keyType != null) ? keyType.FullName : null);

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

            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();
        }
        /// <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)
        {
            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();
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            DefaultContractResolver defaultContractResolver = serializer.ContractResolver as DefaultContractResolver;
            IEntityKeyMember        entityKeyMember         = DynamicWrapper.CreateWrapper <IEntityKeyMember>(value);
            Type type = (entityKeyMember.Value != null) ? entityKeyMember.Value.GetType() : null;

            writer.WriteStartObject();
            writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Key") : "Key");
            writer.WriteValue(entityKeyMember.Key);
            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 value2;
                if (JsonSerializerInternalWriter.TryConvertToString(entityKeyMember.Value, type, out value2))
                {
                    writer.WriteValue(value2);
                }
                else
                {
                    writer.WriteValue(entityKeyMember.Value);
                }
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteEndObject();
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper <IEntityKeyMember>(Activator.CreateInstance(objectType));

            EntityKeyMemberConverter.ReadAndAssertProperty(reader, "Key");
            EntityKeyMemberConverter.ReadAndAssert(reader);
            entityKeyMember.Key = reader.Value.ToString();
            EntityKeyMemberConverter.ReadAndAssertProperty(reader, "Type");
            EntityKeyMemberConverter.ReadAndAssert(reader);
            string typeName = reader.Value.ToString();
            Type   type     = Type.GetType(typeName);

            EntityKeyMemberConverter.ReadAndAssertProperty(reader, "Value");
            EntityKeyMemberConverter.ReadAndAssert(reader);
            entityKeyMember.Value = serializer.Deserialize(reader, type);
            EntityKeyMemberConverter.ReadAndAssert(reader);
            return(DynamicWrapper.GetUnderlyingObject(entityKeyMember));
        }