Beispiel #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();
        }
        private TState Persist(StateObject stateObject, Metadata metadata)
        {
            var raw           = _stateAdapterProvider.AsRaw <StateObject, TState>(stateObject.PersistenceId.ToString(), stateObject, 1, metadata);
            var store         = _stores.ComputeIfAbsent(stateObject.GetType(), type => new Dictionary <long, TState>());
            var persistenceId = stateObject.PersistenceId == -1L ? _nextId++ : stateObject.PersistenceId;

            store.Add(persistenceId, raw);
            stateObject.SetPersistenceId(persistenceId);
            return(raw);
        }
        /// <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();
        }