-Fixed ToString culture inconsistence when serializing dictionary key

-Documentation
This commit is contained in:
JamesNK
2009-12-08 22:27:13 +00:00
parent 52279e610b
commit 2e54bf69ab
12 changed files with 736 additions and 13 deletions
@@ -552,6 +552,124 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(@"<span class=""vevent""><a class=""url"" href=""http://www.web2con.com/"">Text</a><!-- Hi! --></span>", newDoc.InnerXml);
}
[Test]
public void SerializeExample()
{
string xml = @"<?xml version=""1.0"" standalone=""no""?>
<root>
<person id=""1"">
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id=""2"">
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
// {
// "?xml": {
// "@version": "1.0",
// "@standalone": "no"
// },
// "root": {
// "person": [
// {
// "@id": "1",
// "name": "Alan",
// "url": "http://www.google.com"
// },
// {
// "@id": "2",
// "name": "Louis",
// "url": "http://www.yahoo.com"
// }
// ]
// }
// }
// format
jsonText = JObject.Parse(jsonText).ToString();
Assert.AreEqual(@"{
""?xml"": {
""@version"": ""1.0"",
""@standalone"": ""no""
},
""root"": {
""person"": [
{
""@id"": ""1"",
""name"": ""Alan"",
""url"": ""http://www.google.com""
},
{
""@id"": ""2"",
""name"": ""Louis"",
""url"": ""http://www.yahoo.com""
}
]
}
}", jsonText);
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
}
[Test]
public void DeserializeExample()
{
string json = @"{
""?xml"": {
""@version"": ""1.0"",
""@standalone"": ""no""
},
""root"": {
""person"": [
{
""@id"": ""1"",
""name"": ""Alan"",
""url"": ""http://www.google.com""
},
{
""@id"": ""2"",
""name"": ""Louis"",
""url"": ""http://www.yahoo.com""
}
]
}
}";
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
// <?xml version="1.0" standalone="no"?>
// <root>
// <person id="1">
// <name>Alan</name>
// <url>http://www.google.com</url>
// </person>
// <person id="2">
// <name>Louis</name>
// <url>http://www.yahoo.com</url>
// </person>
// </root>
Assert.AreEqual(@"<?xml version=""1.0"" standalone=""no""?>
<root>
<person id=""1"">
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id=""2"">
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>".Replace(Environment.NewLine, string.Empty), doc.InnerXml);
}
}
}
#endif
@@ -415,5 +415,19 @@ Parameter name: index")]
Assert.AreEqual(3, (int)a[key]);
}
[Test]
public void ReplaceAll()
{
JArray a = new JArray(new [] { 1, 2, 3 });
Assert.AreEqual(3, a.Count);
Assert.AreEqual(1, (int)a[0]);
Assert.AreEqual(2, (int)a[1]);
Assert.AreEqual(3, (int)a[2]);
a.ReplaceAll(1);
Assert.AreEqual(1, a.Count);
Assert.AreEqual(1, (int)a[0]);
}
}
}
@@ -242,5 +242,72 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(JTokenType.Integer, t.Type);
Assert.AreEqual(3, (int)t);
}
[Test]
public void Example()
{
JObject o = JObject.Parse(@"{
""Stores"": [
""Lambton Quay"",
""Willis Street""
],
""Manufacturers"": [
{
""Name"": ""Acme Co"",
""Products"": [
{
""Name"": ""Anvil"",
""Price"": 50
}
]
},
{
""Name"": ""Contoso"",
""Products"": [
{
""Name"": ""Elbow Grease"",
""Price"": 99.95
},
{
""Name"": ""Headlight Fluid"",
""Price"": 4
}
]
}
]
}");
string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
// 50
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease
Assert.AreEqual("Acme Co", name);
Assert.AreEqual(50m, productPrice);
Assert.AreEqual("Elbow Grease", productName);
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
// Lambton Quay
// Willis Street
IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
// null
// Headlight Fluid
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
// 149.95
Assert.AreEqual(2, storeNames.Count);
Assert.AreEqual("Lambton Quay", storeNames[0]);
Assert.AreEqual("Willis Street", storeNames[1]);
Assert.AreEqual(2, firstProductNames.Count);
Assert.AreEqual(null, firstProductNames[0]);
Assert.AreEqual("Headlight Fluid", firstProductNames[1]);
Assert.AreEqual(149.95m, totalPrice);
}
}
}
@@ -1993,5 +1993,118 @@ keyword such as type of business.""
Assert.AreEqual(1, newFoo.Bars[1].Id);
Assert.AreEqual(2, newFoo.Bars[2].Id);
}
[Test]
public void SerializeGuidKeyedDictionary()
{
Dictionary<Guid, int> dictionary = new Dictionary<Guid, int>();
dictionary.Add(new Guid("F60EAEE0-AE47-488E-B330-59527B742D77"), 1);
dictionary.Add(new Guid("C2594C02-EBA1-426A-AA87-8DD8871350B0"), 2);
string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);
Assert.AreEqual(@"{
""f60eaee0-ae47-488e-b330-59527b742d77"": 1,
""c2594c02-eba1-426a-aa87-8dd8871350b0"": 2
}", json);
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Could not create string property name from type 'Newtonsoft.Json.Tests.TestObjects.Person'.")]
public void SerializePersonKeyedDictionary()
{
Dictionary<Person, int> dictionary = new Dictionary<Person, int>();
dictionary.Add(new Person { Name = "p1" }, 1);
dictionary.Add(new Person { Name = "p2" }, 2);
JsonConvert.SerializeObject(dictionary, Formatting.Indented);
}
public class SearchResult
{
public string Title { get; set; }
public string Content { get; set; }
public string Url { get; set; }
}
[Test]
public void SerializeFragment()
{
string googleSearchText = @"{
""responseData"": {
""results"": [
{
""GsearchResultClass"": ""GwebSearch"",
""unescapedUrl"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",
""url"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",
""visibleUrl"": ""en.wikipedia.org"",
""cacheUrl"": ""http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org"",
""title"": ""<b>Paris Hilton</b> - Wikipedia, the free encyclopedia"",
""titleNoFormatting"": ""Paris Hilton - Wikipedia, the free encyclopedia"",
""content"": ""[1] In 2006, she released her debut album...""
},
{
""GsearchResultClass"": ""GwebSearch"",
""unescapedUrl"": ""http://www.imdb.com/name/nm0385296/"",
""url"": ""http://www.imdb.com/name/nm0385296/"",
""visibleUrl"": ""www.imdb.com"",
""cacheUrl"": ""http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com"",
""title"": ""<b>Paris Hilton</b>"",
""titleNoFormatting"": ""Paris Hilton"",
""content"": ""Self: Zoolander. Socialite <b>Paris Hilton</b>...""
}
],
""cursor"": {
""pages"": [
{
""start"": ""0"",
""label"": 1
},
{
""start"": ""4"",
""label"": 2
},
{
""start"": ""8"",
""label"": 3
},
{
""start"": ""12"",
""label"": 4
}
],
""estimatedResultCount"": ""59600000"",
""currentPageIndex"": 0,
""moreResultsUrl"": ""http://www.google.com/search?oe=utf8&ie=utf8...""
}
},
""responseDetails"": null,
""responseStatus"": 200
}";
JObject googleSearch = JObject.Parse(googleSearchText);
// get JSON result objects into a list
IList<JToken> results = googleSearch["responseData"]["results"].Children().ToList();
// serialize JSON results into .NET objects
IList<SearchResult> searchResults = new List<SearchResult>();
foreach (JToken result in results)
{
SearchResult searchResult = JsonConvert.DeserializeObject<SearchResult>(result.ToString());
searchResults.Add(searchResult);
}
// Title = <b>Paris Hilton</b> - Wikipedia, the free encyclopedia
// Content = [1] In 2006, she released her debut album...
// Url = http://en.wikipedia.org/wiki/Paris_Hilton
// Title = <b>Paris Hilton</b>
// Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...
// Url = http://www.imdb.com/name/nm0385296/
Assert.AreEqual(2, searchResults.Count);
Assert.AreEqual("<b>Paris Hilton</b> - Wikipedia, the free encyclopedia", searchResults[0].Title);
Assert.AreEqual("<b>Paris Hilton</b>", searchResults[1].Title);
}
}
}
@@ -25,8 +25,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests
@@ -34,6 +36,14 @@ namespace Newtonsoft.Json.Tests
[TestFixture]
public abstract class TestFixtureBase
{
[TestFixtureSetUp]
protected void TestSetup()
{
//CultureInfo turkey = CultureInfo.CreateSpecificCulture("tr");
//Thread.CurrentThread.CurrentCulture = turkey;
//Thread.CurrentThread.CurrentUICulture = turkey;
}
//public string GetOffset(DateTime value)
//{
// TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime());