From a79266c1151aa6862233f8332ae805cb3c560a99 Mon Sep 17 00:00:00 2001 From: Jeroen Breuer Date: Thu, 29 Aug 2024 15:08:03 +0200 Subject: [PATCH] Add GetRawValue() to return raw JSON in JsonSerializedField. (#9) ## Description This pull request introduces an `GetRawValue()` method in the `JsonSerializedField` class to directly return the raw JSON string. This enhancement provides a more straightforward and efficient way to access the underlying JSON without the need for additional deserialization. ## Motivation The primary motivation for this change is to simplify the process of working with JSON data, particularly in scenarios where the JSON is deserialized, modified, and then re-serialized. In my case, I needed to deserialize JSON into a `SitecoreLayoutResponseContent` object, apply modifications, and then serialize it back to JSON. Previously, this required a custom `JsonConverter` that accessed the `_json` field via reflection. With this improvement, such workarounds are no longer necessary, making the code cleaner and more maintainable. --- .../Serialization/Fields/JsonSerializedField.cs | 9 +++++++++ .../Fields/JsonSerializedFieldTests.cs | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Sitecore.AspNetCore.SDK.LayoutService.Client/Serialization/Fields/JsonSerializedField.cs b/src/Sitecore.AspNetCore.SDK.LayoutService.Client/Serialization/Fields/JsonSerializedField.cs index ec4e340..508457d 100644 --- a/src/Sitecore.AspNetCore.SDK.LayoutService.Client/Serialization/Fields/JsonSerializedField.cs +++ b/src/Sitecore.AspNetCore.SDK.LayoutService.Client/Serialization/Fields/JsonSerializedField.cs @@ -18,6 +18,15 @@ public class JsonSerializedField(JsonDocument doc) private readonly string _json = doc != null ? doc.RootElement.GetRawText() : throw new ArgumentNullException(nameof(doc)); + /// + /// Gets the raw JSON string representation of the field. + /// + /// A string containing the raw JSON data. + public string GetRawValue() + { + return _json; + } + /// protected override object? HandleRead(Type type) { diff --git a/tests/Sitecore.AspNetCore.SDK.LayoutService.Client.Tests/Serialization/Fields/JsonSerializedFieldTests.cs b/tests/Sitecore.AspNetCore.SDK.LayoutService.Client.Tests/Serialization/Fields/JsonSerializedFieldTests.cs index 2b5ec9a..cbe12c9 100644 --- a/tests/Sitecore.AspNetCore.SDK.LayoutService.Client.Tests/Serialization/Fields/JsonSerializedFieldTests.cs +++ b/tests/Sitecore.AspNetCore.SDK.LayoutService.Client.Tests/Serialization/Fields/JsonSerializedFieldTests.cs @@ -119,6 +119,21 @@ public void TryRead_InvalidJson_ThrowsException() field.Should().BeNull(); } + [Fact] + public void GetRawValue_ReturnsExpectedJson() + { + // Arrange + const string json = "{\"value\": 100}"; + JsonDocument token = JsonDocument.Parse(json); + JsonSerializedField sut = new(token); + + // Act + string result = sut.GetRawValue(); + + // Assert + result.Should().Be(json); + } + private class ValueField : IField { public required T Value { get; set; }