From 66ab864e6fa4a6e7138f508cdb06fc95f3e799ac Mon Sep 17 00:00:00 2001 From: frankracis-litmos <132316804+frankracis-litmos@users.noreply.github.com> Date: Wed, 26 Jul 2023 09:29:51 -0400 Subject: [PATCH] Fix array enumerator (#8) Remove readonly from ArrayEnumeratorWrapper to allow it to move between elements. Add tests for array and object enumerators. Updated version number. --- src/JsonEasyNavigation.Tests/ArrayTests.cs | 19 ++++++++++- src/JsonEasyNavigation.Tests/PropertyTests.cs | 32 +++++++++++++++++++ .../ArrayEnumeratorWrapper.cs | 4 +-- .../JsonEasyNavigation.csproj | 2 +- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/JsonEasyNavigation.Tests/ArrayTests.cs b/src/JsonEasyNavigation.Tests/ArrayTests.cs index b7d1d80..4fc54e9 100644 --- a/src/JsonEasyNavigation.Tests/ArrayTests.cs +++ b/src/JsonEasyNavigation.Tests/ArrayTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Text.Json; using Shouldly; using Xunit; @@ -123,5 +124,21 @@ public void WhenEnumeratingKey_ShouldBeEmpty() nav.Keys.ShouldBeEmpty(); } - } + + [Fact] + public void EnumerateArray_ShouldEnumerateItems() + { + var json = @"[ ""item1"", ""item2"", ""item3"" ]"; + + var jsonDocument = JsonDocument.Parse(json); + var nav = jsonDocument.ToNavigation(); + + var list = new List(); + foreach (var item in nav) + { + list.Add(item.GetStringOrEmpty()); + } + list.ShouldBe(new[] { "item1", "item2", "item3" }); + } + } } \ No newline at end of file diff --git a/src/JsonEasyNavigation.Tests/PropertyTests.cs b/src/JsonEasyNavigation.Tests/PropertyTests.cs index b91d511..38f89c5 100644 --- a/src/JsonEasyNavigation.Tests/PropertyTests.cs +++ b/src/JsonEasyNavigation.Tests/PropertyTests.cs @@ -270,5 +270,37 @@ public void WhenHierarchyLevel1_ShouldNotExist() var item = nav["item1"]["item3"]; item.Exist.ShouldBeFalse(); } + + [Fact] + public void EnumerateObject_ShouldEnumerateProperties() + { + var json = @"{ ""item1"" : ""first"", ""item2"": ""second"" }"; + + var jsonDocument = JsonDocument.Parse(json); + var nav = jsonDocument.ToNavigation(); + + var list = new List(); + foreach (var item in nav) + { + list.Add(item.Name); + } + list.ShouldBe(new[] { "item1", "item2" }); + } + + [Fact] + public void EnumerateObject_CachedProperties_ShouldEnumerateProperties() + { + var json = @"{ ""item1"" : ""first"", ""item2"": ""second"" }"; + + var jsonDocument = JsonDocument.Parse(json); + var nav = jsonDocument.ToNavigation().WithCachedProperties(); + + var list = new List(); + foreach (var item in nav) + { + list.Add(item.Name); + } + list.ShouldBe(new[] { "item1", "item2" }); + } } } \ No newline at end of file diff --git a/src/JsonEasyNavigation/ArrayEnumeratorWrapper.cs b/src/JsonEasyNavigation/ArrayEnumeratorWrapper.cs index 821d991..6eb5dfc 100644 --- a/src/JsonEasyNavigation/ArrayEnumeratorWrapper.cs +++ b/src/JsonEasyNavigation/ArrayEnumeratorWrapper.cs @@ -5,9 +5,9 @@ namespace JsonEasyNavigation { - internal readonly struct ArrayEnumeratorWrapper : IEnumerator + internal struct ArrayEnumeratorWrapper : IEnumerator { - private readonly JsonElement.ArrayEnumerator _enumerator; + private JsonElement.ArrayEnumerator _enumerator; public ArrayEnumeratorWrapper(JsonElement jsonElement) { diff --git a/src/JsonEasyNavigation/JsonEasyNavigation.csproj b/src/JsonEasyNavigation/JsonEasyNavigation.csproj index 95c066e..884fc1a 100644 --- a/src/JsonEasyNavigation/JsonEasyNavigation.csproj +++ b/src/JsonEasyNavigation/JsonEasyNavigation.csproj @@ -3,7 +3,7 @@ net5.0;netstandard2.0 True - 1.2.0 + 1.3.0 logo.png JsonEasyNavigation Sharkadi Andrey