Skip to content

Commit b71c2a3

Browse files
committed
fix part 2
1 parent bfa8222 commit b71c2a3

File tree

11 files changed

+75
-36
lines changed

11 files changed

+75
-36
lines changed

src/System.Linq.Dynamic.Core.NewtonsoftJson/Config/NewtonsoftJsonParsingConfig.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public class NewtonsoftJsonParsingConfig : ParsingConfig
1010
/// <summary>
1111
/// The default ParsingConfig for <see cref="NewtonsoftJsonParsingConfig"/>.
1212
/// </summary>
13-
public new static NewtonsoftJsonParsingConfig Default { get; } = new();
13+
public new static NewtonsoftJsonParsingConfig Default { get; } = new NewtonsoftJsonParsingConfig
14+
{
15+
ConvertObjectToSupportComparison = true
16+
};
1417

1518
/// <summary>
1619
/// The default <see cref="DynamicJsonClassOptions"/> to use.
@@ -28,7 +31,7 @@ public class NewtonsoftJsonParsingConfig : ParsingConfig
2831
/// <remarks>
2932
/// Use this property to control how the normalization process handles properties that are missing or undefined.
3033
/// The selected behavior may affect the output or error handling of normalization operations.
31-
/// The default value is <see cref="NormalizationNonExistingPropertyBehavior.UseDefaultValue"/>.
34+
/// The default value is <see cref="NormalizationNonExistingPropertyBehavior.UseNull"/>.
3235
/// </remarks>
3336
public NormalizationNonExistingPropertyBehavior NormalizationNonExistingPropertyValueBehavior { get; set; }
3437
}

src/System.Linq.Dynamic.Core.NewtonsoftJson/Config/NormalizationNonExistingPropertyBehavior.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
public enum NormalizationNonExistingPropertyBehavior
77
{
88
/// <summary>
9-
/// Specifies that the default value should be used.
9+
/// Specifies that a null value should be used.
1010
/// </summary>
11-
UseDefaultValue = 0,
11+
UseNull = 0,
1212

1313
/// <summary>
14-
/// Specifies that null values should be used.
14+
/// Specifies that the default value should be used.
1515
/// </summary>
16-
UseNull = 1
16+
UseDefaultValue = 1
1717
}

src/System.Linq.Dynamic.Core.NewtonsoftJson/Extensions/JObjectExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ private class JTokenResolvers : Dictionary<JTokenType, Func<JToken, DynamicJsonC
4444
foreach (var prop in src.Properties())
4545
{
4646
var value = Resolvers[prop.Type](prop.Value, options);
47-
if (value != null)
48-
{
49-
dynamicPropertiesWithValue.Add(new DynamicPropertyWithValue(prop.Name, value));
50-
}
47+
dynamicPropertiesWithValue.Add(new DynamicPropertyWithValue(prop.Name, value));
5148
}
5249

5350
return DynamicClassFactory.CreateInstance(dynamicPropertiesWithValue);

src/System.Linq.Dynamic.Core.NewtonsoftJson/NewtonsoftJsonExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,11 @@ private static JArray ToJArray(Func<IQueryable> func)
871871

872872
private static IQueryable ToQueryable(JArray source, NewtonsoftJsonParsingConfig? config = null)
873873
{
874-
var normalized = config?.Normalize == true ?
875-
NormalizeUtils.NormalizeArray(source, config.NormalizationNonExistingPropertyValueBehavior):
874+
config = config ?? NewtonsoftJsonParsingConfig.Default;
875+
config.ConvertObjectToSupportComparison = true;
876+
877+
var normalized = config.Normalize == true ?
878+
NormalizeUtils.NormalizeArray(source, config.NormalizationNonExistingPropertyValueBehavior) :
876879
source;
877880

878881
return normalized

src/System.Linq.Dynamic.Core.NewtonsoftJson/Utils/NormalizeUtils.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static JObject NormalizeObject(JObject source, Dictionary<string, JsonVa
8686
}
8787
else
8888
{
89-
result[key] = normalizationBehavior == NormalizationNonExistingPropertyBehavior.UseDefaultValue ? GetDefaultValue(schema[key]) : JValue.CreateNull();
89+
result[key] = GetDefaultOrNullValue(normalizationBehavior, schema[key]);
9090
}
9191
}
9292
}
@@ -105,7 +105,7 @@ private static JObject CreateEmptyObject(Dictionary<string, JsonValueInfo> schem
105105
}
106106
else
107107
{
108-
obj[key] = normalizationBehavior == NormalizationNonExistingPropertyBehavior.UseDefaultValue ? GetDefaultValue(schema[key]) : JValue.CreateNull();
108+
obj[key] = GetDefaultOrNullValue(normalizationBehavior, schema[key]);
109109
}
110110
}
111111

@@ -125,7 +125,28 @@ private static JToken GetDefaultValue(JsonValueInfo jType)
125125
JTokenType.Integer => default(int),
126126
JTokenType.String => string.Empty,
127127
JTokenType.TimeSpan => TimeSpan.MinValue,
128+
_ => GetNullValue(jType),
129+
};
130+
}
131+
132+
private static JValue GetNullValue(JsonValueInfo jType)
133+
{
134+
return jType.Type switch
135+
{
136+
JTokenType.Boolean => new JValue((bool?)null),
137+
JTokenType.Bytes => new JValue((byte[]?)null),
138+
JTokenType.Date => new JValue((DateTime?)null),
139+
JTokenType.Float => new JValue((float?)null),
140+
JTokenType.Guid => new JValue((Guid?)null),
141+
JTokenType.Integer => new JValue((int?)null),
142+
JTokenType.String => new JValue((string?)null),
143+
JTokenType.TimeSpan => new JValue((TimeSpan?)null),
128144
_ => JValue.CreateNull(),
129145
};
130146
}
147+
148+
private static JToken GetDefaultOrNullValue(NormalizationNonExistingPropertyBehavior behavior, JsonValueInfo jType)
149+
{
150+
return behavior == NormalizationNonExistingPropertyBehavior.UseDefaultValue ? GetDefaultValue(jType) : GetNullValue(jType);
151+
}
131152
}

src/System.Linq.Dynamic.Core.SystemTextJson/Config/NormalizationNonExistingPropertyBehavior.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
public enum NormalizationNonExistingPropertyBehavior
77
{
88
/// <summary>
9-
/// Specifies that the default value should be used.
9+
/// Specifies that a null value should be used.
1010
/// </summary>
11-
UseDefaultValue = 0,
11+
UseNull = 0,
1212

1313
/// <summary>
14-
/// Specifies that null values should be used.
14+
/// Specifies that the default value should be used.
1515
/// </summary>
16-
UseNull = 1
16+
UseDefaultValue = 1
1717
}

src/System.Linq.Dynamic.Core.SystemTextJson/Config/SystemTextJsonParsingConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class SystemTextJsonParsingConfig : ParsingConfig
2424
/// <remarks>
2525
/// Use this property to control how the normalization process handles properties that are missing or undefined.
2626
/// The selected behavior may affect the output or error handling of normalization operations.
27-
/// The default value is <see cref="NormalizationNonExistingPropertyBehavior.UseDefaultValue"/>.
27+
/// The default value is <see cref="NormalizationNonExistingPropertyBehavior.UseNull"/>.
2828
/// </remarks>
2929
public NormalizationNonExistingPropertyBehavior NormalizationNonExistingPropertyValueBehavior { get; set; }
3030
}

src/System.Linq.Dynamic.Core.SystemTextJson/Extensions/JsonDocumentExtensions.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ private class JTokenResolvers : Dictionary<JsonValueKind, Func<JsonElement, Dyna
3434
foreach (var prop in src.Value.EnumerateObject())
3535
{
3636
var value = Resolvers[prop.Value.ValueKind](prop.Value, options);
37-
if (value != null)
38-
{
39-
dynamicPropertiesWithValue.Add(new DynamicPropertyWithValue(prop.Name, value));
40-
}
37+
dynamicPropertiesWithValue.Add(new DynamicPropertyWithValue(prop.Name, value));
4138
}
4239

4340
return DynamicClassFactory.CreateInstance(dynamicPropertiesWithValue);
@@ -129,7 +126,7 @@ private static IEnumerable ConvertJsonElementToEnumerable(JsonElement arg, Dynam
129126
private static IEnumerable ConvertToTypedArray(IEnumerable<object?> src, Type newType)
130127
{
131128
var method = ConvertToTypedArrayGenericMethod.MakeGenericMethod(newType);
132-
return (IEnumerable)method.Invoke(null, new object[] { src })!;
129+
return (IEnumerable)method.Invoke(null, [src])!;
133130
}
134131

135132
private static readonly MethodInfo ConvertToTypedArrayGenericMethod = typeof(JsonDocumentExtensions).GetMethod(nameof(ConvertToTypedArrayGeneric), BindingFlags.NonPublic | BindingFlags.Static)!;

src/System.Linq.Dynamic.Core.SystemTextJson/Utils/NormalizeUtils.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private static JsonObject NormalizeObject(JsonObject source, Dictionary<string,
113113
}
114114
else
115115
{
116-
result[key] = normalizationBehavior == NormalizationNonExistingPropertyBehavior.UseDefaultValue ? GetDefaultValue(jType) : null;
116+
result[key] = GetDefaultOrNullValue(normalizationBehavior, jType);
117117
}
118118
}
119119
}
@@ -135,7 +135,7 @@ private static JsonObject CreateEmptyObject(Dictionary<string, JsonValueInfo> sc
135135
}
136136
else
137137
{
138-
obj[key] = normalizationBehavior == NormalizationNonExistingPropertyBehavior.UseDefaultValue ? GetDefaultValue(jType) : null;
138+
obj[key] = GetDefaultOrNullValue(normalizationBehavior, jType);
139139
}
140140
}
141141

@@ -151,7 +151,25 @@ private static JsonObject CreateEmptyObject(Dictionary<string, JsonValueInfo> sc
151151
JsonValueKind.Number => default(int),
152152
JsonValueKind.String => string.Empty,
153153
JsonValueKind.True => false,
154+
_ => GetNullValue(jType),
155+
};
156+
}
157+
158+
private static JsonNode? GetNullValue(JsonValueInfo jType)
159+
{
160+
return jType.Type switch
161+
{
162+
JsonValueKind.Array => null,
163+
JsonValueKind.False => JsonValue.Create<bool?>(false),
164+
JsonValueKind.Number => JsonValue.Create<int?>(null),
165+
JsonValueKind.String => JsonValue.Create<string?>(null),
166+
JsonValueKind.True => JsonValue.Create<bool?>(true),
154167
_ => null,
155168
};
156169
}
170+
171+
private static JsonNode? GetDefaultOrNullValue(NormalizationNonExistingPropertyBehavior behavior, JsonValueInfo jType)
172+
{
173+
return behavior == NormalizationNonExistingPropertyBehavior.UseDefaultValue ? GetDefaultValue(jType) : GetNullValue(jType);
174+
}
157175
}

test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,6 @@ public void NormalizeArray_When_NormalizeIsFalse_ShouldThrow()
568568
Action act = () => JArray.Parse(array).Where(config, "Age >= 30");
569569

570570
// Assert
571-
act.Should().Throw<InvalidOperationException>().WithMessage("The binary operator GreaterThanOrEqual is not defined for the types 'System.Object' and 'System.Int32'.");
571+
act.Should().Throw<InvalidOperationException>().WithMessage("Unable to find property 'Age' on type '<>*");
572572
}
573573
}

0 commit comments

Comments
 (0)