Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions tools/Custom/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static string ReplaceAndRemoveSlashes(this string body)
ProcessBody(jsonToken);

// Return cleaned JSON string
return JsonConvert.SerializeObject(jsonToken, Formatting.None);
return JsonConvert.SerializeObject(jsonToken, Formatting.None, new PreserveStringConverter());
}
catch (Newtonsoft.Json.JsonException)
{
Expand All @@ -155,8 +155,9 @@ private static void ProcessBody(JToken token)
try
{
JToken parsedValue = JToken.Parse(stringValue);
property.Value = parsedValue; // Replace with unescaped JSON object
ProcessBody(parsedValue); // Recursively process
string originalToken = JsonConvert.SerializeObject(parsedValue, Formatting.None, new PreserveStringConverter()); // Ensures that the value matches the original type
property.Value = originalToken; // Replace with unescaped JSON object
ProcessBody(originalToken); // Recursively process
}
catch (Newtonsoft.Json.JsonException)
{
Expand All @@ -182,8 +183,9 @@ private static void ProcessBody(JToken token)
try
{
JToken parsedValue = JToken.Parse(stringValue);
jsonArray[i] = parsedValue; // Replace with unescaped JSON object
ProcessBody(parsedValue); // Recursively process
string originalToken = JsonConvert.SerializeObject(parsedValue, Formatting.None, new PreserveStringConverter()); // Ensures that the value matches the original type
jsonArray[i] = originalToken; // Replace with unescaped JSON object
ProcessBody(originalToken); // Recursively process
}
catch (Newtonsoft.Json.JsonException)
{
Expand Down
31 changes: 31 additions & 0 deletions tools/Custom/PreserveStringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace NamespacePrefixPlaceholder.PowerShell.JsonUtilities
{
public class PreserveStringConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string); // Only applies to string properties
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// If the value is a number but the property expects a string, return it as a string
if ((reader.TokenType == JsonToken.Integer || reader.TokenType == JsonToken.Float) && objectType == typeof(string))
{
return reader.Value?.ToString();
}

return reader?.Value; // Otherwise, keep it as is
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value); // Preserve the original type
}
}
}
56 changes: 56 additions & 0 deletions tools/Tests/JsonUtilitiesTest/JsonExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,61 @@ public void RemoveDefaultNullProperties_ShouldRemoveDefaultNullValuesInJsonObjec
Assert.False(result["body"]?["users"][0]?.ToObject<JObject>().ContainsKey("email"));
Assert.True(result["body"]?["users"][0]?["metadata"]?.ToObject<JObject>().ContainsKey("phone"));
}

[Fact]
public void NumericString_RemainsString()
{
// Arrange
JObject json = JObject.Parse(@"{
""displayname"": ""Tim"",
""position"": ""123"",
""salary"": 2000000
}");

// Act
string cleanedJson = json.ToString()?.ReplaceAndRemoveSlashes();
JObject result = JObject.Parse(cleanedJson);

// Assert
Assert.Equal("123", result["position"]?.ToString());
Assert.Equal(2000000, result["salary"]?.ToObject<int>());
}
[Fact]
public void NumericString_RemainsStringInJsonArray()
{
// Arrange
JArray json = JArray.Parse(@"[
{ ""displayname"": ""Tim"", ""position"": ""123"" }

]");

// Act
string cleanedJson = json.ToString()?.ReplaceAndRemoveSlashes();
JArray result = JArray.Parse(cleanedJson);

// Assert
Assert.Equal("123", result[0]?["position"]?.ToString());
}

[Fact]
public void NumericString_RemainsStringInNestedJsonObject()
{
// Arrange
JObject json = JObject.Parse(@"{
""body"":{
""users"": [
{ ""displayname"": ""Tim"", ""position"": ""123"" }
]
}
}");

// Act
string cleanedJson = json.ToString()?.ReplaceAndRemoveSlashes();
JObject result = JObject.Parse(cleanedJson);

// Assert
Assert.Equal("123", result["body"]?["users"][0]?["position"]?.ToString());
Assert.Equal("Tim", result["body"]?["users"][0]?["displayname"]?.ToString());
}
}

4 changes: 4 additions & 0 deletions tools/Tests/JsonUtilitiesTest/JsonUtilitiesTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<ItemGroup>
<Compile Include="../../Custom/JsonExtensions.cs">
<Link>../../Custom/JsonExtensions.cs</Link>
<Link>../../Custom/PreserveStringConverter.cs</Link>
</Compile>
<Compile Include="../../Custom/PreserveStringConverter.cs">
<Link>../../Custom/PreserveStringConverter.cs</Link>
</Compile>
</ItemGroup>

Expand Down
Loading