Skip to content

Commit 0e585bc

Browse files
committed
Fix merging keys with different types of values
1 parent 1ab8c1c commit 0e585bc

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

dotnet-json.Tests/JsonDocumentTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ public void FindValue_ReturnsCorrectValue(JToken root, string key, JValue expect
8585
.Which.Should<JValue>().Be(expected);
8686
}
8787

88+
[Fact]
89+
public void FindValue_ReturnsNullIfKeyDoesNotExist()
90+
{
91+
var document = new JsonDocument(JObject.Parse(@"{ ""key"": ""value"" }"));
92+
var actual = document.FindToken("notkey");
93+
94+
actual.Should().BeNull();
95+
}
96+
97+
[Fact]
98+
public void FindValue_ReturnsNullIfKeyDoesNotExist_ButSubkeyExists()
99+
{
100+
var document = new JsonDocument(JObject.Parse(@"{ ""key"": ""value"" }"));
101+
var actual = document.FindToken("key:nested");
102+
103+
actual.Should().BeNull();
104+
}
105+
88106
[Fact]
89107
public void SetValue_ModifiesOriginalDocument_Object()
90108
{
@@ -147,6 +165,63 @@ public void SetValue_CreatesNestedStructure_Array()
147165
.Which.Value.Should().Be("value");;
148166
}
149167

168+
[Fact]
169+
public void SetValue_ReplacesValue_WithArray()
170+
{
171+
var document = new JsonDocument(JObject.Parse(@"{ ""key"": ""value"" }"));
172+
173+
document.SetValue("key:0", "array");
174+
175+
document._json.Should().BeOfType<JObject>()
176+
.Which.Should().ContainKey("key");
177+
178+
document._json["key"].Should().BeOfType<JArray>()
179+
.Which.Should().HaveCount(1);
180+
181+
document._json["key"][0].Should().BeOfType<JValue>()
182+
.Which.Value.Should().Be("array");
183+
}
184+
185+
[Fact]
186+
public void SetValue_ReplacesValue_WithObject()
187+
{
188+
var document = new JsonDocument(JObject.Parse(@"{ ""key"": ""value"" }"));
189+
190+
document.SetValue("key:nested", "object");
191+
192+
document._json.Should().BeOfType<JObject>()
193+
.Which.Should().ContainKey("key");
194+
195+
document._json["key"].Should().BeOfType<JObject>()
196+
.Which.Should().ContainKey("nested");
197+
198+
document._json["key"]["nested"].Should().BeOfType<JValue>()
199+
.Which.Value.Should().Be("object");
200+
}
201+
202+
[Fact]
203+
public void Merge_MergesTwoDocuments()
204+
{
205+
var document1 = new JsonDocument(JObject.Parse(@"{ ""key1"": ""value1"" }"));
206+
var document2 = new JsonDocument(JObject.Parse(@"{ ""key2"": ""value2"" }"));
207+
208+
document1.Merge(document2);
209+
210+
document1._json.Should().BeOfType<JObject>()
211+
.Which.Should().ContainKeys("key1", "key1");
212+
213+
document1._json["key1"].Should().BeOfType<JValue>()
214+
.Which.Value.Should().Be("value1");
215+
document1._json["key2"].Should().BeOfType<JValue>()
216+
.Which.Value.Should().Be("value2");
217+
}
218+
219+
[Fact(Skip = "not yet implemented")]
220+
public void Merge_ReplacesWholeArray()
221+
{
222+
// TODO: Make sure Merge replaces an array instead of only update array indices.
223+
}
224+
150225
[Fact]
151226
public void SetValue_CreatesNestedStructure_Multi()
152227
{

dotnet-json/Core/JsonDocument.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ internal static IEnumerable<KeyValuePair<string, JValue>> AllValues(JToken token
109109
var subkey = subKeys[i];
110110
var isLastKey = i == subKeys.Length - 1;
111111

112-
if (current is JValue jValue && (!createNew || jValue.Value == null))
113-
throw new ArgumentException("");
112+
if (current is JValue && !createNew)
113+
return null;
114114

115115
if (int.TryParse(subkey, out _) && current is JObject && ((JObject)current).Count == 0 && current.Parent != null)
116116
{
@@ -126,10 +126,13 @@ internal static IEnumerable<KeyValuePair<string, JValue>> AllValues(JToken token
126126
continue;
127127
}
128128

129+
if (current is JValue)
130+
return null;
131+
129132
var jObject = (JObject)current!; // At this point current can only be a JObject.
130133
current = jObject[subkey];
131134

132-
if (current == null && createNew)
135+
if (createNew && (current is null || (current is JValue && !isLastKey)))
133136
{
134137
current = jObject[subkey] = isLastKey ? (JToken)new JValue((object?)null) : new JObject();
135138
}

dotnet-json/dotnet-json.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<ToolCommandName>dotnet-json</ToolCommandName>
1515
<PackageOutputPath>./nupkg</PackageOutputPath>
1616

17-
<Version>0.4.1</Version>
17+
<Version>0.4.2</Version>
1818
<PackageId>dotnet-json</PackageId>
1919
<Authors>sleeuwen</Authors>
2020
<RepositoryUrl>https://github.com/sleeuwen/dotnet-json</RepositoryUrl>

0 commit comments

Comments
 (0)