Ejemplo n.º 1
0
        /// <summary>
        /// Writes the JSON representation of a <see cref="StateObject"/> instance.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="objectType">The object type.</param>
        /// <param name="value">The value to serialize.</param>
        /// <param name="serializer">The calling serializer.</param>
        public void WriteJson(JsonWriter writer, Type objectType, StateObject value, JsonSerializer serializer)
        {
            var state        = value.GetState();
            var instanceType = value.GetType();

            writer.WriteStartObject();

            // Determine if we must write out the state object type (i.e., $type property) or if we can simply infer the type from the objectType provided.
            if (objectType != instanceType)
            {
                writer.WritePropertyName(TypePropertyName);
                serializer.Serialize(writer, instanceType.GetFullNameWithAssembly());
            }

            // Write out each state object property value.
            foreach (var item in state)
            {
                writer.WritePropertyName(item.Key);

                if (item.Value == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    WriteProperty(writer, value.GetFieldType(item.Key), item.Value, serializer);
                }
            }

            writer.WriteEndObject();
        }
        /// <summary>
        /// Writes the JSON representation of a <see cref="StateObject"/> instance.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="objectType">The object type.</param>
        /// <param name="value">The value to serialize.</param>
        /// <param name="serializer">The calling serializer.</param>
        public void WriteJson(JsonWriter writer, Type objectType, StateObject value, JsonSerializer serializer)
        {
            var state = value.GetState();
            var instanceType = value.GetType();

            writer.WriteStartObject();

            // Determine if we must write out the state object type (i.e., $type property) or if we can simply infer the type from the objectType provided.
            if (objectType != instanceType)
            {
                writer.WritePropertyName(TypePropertyName);
                serializer.Serialize(writer, instanceType.GetFullNameWithAssembly());
            }

            // Write out each state object property value.
            foreach (var item in state)
            {
                writer.WritePropertyName(item.Key);

                if (item.Value == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    WriteProperty(writer, value.GetFieldType(item.Key), item.Value, serializer);
                }
            }

            writer.WriteEndObject();
        }