Skip to content

Commit

Permalink
Adds costomization support
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Omondi committed Aug 1, 2024
1 parent df9848e commit 389b76d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [1.10.1] - 2024-08-01

- Cleans up enum serialization to read from attributes for form and text serialization [#284](https://github.com/microsoft/kiota-dotnet/issues/284)
- Pass relevant `JsonWriterOptions` from the `KiotaJsonSerializationContext.Options` to the `Utf8JsonWriter` when writing json to enable customization. [#281](https://github.com/microsoft/kiota-dotnet/issues/281)

## [1.10.0] - 2024-07-17

Expand Down
6 changes: 5 additions & 1 deletion src/serialization/json/JsonSerializationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public JsonSerializationWriter()
public JsonSerializationWriter(KiotaJsonSerializationContext kiotaJsonSerializationContext)
{
_kiotaJsonSerializationContext = kiotaJsonSerializationContext;
writer = new Utf8JsonWriter(_stream);
writer = new Utf8JsonWriter(_stream, new JsonWriterOptions
{
Encoder = kiotaJsonSerializationContext.Options.Encoder,
Indented = kiotaJsonSerializationContext.Options.WriteIndented
});
}

/// <summary>
Expand Down
63 changes: 63 additions & 0 deletions tests/serialization/json/JsonSerializationWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading;
using Microsoft.Kiota.Abstractions;
Expand Down Expand Up @@ -258,6 +259,68 @@ public void WriteGuidUsingConverter()
Assert.Equal(expectedString, serializedJsonString);
}

[Fact]
public void ForwardsOptionsToWriterFromSerializationContext()
{
// Arrange
var testEntity = new TestEntity
{
Id = "testId",
AdditionalData = new Dictionary<string, object>()
{
{"href", "https://graph.microsoft.com/users/{user-id}"},
{"unicodeName", "你好"}
}
};
var serializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.General)
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
var serializationContext = new KiotaJsonSerializationContext(serializerOptions);
using var jsonSerializerWriter = new JsonSerializationWriter(serializationContext);

// Act
jsonSerializerWriter.WriteObjectValue(string.Empty, testEntity);
var serializedStream = jsonSerializerWriter.GetSerializedContent();
using var reader = new StreamReader(serializedStream, Encoding.UTF8);
var serializedJsonString = reader.ReadToEnd();

// Assert
const string expectedString = "{\n \"id\": \"testId\",\n \"href\": \"https://graph.microsoft.com/users/{user-id}\",\n \"unicodeName\": \"你好\"\n}";
Assert.Contains("\n", serializedJsonString); // string is indented and not escaped
Assert.Contains("你好", serializedJsonString); // string is indented and not escaped
Assert.Equal(expectedString, serializedJsonString.Replace("\r", string.Empty)); // string is indented and not escaped
}

[Fact]
public void UsesDefaultOptionsToWriterFromSerializationContext()
{
// Arrange
var testEntity = new TestEntity
{
Id = "testId",
AdditionalData = new Dictionary<string, object>()
{
{"href", "https://graph.microsoft.com/users/{user-id}"},
{"unicodeName", "你好"}
}
};
using var jsonSerializerWriter = new JsonSerializationWriter(new KiotaJsonSerializationContext());

// Act
jsonSerializerWriter.WriteObjectValue(string.Empty, testEntity);
var serializedStream = jsonSerializerWriter.GetSerializedContent();
using var reader = new StreamReader(serializedStream, Encoding.UTF8);
var serializedJsonString = reader.ReadToEnd();

// Assert
var expectedString = $"{{\"id\":\"testId\",\"href\":\"https://graph.microsoft.com/users/{{user-id}}\",\"unicodeName\":\"\\u4F60\\u597D\"}}";
Assert.DoesNotContain("\n", serializedJsonString); // string is not indented and not escaped
Assert.DoesNotContain("你好", serializedJsonString); // string is not indented and not escaped
Assert.Contains("\\u4F60\\u597D", serializedJsonString); // string is not indented and not escaped
Assert.Equal(expectedString, serializedJsonString); // string is indented and not escaped
}
[Fact]
public void WriteGuidUsingNoConverter()
{
Expand Down
6 changes: 3 additions & 3 deletions tests/serialization/text/TextParseNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public void TextParseNode_GetEnumFromString()
[Fact]
public void TextParseNode_GetEnumFromEnumMember()
{
var text = "Value_2";
var text = "Item2:SubItem1";
var parseNode = new TextParseNode(text);

var result = parseNode.GetEnumValue<TestEnum>();
var result = parseNode.GetEnumValue<TestNamingEnum>();

Assert.Equal(TestEnum.SecondItem, result);
Assert.Equal(TestNamingEnum.Item2SubItem1, result);
}

[Fact]
Expand Down

0 comments on commit 389b76d

Please sign in to comment.