From 99ed4265ef3bcc7c3914bf703bd22f416f5353cf Mon Sep 17 00:00:00 2001 From: Matt Burke Date: Mon, 12 Feb 2024 12:11:57 -0600 Subject: [PATCH] Fix exception when converting documents containing maps to JSON --- .../Extensions/Conversion.cs | 28 +++++++++++++++++++ YDotNet.Extensions/YDotNetExtensions.cs | 4 +-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs b/Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs index 26230b89..d10b2e41 100644 --- a/Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs +++ b/Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs @@ -42,6 +42,34 @@ public void ConvertTo() Assert.That(result.Value, Is.EqualTo("Hello YDotNet")); } + [Test] + public void CanParseMapsToObject() + { + // Arrange + var doc = new Doc(); + var items = doc.Array("items"); + + using (var transaction = doc.WriteTransaction()) + { + var map = Input.Map(new Dictionary() + { + ["value"] = Input.String("Hello YDotNet"), + }); + items.InsertRange(transaction, 0, map); + } + + using (var transaction = doc.ReadTransaction()) + { + var map = items.Get(transaction, 0); + + // Act + var parsed = map.To(transaction); + + // Assert + Assert.That(parsed.Value, Is.EqualTo("Hello YDotNet")); + } + } + [Test] public void ConvertToJson() { diff --git a/YDotNet.Extensions/YDotNetExtensions.cs b/YDotNet.Extensions/YDotNetExtensions.cs index dd6ac49e..1f98772b 100644 --- a/YDotNet.Extensions/YDotNetExtensions.cs +++ b/YDotNet.Extensions/YDotNetExtensions.cs @@ -91,14 +91,14 @@ static void WriteCollection(JsonArray array, Utf8JsonWriter jsonWriter, Transact static void WriteMap(Map map, Utf8JsonWriter jsonWriter, Transaction transaction) { - jsonWriter.WriteStartArray(); + jsonWriter.WriteStartObject(); foreach (var property in map.Iterate(transaction)) { WriteProperty(property.Key, property.Value, jsonWriter, transaction); } - jsonWriter.WriteEndArray(); + jsonWriter.WriteEndObject(); } static void WriteArray(Array array, Utf8JsonWriter jsonWriter, Transaction transaction)