Example #1
0
 /// <summary>
 /// Changes to the specified culture.
 /// </summary>
 /// <param name="cultureInfo">The culture information.</param>
 /// <returns>The disposable current culture object.</returns>
 public static IDisposable TemporaryChangeTo(CultureInfo cultureInfo)
 {
     var cc = new CurrentCulture { previousCulture = Thread.CurrentThread.CurrentCulture };
     Thread.CurrentThread.CurrentCulture = cultureInfo;
     System.Diagnostics.Debug.WriteLine("Set CurrentCulture to " + cultureInfo.Name);
     return cc;
 }
        /// <summary>
        /// Changes to the specified culture.
        /// </summary>
        /// <param name="cultureInfo">The culture information.</param>
        /// <returns>The disposable current culture object.</returns>
        public static IDisposable TemporaryChangeTo(CultureInfo cultureInfo)
        {
            var cc = new CurrentCulture {
                previousCulture = CultureInfo.CurrentCulture
            };

            CultureInfo.CurrentCulture = cultureInfo;
            System.Diagnostics.Debug.WriteLine("Set CurrentCulture to " + cultureInfo.Name);
            return(cc);
        }
Example #3
0
 public void Double_Serialize()
 {
     // assert that we are consistent with serialization of numbers
     using (CurrentCulture.TemporaryChangeTo(CultureInfos.Norwegian))
     {
         // assert the current format provider uses , as decimal separator
         Assert.AreEqual("1,2", 1.2.ToString());
         var xml = Serialize(1.2);
         Assert.AreEqual(XmlDeclaration + "<double>1.2</double>", xml);
     }
 }
Example #4
0
 public void Parse_CustomCulture2()
 {
     using (CurrentCulture.TemporaryChangeTo(this.customCulture))
     {
         var unitProvider = new UnitProvider(CultureInfo.InvariantCulture);
         unitProvider.RegisterUnits(typeof(Length));
         using (DefaultUnitProvider.TemporaryChangeTo(unitProvider))
         {
             Assert.AreEqual(1.2 * Length.Metre, Length.Parse("1,2 m"));
         }
     }
 }
Example #5
0
        public void Serialize_CultureWithCommaAsNumericSeparator()
        {
            using (CurrentCulture.TemporaryChangeTo(CultureInfos.Norwegian))
            {
                var t   = new Test();
                var xml = Serialize(t);
                Assert.AreEqual(ExpectedXml, xml);

                var t2 = Deserialize <Test>(xml);
                Assert.AreEqual(t2.Distance, t.Distance);
            }
        }
        public void Serialize_CultureWithCommaAsNumericSeparator()
        {
            using (CurrentCulture.TemporaryChangeTo(CultureInfos.Norwegian))
            {
                var t   = new Test();
                var xml = Serialize(t);
                Assert.IsTrue(xml.Contains("<Distance>100.2</Distance>"));

                var t2 = Deserialize <Test>(xml);
                Assert.AreEqual(t2.Distance, t.Distance);
            }
        }
        public void Serialize_NorwegianCulture_ShouldBeSerializedToInvariantCulture()
        {
            var obj = new TestObject {
                Distance = 1.23 * Length.Metre, Time = null
            };

            using (CurrentCulture.TemporaryChangeTo("nb-NO"))
            {
                JsConfig.IncludeNullValues = true;
                var json = JsonSerializer.SerializeToString(obj);
                Assert.AreEqual("{\"Distance\":\"1.23 m\",\"Time\":null,\"Quantities\":[]}", json);
            }
        }
Example #8
0
        public void Serialize_XmlSerializer()
        {
            using (CurrentCulture.TemporaryChangeTo(CultureInfo.InvariantCulture))
            {
                var s = new XmlSerializer(typeof(Test));
                var t = new Test {
                    Distance = 100.2 * Length.Metre
                };
                var ms = new MemoryStream();
                s.Serialize(ms, t);
                var xml = Encoding.UTF8.GetString(ms.ToArray());
                Assert.IsTrue(xml.Contains(@"<Distance>100.2 m</Distance>"));

                // Deserialize
                var ms2 = new MemoryStream(Encoding.UTF8.GetBytes(xml));
                var t2  = (Test)s.Deserialize(ms2);
                Assert.AreEqual(t2.Distance, t.Distance);
            }
        }