public void CannotSerializeFileWithInternationalCharactersInFilenameWhenSettingsDisallowIt()
        {
            ObjectSerializerSettings settings = new ObjectSerializerSettings();
              settings.EnableNonAsciiCharactersInMultipartFilenames = false;

              using (MemoryStream s = new MemoryStream())
              {
            FileData data = new FileData
            {
              MyFile = new FileWithSpecialName("..\\..\\data1.txt", "Bøllefrø.txt")
            };
            new MultipartFormDataSerializer(typeof(FileData)).Serialize(s, data, Encoding.UTF8, "xyzq", settings);

            string expected = @"
            --xyzq
            Content-Disposition: form-data; name=""MyFile""; filename=""Bxllefrx.txt""

            Æüî´`'";

            s.Seek(0, SeekOrigin.Begin);
            using (StreamReader r = new StreamReader(s))
            {
              string result = r.ReadToEnd();
              Assert.AreEqual(expected, result);
            }
              }
        }
        public void CanSerializeSimpleClassWithEncoding(
            [Values("UTF-8|MyInt=10&MyString=Abc+%c3%86%c3%98%c3%85%5e%c3%bc",
            "Windows-1252|MyInt=10&MyString=Abc+%c6%d8%c5%5e%fc",
            "iso-8859-1|MyInt=10&MyString=Abc+%c6%d8%c5%5e%fc")] string charsetData)
        {
            string[] elements = charsetData.Split('|');
              string charset = elements[0];
              string expected = elements[1];

              using (MemoryStream s = new MemoryStream())
              using (StreamWriter w = new StreamWriter(s))
              {
            SimpleData data = new SimpleData
            {
              MyInt = 10,
              MyString = "Abc ÆØÅ^ü"
            };
            ObjectSerializerSettings settings = new ObjectSerializerSettings { Encoding = Encoding.GetEncoding(charset) };
            new FormUrlEncodingSerializer(typeof(SimpleData)).Serialize(w, data, settings);

            w.Flush();
            s.Seek(0, SeekOrigin.Begin);

            // Must always be written in plain old ASCII
            using (StreamReader r = new StreamReader(s, Encoding.ASCII))
            {
              string result = r.ReadToEnd();
              Assert.AreEqual(expected, result);
            }
              }
        }
        public void Serialize(TextWriter writer, object data, ObjectSerializerSettings settings = null)
        {
            Condition.Requires(writer, "writer").IsNotNull();

              Encoding enc = GetEncoding(settings);
              FormUrlEncodingPropertyVisitor v = new FormUrlEncodingPropertyVisitor(writer, enc);
              Serializer.Serialize(data, v, settings);
        }
 public MultipartFormDataPropertyVisitor(Stream s, Encoding encoding = null, string boundary = null, ObjectSerializerSettings settings = null)
 {
     if (encoding == null)
     encoding = Encoding.UTF8;
       Writer = new StreamWriter(s, encoding);
       Output = s;
       Boundary = (boundary != null ? boundary : Guid.NewGuid().ToString());
       Settings = settings;
 }
        /// <summary>
        /// Deserialize form-urlencoded data from reader.
        /// </summary>
        /// <param name="reader">A TextReader for reading url-encoded string input.</param>
        /// <param name="settings">Settings for deserialization.</param>
        /// <returns></returns>
        public object Deserialize(TextReader reader, ObjectSerializerSettings settings = null)
        {
            Condition.Requires(reader, "reader").IsNotNull();

              string data = reader.ReadToEnd();
              Encoding enc = GetEncoding(settings);
              NameValueCollection values = HttpUtility.ParseQueryString(data, enc);
              return Serializer.Deserialize(values, settings);
        }
 public RamoneService(Uri baseUri)
 {
     BaseUri = baseUri;
       DefaultEncoding = Encoding.UTF8;
       CodecManager = new CodecManager();
       AuthorizationDispatcher = new AuthorizationDispatcher();
       RequestInterceptors = new RequestInterceptorSet();
       SerializerSettings = new ObjectSerializerSettings();
       AllowedRedirectsMap = new Dictionary<int, int>();
 }
 public void WriteTo(object entity, OpenRasta.Web.IHttpEntity response, string[] codecParameters)
 {
     using (TextWriter writer = new StreamWriter(response.Stream))
       {
     Encoding enc = null;
     if (Context.Request.Headers["Accept-Charset"] != null)
       enc = Encoding.GetEncoding(Context.Request.Headers["Accept-Charset"]);
     ObjectSerializerSettings settings = new ObjectSerializerSettings { Encoding = enc };
     FormUrlEncodingSerializer serializer = new FormUrlEncodingSerializer(entity.GetType());
     serializer.Serialize(writer, entity, settings);
       }
 }
 public ObjectSerializerSettings(ObjectSerializerSettings src)
 {
     ArrayFormat       = src.ArrayFormat;
     DictionaryFormat  = src.DictionaryFormat;
     PropertyFormat    = src.PropertyFormat;
     DateTimeFormat    = src.DateTimeFormat;
     PropertySeparator = src.PropertySeparator;
     Formaters         = src.Formaters.Clone();
     Culture           = (CultureInfo)src.Culture.Clone();
     Encoding          = src.Encoding;
     EnableNonAsciiCharactersInMultipartFilenames = src.EnableNonAsciiCharactersInMultipartFilenames;
 }
 public ObjectSerializerSettings(ObjectSerializerSettings src)
 {
     ArrayFormat = src.ArrayFormat;
       DictionaryFormat = src.DictionaryFormat;
       PropertyFormat = src.PropertyFormat;
       DateTimeFormat = src.DateTimeFormat;
       PropertySeparator = src.PropertySeparator;
       Formaters = src.Formaters.Clone();
       Culture = (CultureInfo)src.Culture.Clone();
       Encoding = src.Encoding;
       EnableNonAsciiCharactersInMultipartFilenames = src.EnableNonAsciiCharactersInMultipartFilenames;
 }
Exemple #10
0
        public void Serialize(object data, IPropertyVisitor visitor, ObjectSerializerSettings settings = null)
        {
            Visitor = visitor;
              Settings = settings ?? new ObjectSerializerSettings();

              if (data != null && data.GetType() != DataType)
            throw new ArgumentException(string.Format("Cannot serialize {0} - expected {1}.", data.GetType(), DataType), "data");

              visitor.Begin();
              Serialize(data, DataType, "");
              visitor.End();
        }
Exemple #11
0
        public void Serialize(object data, IPropertyVisitor visitor, ObjectSerializerSettings settings = null)
        {
            Visitor  = visitor;
            Settings = settings ?? new ObjectSerializerSettings();

            if (data != null && data.GetType() != DataType)
            {
                throw new ArgumentException(string.Format("Cannot serialize {0} - expected {1}.", data.GetType(), DataType), "data");
            }

            visitor.Begin();
            Serialize(data, DataType, "");
            visitor.End();
        }
 public RamoneSession(IService service)
 {
     Service = service;
       UserAgent = service.UserAgent;
       BaseUri = Service.BaseUri;
       DefaultEncoding = service.DefaultEncoding;
       DefaultRequestMediaType = service.DefaultRequestMediaType;
       DefaultResponseMediaType = service.DefaultResponseMediaType;
       Cookies = new CookieContainer();
       AuthorizationDispatcher = service.AuthorizationDispatcher.Clone();
       RequestInterceptors = service.RequestInterceptors.Clone();
       SerializerSettings = new ObjectSerializerSettings(service.SerializerSettings);
       AllowedRedirectsMap = new Dictionary<int, int>();
       service.CopyRedirect(this);
 }
Exemple #13
0
        public object Deserialize(NameValueCollection values, ObjectSerializerSettings settings = null)
        {
            if (DataType == typeof(NameValueCollection))
            return values;

              Settings = settings ?? new ObjectSerializerSettings();

              object result = Activator.CreateInstance(DataType);
              foreach (string key in values.AllKeys)
              {
            if (key != null)
            {
              string value = values[key];
              IEnumerator propertyNames = key.Split(Settings.PropertySeparator).GetEnumerator();
              propertyNames.MoveNext();
              Evaluate(result, DataType, propertyNames, value);
            }
              }
              return result;
        }
        public void CanSerializeWithStandardFormaters()
        {
            // Arrange
              RamoneConfiguration.RegisterStandardSerializationFormaters(MyObjectSerializerFormaterManager);
              var o = new
              {
            Bool = false,
            Date = new DateTime(2012, 10, 12, 15, 14, 13),
            Url = new Uri("http://dr.dk")
              };

              // Act
              ObjectSerializerSettings settings = new ObjectSerializerSettings
              {
            Formaters = MyObjectSerializerFormaterManager
              };

              string result = Serialize(o, settings);

              // Assert
              Assert.AreEqual("|Bool=False|Date=2012-10-12T15:14:13|Url=http://dr.dk/", result);
        }
        public void CanSerializeWithFormaters()
        {
            // Arrange
              MyObjectSerializerFormaterManager.AddFormater(typeof(Mail), new MailObjectSerializerFormater());
              MyObjectSerializerFormaterManager.AddFormater(typeof(bool), new BoolObjectSerializerFormater());
              var o = new
              {
            Mail = new Mail { Address = "*****@*****.**" },
            Bool = true
              };

              // Act
              ObjectSerializerSettings settings = new ObjectSerializerSettings
              {
            Formaters = MyObjectSerializerFormaterManager
              };

              string result = Serialize(o, settings);

              // Assert
              Assert.AreEqual("|[email protected]|Bool=1", result);
        }
Exemple #16
0
        public object Deserialize(NameValueCollection values, ObjectSerializerSettings settings = null)
        {
            if (DataType == typeof(NameValueCollection))
            {
                return(values);
            }

            Settings = settings ?? new ObjectSerializerSettings();

            object result = Activator.CreateInstance(DataType);

            foreach (string key in values.AllKeys)
            {
                if (key != null)
                {
                    string      value         = values[key];
                    IEnumerator propertyNames = key.Split(Settings.PropertySeparator).GetEnumerator();
                    propertyNames.MoveNext();
                    Evaluate(result, DataType, propertyNames, value);
                }
            }
            return(result);
        }
 public static void Reset()
 {
     UseStandardCodecs = true;
       UserAgent = "Ramone/1.0";
       DefaultEncoding = Encoding.UTF8;
       SerializerSettings = new ObjectSerializerSettings();
 }
 Encoding GetEncoding(ObjectSerializerSettings settings)
 {
     return (settings != null ? settings.Encoding ?? Encoding.UTF8 : Encoding.UTF8);
 }
        public void CanSerializeDictionaryAndClassesWithNullValues()
        {
            // Arrange
              var o = new
              {
            A = (int?)null,
            B = new Dictionary<string, object>
            {
              { "X", 100 },
              { "Y", null }
            }
              };

              ObjectSerializerSettings settings = new ObjectSerializerSettings { IncludeNullValues = true };

              // Act
              string result = Serialize(o, settings);

              // Assert
              Assert.AreEqual("|A=|B[X]=100|B[Y]=", result);
        }
        public void CanSerializeWithDifferentSeparatorFormaters()
        {
            // Arrange
              var o = new
              {
            B = new string[] { "abc", "xyz" },
            A = new Hashtable()
              };
              ((Hashtable)o.A)["p"] = 17;
              ((Hashtable)o.A)["q"] = "abc";

              ObjectSerializerSettings settings = new ObjectSerializerSettings
              {
            ArrayFormat = "{0}:{1}",
            DictionaryFormat = "{0}#{1}",
            PropertyFormat = "{0}+{1}"
              };

              // Act
              string result = Serialize(o, settings);

              // Assert
              Assert.AreEqual("|B:0=abc|B:1=xyz|A#p=17|A#q=abc", result);
        }
        public void WhenSerializingItUsesSuppliedLocale()
        {
            // Arrange
              ClassWithLocaleDependentValues o = new ClassWithLocaleDependentValues
              {
            Dec = 10.5M,
            Flo = 20.12F,
            Dou = 13.23
              };

              // Act
              ObjectSerializerSettings settings = new ObjectSerializerSettings
              {
            Culture = CultureInfo.GetCultureInfo("da-DK")
              };

              string result = Serialize(o, settings);

              // Assert
              Assert.AreEqual("|Dec=10,5|Flo=20,12|Dou=13,23", result);
        }
 public void Serialize(Stream s, object data, Encoding encoding = null, string boundary = null, ObjectSerializerSettings settings = null)
 {
   MultipartFormDataPropertyVisitor v = new MultipartFormDataPropertyVisitor(s, encoding, boundary, settings);
   Serializer.Serialize(data, v, settings);
 }