Skip to content

Commit

Permalink
Merge pull request #87 from akatakritos/86-crash-map
Browse files Browse the repository at this point in the history
Fix exception when converting documents containing maps to JSON
  • Loading branch information
SebastianStehle authored Feb 12, 2024
2 parents 58a35d3 + 99ed426 commit 62f53cd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions Tests/YDotNet.Tests.Unit/Extensions/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Input>()
{
["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<Expected>(transaction);

// Assert
Assert.That(parsed.Value, Is.EqualTo("Hello YDotNet"));
}
}

[Test]
public void ConvertToJson()
{
Expand Down
4 changes: 2 additions & 2 deletions YDotNet.Extensions/YDotNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 62f53cd

Please sign in to comment.