Ejemplo n.º 1
0
        /// <summary>
        /// Set the object's value, after serializing it with the provided serializer.
        /// </summary>
        /// <typeparam name="T">The type of the value.</typeparam>
        /// <param name="value">The unserialized value.</param>
        /// <param name="serializeObject">A delegate to handle serialization of an object to a string.</param>
        public void SetObject <T>(T value, SerializeObjectToByteArray <T> serializeObject)
        {
            if (serializeObject == null)
            {
                throw new ArgumentException("serializeObject cannot be null");
            }

            Value = serializeObject(value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Set the object's value, after serializing it.
        /// </summary>
        /// <typeparam name="T">The type of the value.</typeparam>
        /// <param name="value">The unserialized value.</param>
        /// <param name="contentType">The content type of the object.</param>
        /// <remarks>
        /// This overload will choose its serializer based on the <paramref name="contentType"/>.
        /// Supported types for this overload are: <see cref="RiakConstants.ContentTypes.ApplicationJson"/>,
        /// <see cref="RiakConstants.ContentTypes.ProtocolBuffers"/>
        /// <see cref="RiakConstants.ContentTypes.Xml"/>
        /// and text.
        /// </remarks>
        public void SetObject <T>(T value, string contentType = null)
            where T : class
        {
            if (!string.IsNullOrEmpty(contentType))
            {
                ContentType = contentType;
            }

            // check content type
            // save based on content type's deserialization method
            if (ContentType == RiakConstants.ContentTypes.ApplicationJson)
            {
                var sots = new SerializeObjectToString <T>(theObject => theObject.Serialize());
                SetObject(value, ContentType, sots);
                return;
            }

            if (ContentType == RiakConstants.ContentTypes.ProtocolBuffers)
            {
                var objectToByteArrayFunc = new SerializeObjectToByteArray <T>(theObject =>
                {
                    using (var ms = new MemoryStream())
                    {
                        Serializer.Serialize(ms, value);
                        return(ms.ToArray());
                    }
                });

                SetObject(value, ContentType, objectToByteArrayFunc);

                return;
            }

            if (ContentType == RiakConstants.ContentTypes.Xml)
            {
                var objectToByteArrayFunc = new SerializeObjectToByteArray <T>(theObject =>
                {
                    var ms    = new MemoryStream();
                    var serde = new XmlSerializer(typeof(T));
                    serde.Serialize(ms, value);
                    return(ms.ToArray());
                });

                SetObject(value, ContentType, objectToByteArrayFunc);

                return;
            }

            if (ContentType.StartsWith("text"))
            {
                Value = value.ToString().ToRiakString();
                return;
            }

            throw new NotSupportedException(string.Format("Your current ContentType ({0}), is not supported.", ContentType));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Set the object's value, after serializing it with the provided serializer.
        /// </summary>
        /// <typeparam name="T">The type of the value.</typeparam>
        /// <param name="value">The unserialized value.</param>
        /// <param name="contentType">The content type of the object.</param>
        /// <param name="serializeObject">A delegate to handle serialization of an object to a string.</param>
        public void SetObject <T>(T value, string contentType, SerializeObjectToByteArray <T> serializeObject)
        {
            if (string.IsNullOrEmpty(contentType))
            {
                throw new ArgumentException("contentType must be a valid MIME type");
            }

            ContentType = contentType;

            SetObject(value, serializeObject);
        }