Skip to content

Commit 1803720

Browse files
committed
Fix bug where a JSON value can't match an earlier key
1 parent a0d7cfd commit 1803720

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.LightJson
5+
{
6+
using global::LightJson;
7+
using global::LightJson.Serialization;
8+
using Xunit;
9+
10+
public class JsonReaderTests
11+
{
12+
[Fact]
13+
public void TestKeyMatchesPreviousValue()
14+
{
15+
var jsonObject = JsonValue.Parse("{ \"x\": \"value\", \"value\": \"value\" }");
16+
Assert.NotNull(jsonObject);
17+
Assert.Equal("value", jsonObject["x"].AsString);
18+
Assert.Equal("value", jsonObject["value"].AsString);
19+
Assert.Equal(jsonObject["x"], jsonObject["value"]);
20+
}
21+
22+
[Fact]
23+
public void TestDuplicateKeys()
24+
{
25+
Assert.ThrowsAny<JsonParseException>(() => JsonValue.Parse("{ \"x\": \"value\", \"x\": \"value\" }"));
26+
}
27+
}
28+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<Compile Include="LayoutRules\SA1518UnitTests.cs" />
236236
<Compile Include="LayoutRules\SA1519UnitTests.cs" />
237237
<Compile Include="LayoutRules\SA1520UnitTests.cs" />
238+
<Compile Include="LightJson\JsonReaderTests.cs" />
238239
<Compile Include="LightJson\JsonValueTests.cs" />
239240
<Compile Include="Lightup\AccessorDeclarationSyntaxExtensionsTests.cs" />
240241
<Compile Include="Lightup\AutoWrapSeparatedSyntaxListTests.cs" />

StyleCop.Analyzers/StyleCop.Analyzers/LightJson/Serialization/JsonReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private JsonObject ReadObject(JsonObject jsonObject)
344344

345345
var key = this.ReadJsonKey();
346346

347-
if (jsonObject.Contains(key))
347+
if (jsonObject.ContainsKey(key))
348348
{
349349
throw new JsonParseException(
350350
ErrorType.DuplicateObjectKeys,

0 commit comments

Comments
 (0)