From 68af212cfb443634f386de8dac4adf62608ffd6b Mon Sep 17 00:00:00 2001 From: JamesNK Date: Fri, 10 Oct 2008 20:02:05 +0000 Subject: [PATCH] -Fix JContainer child ordering when adding multi values --- Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs | 55 +++++++++++++++++++ Src/Newtonsoft.Json/Linq/JContainer.cs | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs index 5b5af163..5cfb2b46 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs @@ -298,5 +298,60 @@ Parameter name: index")] string json = @"{""prop"":""value""}"; JArray.Parse(json); } + + public class ListItemFields + { + public string ListItemText { get; set; } + public object ListItemValue { get; set; } + } + + [Test] + public void ArrayOrder() + { + string itemZeroText = "Zero text"; + + IEnumerable t = new List + { + new ListItemFields { ListItemText = "First", ListItemValue = 1 }, + new ListItemFields { ListItemText = "Second", ListItemValue = 2 }, + new ListItemFields { ListItemText = "Third", ListItemValue = 3 } + }; + + JObject optionValues = + new JObject( + new JProperty("options", + new JArray( + new JObject( + new JProperty("text", itemZeroText), + new JProperty("value", "0")), + from r in t + orderby r.ListItemValue + select new JObject( + new JProperty("text", r.ListItemText), + new JProperty("value", r.ListItemValue.ToString()))))); + + string result = "myOptions = " + optionValues.ToString(); + + Assert.AreEqual(@"myOptions = { + ""options"": [ + { + ""text"": ""Zero text"", + ""value"": ""0"" + }, + { + ""text"": ""First"", + ""value"": ""1"" + }, + { + ""text"": ""Second"", + ""value"": ""2"" + }, + { + ""text"": ""Third"", + ""value"": ""3"" + } + ] +}", result); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 48b759fc..557ffd89 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -227,7 +227,7 @@ namespace Newtonsoft.Json.Linq foreach (object c in enumerable) { AddInternal(isLast, multiPrevious, c); - multiPrevious = (previous != null) ? previous._next : Last; + multiPrevious = (multiPrevious != null) ? multiPrevious._next : Last; } } else