Skip to content

Commit 0dcb4a6

Browse files
committed
Cleanup + a few bugfixes
1 parent 7483c60 commit 0dcb4a6

File tree

7 files changed

+41
-33
lines changed

7 files changed

+41
-33
lines changed

src/Features/JsonPatch.SystemTextJson/src/Adapters/ObjectAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void Copy(Operation operation, object objectToApplyTo)
183183
if (TryGetValue(operation.from, objectToApplyTo, operation, out var propertyValue))
184184
{
185185
// Create deep copy
186-
var copyResult = ConversionResultProvider.CopyTo(propertyValue, propertyValue?.GetType());
186+
var copyResult = ConversionResultProvider.CopyTo(propertyValue, propertyValue?.GetType(), SerializerOptions);
187187
if (copyResult.CanBeConverted)
188188
{
189189
Add(operation.path, copyResult.ConvertedInstance, objectToApplyTo, operation);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Text.Json;
5+
using System.Text.Json.Nodes;
6+
7+
namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Helpers;
8+
9+
internal static class JsonUtilities
10+
{
11+
public static bool DeepEquals(object a, object b, JsonSerializerOptions serializerOptions)
12+
{
13+
return JsonObject.DeepEquals(
14+
JsonSerializer.SerializeToNode(a, serializerOptions),
15+
JsonSerializer.SerializeToNode(b, serializerOptions));
16+
}
17+
}

src/Features/JsonPatch.SystemTextJson/src/Internal/ConversionResultProvider.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Internal;
99

1010
internal static class ConversionResultProvider
1111
{
12-
public static ConversionResult ConvertTo(object value, Type typeToConvertTo)
13-
{
14-
return ConvertTo(value, typeToConvertTo, null);
15-
}
16-
1712
internal static ConversionResult ConvertTo(object value, Type typeToConvertTo, JsonSerializerOptions serializerOptions)
1813
{
1914
if (value == null)
@@ -35,8 +30,7 @@ internal static ConversionResult ConvertTo(object value, Type typeToConvertTo, J
3530

3631
try
3732
{
38-
var serializedDocument = JsonSerializer.Serialize(value, serializerOptions);
39-
var deserialized = JsonSerializer.Deserialize(serializedDocument, typeToConvertTo, serializerOptions);
33+
var deserialized = ConvertToTargetType(value, typeToConvertTo, serializerOptions);
4034
return new ConversionResult(true, deserialized);
4135
}
4236
catch
@@ -45,7 +39,7 @@ internal static ConversionResult ConvertTo(object value, Type typeToConvertTo, J
4539
}
4640
}
4741

48-
public static ConversionResult CopyTo(object value, Type typeToConvertTo)
42+
internal static ConversionResult CopyTo(object value, Type typeToConvertTo, JsonSerializerOptions serializerOptions)
4943
{
5044
var targetType = typeToConvertTo;
5145
if (value == null)
@@ -67,7 +61,7 @@ public static ConversionResult CopyTo(object value, Type typeToConvertTo)
6761

6862
try
6963
{
70-
var deserialized = JsonSerializer.Deserialize(JsonSerializer.Serialize(value), targetType);
64+
var deserialized = ConvertToTargetType(value, targetType, serializerOptions);
7165
return new ConversionResult(true, deserialized);
7266
}
7367
catch
@@ -87,4 +81,9 @@ private static bool IsNullableType(Type type)
8781
// reference types are always nullable
8882
return true;
8983
}
84+
85+
private static object ConvertToTargetType(object value, Type targetType, JsonSerializerOptions serializerOptions)
86+
{
87+
return JsonSerializer.Deserialize(JsonSerializer.Serialize(value, serializerOptions), targetType, serializerOptions);
88+
}
9089
}

src/Features/JsonPatch.SystemTextJson/src/Internal/DictionaryAdapterOfTU.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Collections.Generic;
55
using System.Text.Json;
66
using System.Text.Json.Nodes;
7-
using System.Text.Json.Serialization.Metadata;
7+
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Helpers;
88

99
namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Internal;
1010

@@ -17,7 +17,7 @@ public virtual bool TryAdd(
1717
object value,
1818
out string errorMessage)
1919
{
20-
var key = ExtractKeyFromSegment(segment);
20+
var key = segment;
2121
var dictionary = (IDictionary<TKey, TValue>)target;
2222

2323
// As per JsonPatch spec, if a key already exists, adding should replace the existing value
@@ -43,7 +43,7 @@ public virtual bool TryGet(
4343
out object value,
4444
out string errorMessage)
4545
{
46-
var key = ExtractKeyFromSegment(segment);
46+
var key = segment;
4747
var dictionary = (IDictionary<TKey, TValue>)target;
4848

4949
if (!TryConvertKey(key, out var convertedKey, out errorMessage))
@@ -70,7 +70,7 @@ public virtual bool TryRemove(
7070
JsonSerializerOptions serializerOptions,
7171
out string errorMessage)
7272
{
73-
var key = ExtractKeyFromSegment(segment);
73+
var key = segment;
7474
var dictionary = (IDictionary<TKey, TValue>)target;
7575

7676
if (!TryConvertKey(key, out var convertedKey, out errorMessage))
@@ -96,7 +96,7 @@ public virtual bool TryReplace(
9696
object value,
9797
out string errorMessage)
9898
{
99-
var key = ExtractKeyFromSegment(segment);
99+
var key = segment;
100100
var dictionary = (IDictionary<TKey, TValue>)target;
101101

102102
if (!TryConvertKey(key, out var convertedKey, out errorMessage))
@@ -129,7 +129,7 @@ public virtual bool TryTest(
129129
object value,
130130
out string errorMessage)
131131
{
132-
var key = ExtractKeyFromSegment(segment);
132+
var key = segment;
133133
var dictionary = (IDictionary<TKey, TValue>)target;
134134

135135
if (!TryConvertKey(key, out var convertedKey, out errorMessage))
@@ -156,7 +156,7 @@ public virtual bool TryTest(
156156
return false;
157157
}
158158

159-
if (!JsonObject.DeepEquals(JsonSerializer.SerializeToNode(currentValue), JsonSerializer.SerializeToNode(convertedValue)))
159+
if (!JsonUtilities.DeepEquals(currentValue, convertedValue, serializerOptions))
160160
{
161161
errorMessage = Resources.FormatValueNotEqualToTestValue(currentValue, value, segment);
162162
return false;
@@ -175,7 +175,7 @@ public virtual bool TryTraverse(
175175
out object nextTarget,
176176
out string errorMessage)
177177
{
178-
var key = ExtractKeyFromSegment(segment);
178+
var key = segment;
179179
var dictionary = (IDictionary<TKey, TValue>)target;
180180

181181
if (!TryConvertKey(key, out var convertedKey, out errorMessage))
@@ -198,12 +198,7 @@ public virtual bool TryTraverse(
198198
}
199199
}
200200

201-
private static string ExtractKeyFromSegment(string segment)
202-
{
203-
return segment.ToString();
204-
}
205-
206-
protected virtual bool TryConvertKey(string key, out TKey convertedKey, out string errorMessage)
201+
private static bool TryConvertKey(string key, out TKey convertedKey, out string errorMessage)
207202
{
208203
var options = new JsonSerializerOptions() { NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString };
209204
var conversionResult = ConversionResultProvider.ConvertTo(key, typeof(TKey), options);
@@ -221,12 +216,7 @@ protected virtual bool TryConvertKey(string key, out TKey convertedKey, out stri
221216
}
222217
}
223218

224-
protected virtual bool TryConvertValue(object value, out TValue convertedValue, out string errorMessage)
225-
{
226-
return TryConvertValue(value, null, out convertedValue, out errorMessage);
227-
}
228-
229-
protected virtual bool TryConvertValue(object value, JsonSerializerOptions serializerOptions, out TValue convertedValue, out string errorMessage)
219+
private static bool TryConvertValue(object value, JsonSerializerOptions serializerOptions, out TValue convertedValue, out string errorMessage)
230220
{
231221
var conversionResult = ConversionResultProvider.ConvertTo(value, typeof(TValue), serializerOptions);
232222
if (conversionResult.CanBeConverted)

src/Features/JsonPatch.SystemTextJson/src/Internal/JsonObjectAdapter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Text.Json;
55
using System.Text.Json.Nodes;
6+
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Helpers;
67

78
namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Internal;
89

@@ -105,7 +106,7 @@ public virtual bool TryTest(
105106
return false;
106107
}
107108

108-
if (!JsonObject.DeepEquals(JsonSerializer.SerializeToNode(currentValue, serializerOptions), JsonSerializer.SerializeToNode(value, serializerOptions)))
109+
if (!JsonUtilities.DeepEquals(currentValue, value, serializerOptions))
109110
{
110111
errorMessage = Resources.FormatValueNotEqualToTestValue(currentValue, value, segment);
111112
return false;

src/Features/JsonPatch.SystemTextJson/src/Internal/ListAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public virtual bool TryTest(object target, string segment, JsonSerializerOptions
134134

135135
var currentValue = GenericListOrJsonArrayUtilities.GetElementAt(target, positionInfo.Index);
136136

137-
if (!JsonObject.DeepEquals(JsonSerializer.SerializeToNode(currentValue), JsonSerializer.SerializeToNode(convertedValue)))
137+
if (!JsonUtilities.DeepEquals(currentValue, convertedValue, serializerOptions))
138138
{
139139
errorMessage = Resources.FormatValueAtListPositionNotEqualToTestValue(currentValue, value, positionInfo.Index);
140140
return false;

src/Features/JsonPatch.SystemTextJson/src/Internal/PocoAdapter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.Json;
77
using System.Text.Json.Nodes;
88
using System.Text.Json.Serialization.Metadata;
9+
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Helpers;
910

1011
namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson.Internal;
1112

@@ -158,7 +159,7 @@ public virtual bool TryTest(
158159
}
159160

160161
var currentValue = jsonProperty.Get(target);
161-
if (!JsonObject.DeepEquals(JsonSerializer.SerializeToNode(currentValue), JsonSerializer.SerializeToNode(convertedValue)))
162+
if (!JsonUtilities.DeepEquals(currentValue, convertedValue, serializerOptions))
162163
{
163164
errorMessage = Resources.FormatValueNotEqualToTestValue(currentValue, value, segment);
164165
return false;

0 commit comments

Comments
 (0)