-Fixed JTokenWriter error with duplicate properties
This commit is contained in:
@@ -265,6 +265,28 @@ namespace Newtonsoft.Json.Tests.Linq
|
|||||||
]", writer.Token.ToString());
|
]", writer.Token.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void WriteDuplicatePropertyName()
|
||||||
|
{
|
||||||
|
JTokenWriter writer = new JTokenWriter();
|
||||||
|
|
||||||
|
writer.WriteStartObject();
|
||||||
|
|
||||||
|
writer.WritePropertyName("prop1");
|
||||||
|
writer.WriteStartObject();
|
||||||
|
writer.WriteEndObject();
|
||||||
|
|
||||||
|
writer.WritePropertyName("prop1");
|
||||||
|
writer.WriteStartArray();
|
||||||
|
writer.WriteEndArray();
|
||||||
|
|
||||||
|
writer.WriteEndObject();
|
||||||
|
|
||||||
|
StringAssert.AreEqual(@"{
|
||||||
|
""prop1"": []
|
||||||
|
}", writer.Token.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void DateTimeZoneHandling()
|
public void DateTimeZoneHandling()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8630,11 +8630,11 @@ Path '', line 1, position 1.");
|
|||||||
[Test]
|
[Test]
|
||||||
public void DuplicatePropertiesInNestedObject()
|
public void DuplicatePropertiesInNestedObject()
|
||||||
{
|
{
|
||||||
ExceptionAssert.Throws<ArgumentException>(() =>
|
string content = @"{""result"":{""time"":1408188592,""time"":1408188593},""error"":null,""id"":""1""}";
|
||||||
{
|
JObject o = JsonConvert.DeserializeObject<JObject>(content);
|
||||||
string content = @"{""result"":{""time"":1408188592,""time"":1408188593},""error"":null,""id"":""1""}";
|
int time = (int)o["result"]["time"];
|
||||||
JsonConvert.DeserializeObject<JObject>(content);
|
|
||||||
}, "Can not add property time to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.");
|
Assert.AreEqual(1408188593, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|||||||
@@ -166,9 +166,17 @@ namespace Newtonsoft.Json.Linq
|
|||||||
/// <param name="name">The name of the property.</param>
|
/// <param name="name">The name of the property.</param>
|
||||||
public override void WritePropertyName(string name)
|
public override void WritePropertyName(string name)
|
||||||
{
|
{
|
||||||
|
JObject o = _parent as JObject;
|
||||||
|
if (o != null)
|
||||||
|
{
|
||||||
|
// avoid duplicate property name exception
|
||||||
|
// last property name wins
|
||||||
|
o.Remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
AddParent(new JProperty(name));
|
AddParent(new JProperty(name));
|
||||||
|
|
||||||
// don't set state until after in case of an error such as duplicate property names
|
// don't set state until after in case of an error
|
||||||
// incorrect state will cause issues if writer is disposed when closing open properties
|
// incorrect state will cause issues if writer is disposed when closing open properties
|
||||||
base.WritePropertyName(name);
|
base.WritePropertyName(name);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -3,7 +3,6 @@
|
|||||||
<packageSources>
|
<packageSources>
|
||||||
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
|
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
|
||||||
<clear />
|
<clear />
|
||||||
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
|
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||||
<add key="nuget.org/api/v2" value="https://nuget.org/api/v2/" />
|
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|||||||
Reference in New Issue
Block a user