Exemplo n.º 1
0
        public void TestStringSerialization()
        {
            Assert.AreEqual("\"hello world\"", JsonSerializationHelper.Serialize("hello world"));
            Assert.AreEqual("\"hello Mr. \\\"officer\\\"!\"", JsonSerializationHelper.Serialize("hello Mr. \"officer\"!"));
            Assert.AreEqual("\"The file is in C:\\\\mystuff\"", JsonSerializationHelper.Serialize("The file is in C:\\mystuff"));

            Assert.AreEqual("null", JsonSerializationHelper.Serialize((string)null));
        }
        public void Should_resolve_constructor_created_by_memberinfo()
        {
            var expected = typeof(Overload).GetConstructor(Array.Empty <Type>());
            var ctor     = new ConstructorInfo(expected);
            var ctor2    = JsonSerializationHelper.Serialize(ctor);

            ctor2.Constructor.ShouldBeSameAs(expected);
        }
Exemplo n.º 3
0
        public void Should_resolve_method_created_by_memberinfo()
        {
            var expected = typeof(Overload)
                           .GetMethods(BindingFlags.Public | BindingFlags.Instance)
                           .Single(x => x.Name == nameof(Overload.Method) && x.GetParameters().Length == 1);

            var methodInfo = new MethodInfo(expected);

            var methodInfo2 = JsonSerializationHelper.Serialize(methodInfo);

            methodInfo2.ToMethodInfo().ShouldBeSameAs(expected);
        }
Exemplo n.º 4
0
        public void TestArraySerialization()
        {
            var list = new List <object>();

            list.Add("The quick brown fox jumps over the lazy dog");
            list.Add(1337);
            list.Add(null);
            list.Add(new Person()
            {
                Name = "Foobar", Age = 1337
            });

            var json = JsonSerializationHelper.Serialize(list);

            Assert.AreEqual("[\"The quick brown fox jumps over the lazy dog\",1337,null,{\"Id\":\"" + default(Guid) + "\",\"Name\":\"Foobar\",\"Age\":1337,\"IsAlive\":false}]", json);
        }
Exemplo n.º 5
0
        public void TestNumericSerialization()
        {
            Assert.AreEqual("1.337", JsonSerializationHelper.Serialize((double)1.337));
            Assert.AreEqual("1.337", JsonSerializationHelper.Serialize((decimal)1.337));
            Assert.AreEqual("1.337", JsonSerializationHelper.Serialize((float)1.337));

            Assert.AreEqual("-1.337", JsonSerializationHelper.Serialize((double)-1.337));
            Assert.AreEqual("-1.337", JsonSerializationHelper.Serialize((decimal) - 1.337));
            Assert.AreEqual("-1.337", JsonSerializationHelper.Serialize((float)-1.337));

            Assert.AreEqual("0", JsonSerializationHelper.Serialize((double)0));
            Assert.AreEqual("0", JsonSerializationHelper.Serialize((int)0));

            Assert.AreEqual("1337", JsonSerializationHelper.Serialize((int)1337));
            Assert.AreEqual("1337", JsonSerializationHelper.Serialize((short)1337));
            Assert.AreEqual("1337", JsonSerializationHelper.Serialize((long)1337));

            Assert.AreEqual("-1337", JsonSerializationHelper.Serialize((int)-1337));
            Assert.AreEqual("-1337", JsonSerializationHelper.Serialize((short)-1337));
            Assert.AreEqual("-1337", JsonSerializationHelper.Serialize((long)-1337));
        }
Exemplo n.º 6
0
        public void TestObjectSerialization()
        {
            var group = new Group();

            group.Title    = "My group";
            group.IsActive = true;

            var persons = group.Persons = new List <Person>();

            persons.Add(new Person()
            {
                Name    = "Jens",
                Age     = 1337,
                IsAlive = true
            });
            persons.Add(new Person()
            {
                Name    = "Ole",
                Age     = -10,
                IsAlive = false
            });

            Assert.AreEqual("{\"Leader\":null,\"Persons\":[{\"Id\":\"" + default(Guid) + "\",\"Name\":\"Jens\",\"Age\":1337,\"IsAlive\":true},{\"Id\":\"" + default(Guid) + "\",\"Name\":\"Ole\",\"Age\":-10,\"IsAlive\":false}],\"Title\":\"My group\",\"IsActive\":true}", JsonSerializationHelper.Serialize(group));
        }
Exemplo n.º 7
0
 public void TestBooleanSerialization()
 {
     Assert.AreEqual("true", JsonSerializationHelper.Serialize(true));
     Assert.AreEqual("false", JsonSerializationHelper.Serialize(false));
 }
        private static byte[] GetMultipartFormData(Dictionary <string, object> postParameters, string boundary)
        {
            Stream formDataStream = new MemoryStream();
            var    needsClrf      = false;

            if (postParameters.Count > 1)
            {
                foreach (var param in postParameters)
                {
                    // Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
                    // Skip it on the first parameter, add it to subsequent parameters.
                    if (needsClrf)
                    {
                        formDataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, Encoding.UTF8.GetByteCount("\r\n"));
                    }

                    needsClrf = true;

                    if (param.Value is FileInfo)
                    {
                        var fileInfo = (FileInfo)param.Value;
                        var postData =
                            string.Format(
                                "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n",
                                boundary,
                                param.Key,
                                fileInfo.MimeType);
                        formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));

                        // Write the file data directly to the Stream, rather than serializing it to a string.
                        formDataStream.Write(fileInfo.FileContent, 0, fileInfo.FileContent.Length);
                    }
                    else
                    {
                        var stringData = JsonSerializationHelper.Serialize(param.Value);
                        var postData   =
                            string.Format(
                                "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
                                boundary,
                                param.Key,
                                stringData);
                        formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));
                    }
                }

                // Add the end of the request.  Start with a newline
                var footer = "\r\n--" + boundary + "--\r\n";
                formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
            }
            else
            {
                foreach (var param in postParameters)
                {
                    if (param.Value is FileInfo)
                    {
                        var fileInfo = (FileInfo)param.Value;

                        // Write the file data directly to the Stream, rather than serializing it to a string.
                        formDataStream.Write(fileInfo.FileContent, 0, fileInfo.FileContent.Length);
                    }
                    else if (param.Value is byte[])
                    {
                        // Write the file data directly to the Stream, rather than serializing it to a string.
                        formDataStream.Write(param.Value as byte[], 0, (param.Value as byte[]).Length);
                    }
                    else
                    {
                        string postData;
                        if (!(param.Value is string))
                        {
                            postData = JsonSerializationHelper.Serialize(param.Value);
                        }
                        else
                        {
                            postData = (string)param.Value;
                        }

                        formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));
                    }
                }
            }

            // Dump the Stream into a byte[]
            formDataStream.Position = 0;
            var formData = new byte[formDataStream.Length];

            formDataStream.Read(formData, 0, formData.Length);
            formDataStream.Close();

            return(formData);
        }
Exemplo n.º 9
0
 /// <summary>
 ///     Produce a string representation of the supplied object.
 /// </summary>
 /// <param name="value">Instance to serialize.</param>
 /// <returns>
 ///     Returns a string representing the object instance that can be placed into the Redis cache.
 /// </returns>
 /// <seealso cref="Deserialize" />
 public string Serialize(object value)
 {
     return(JsonSerializationHelper.Serialize(value));
 }
Exemplo n.º 10
0
 public JsonActionResult(object response)
     : this(JsonSerializationHelper.Serialize(response))
 {
 }
Exemplo n.º 11
0
 public JsonSerializer()
     : base(x => (Expression)JsonSerializationHelper.Serialize(x, x.GetType()))
 {
 }