Ejemplo n.º 1
0
        /// <summary>
        /// Serializes the object into an action result based on the content type requested by the
        /// user (defaults to json)
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="Object">Object to serialize</param>
        /// <param name="ContentType">Content type to serialize it as</param>
        /// <returns>Action result</returns>
        protected virtual ActionResult Serialize <T>(T Object, string ContentType = "")
        {
            Utilities.IO.Serializers.Manager Manager = Utilities.IoC.Manager.Bootstrapper == null ? null : Utilities.IoC.Manager.Bootstrapper.Resolve <Utilities.IO.Serializers.Manager>();
            var Result = new ContentResult();

            if (Manager == null)
            {
                return(Result);
            }
            if (string.IsNullOrEmpty(ContentType))
            {
                if (Request.AcceptTypes != null)
                {
                    ContentType = Request.AcceptTypes.Length > 0 ? Request.AcceptTypes.FirstOrDefault(Manager.CanSerialize) : "";
                }
                if (string.IsNullOrEmpty(ContentType) && Request.Path.Contains('.'))
                {
                    ContentType = Manager.FileTypeToContentType(Request.Path.ToUpperInvariant().Right((Request.Path.Length - Request.Path.LastIndexOf('.'))));
                }
                if (string.IsNullOrEmpty(ContentType))
                {
                    ContentType = "application/json";
                }
            }
            Result.Content     = Object.Serialize <string, T>(ContentType);
            Result.ContentType = ContentType;
            return(Result);
        }
        public void SerializeDeserialize()
        {
            var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects <ISerializer>());

            Assert.Equal("{\"A\":10}", Temp.Serialize <string>(new Temp {
                A = 10
            }, typeof(Temp)));
            Assert.Equal(new Temp {
                A = 10
            }.A, ((Temp)Temp.Deserialize <string>("{\"A\":10}", typeof(Temp))).A);
        }
        public void FileTypeToContentType()
        {
            var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects <ISerializer>());

            Assert.Equal("application/json", Temp.FileTypeToContentType(".json"));
        }
        public void Creation()
        {
            var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects <ISerializer>());

            Assert.NotNull(Temp);
        }
        public void CanSerialize()
        {
            var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects <ISerializer>());

            Assert.True(Temp.CanSerialize("application/json"));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="GeneralValueProvider"/> class.
 /// </summary>
 /// <param name="Manager">The manager.</param>
 public GeneralValueProvider(Utilities.IO.Serializers.Manager Manager)
 {
     this.Manager = Manager;
 }
Ejemplo n.º 7
0
 public void SerializeDeserialize()
 {
     var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects<ISerializer>());
     Assert.Equal("{\"A\":10}", Temp.Serialize<string>(new Temp { A = 10 }, typeof(Temp)));
     Assert.Equal(new Temp { A = 10 }.A, ((Temp)Temp.Deserialize<string>("{\"A\":10}", typeof(Temp))).A);
 }
Ejemplo n.º 8
0
 public void FileTypeToContentType()
 {
     var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects<ISerializer>());
     Assert.Equal("application/json", Temp.FileTypeToContentType(".json"));
 }
Ejemplo n.º 9
0
 public void Creation()
 {
     var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects<ISerializer>());
     Assert.NotNull(Temp);
 }
Ejemplo n.º 10
0
 public void CanSerialize()
 {
     var Temp = new Utilities.IO.Serializers.Manager(AppDomain.CurrentDomain.GetAssemblies().Objects<ISerializer>());
     Assert.True(Temp.CanSerialize("application/json"));
 }