Skip to content

Commit 96704a1

Browse files
authored
change JToken.WriteTo converters parameter to params IList<JsonConverter> (#359)
1 parent 659369a commit 96704a1

9 files changed

Lines changed: 76 additions & 14 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ obj/
66
*.DotSettings.user
77
.idea/
88
*.received.*
9-
nugets/
9+
nugets/
10+
/BenchmarkDotNet.Artifacts

src/Argon/Linq/JArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ internal override JToken CloneToken() =>
174174
/// </summary>
175175
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
176176
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
177-
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
177+
public override void WriteTo(JsonWriter writer, params IList<JsonConverter> converters)
178178
{
179179
writer.WriteStartArray();
180180

src/Argon/Linq/JObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public JToken? this[string propertyName]
349349
/// </summary>
350350
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
351351
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
352-
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
352+
public override void WriteTo(JsonWriter writer, params IList<JsonConverter> converters)
353353
{
354354
writer.WriteStartObject();
355355

src/Argon/Linq/JProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public JProperty(string name, object? content)
250250
/// </summary>
251251
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
252252
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
253-
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
253+
public override void WriteTo(JsonWriter writer, params IList<JsonConverter> converters)
254254
{
255255
writer.WritePropertyName(Name);
256256

src/Argon/Linq/JToken.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,21 +419,21 @@ public void Replace(JToken value)
419419
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "WriteTo without converters is safe.")]
420420
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "WriteTo without converters is safe.")]
421421
public void WriteTo(JsonWriter writer) =>
422-
WriteTo(writer, CollectionUtils.ArrayEmpty<JsonConverter>());
422+
WriteTo(writer, []);
423423

424424
/// <summary>
425425
/// Writes this token to a <see cref="JsonWriter" />.
426426
/// </summary>
427427
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
428428
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
429-
public abstract void WriteTo(JsonWriter writer, params JsonConverter[] converters);
429+
public abstract void WriteTo(JsonWriter writer, params IList<JsonConverter> converters);
430430

431431
/// <summary>
432432
/// Returns the indented JSON for this token.
433433
/// </summary>
434434
/// <remarks>
435435
/// <c>ToString()</c> returns a non-JSON string value for tokens with a type of <see cref="JTokenType.String" />.
436-
/// If you want the JSON for all token types then you should use <see cref="WriteTo(JsonWriter, JsonConverter[])" />.
436+
/// If you want the JSON for all token types then you should use <see cref="WriteTo(JsonWriter, IList{JsonConverter})" />.
437437
/// </remarks>
438438
/// <returns>
439439
/// The indented JSON for this token.
@@ -2070,4 +2070,4 @@ object ICloneable.Clone() =>
20702070
/// <returns>A new instance of the <see cref="JToken" />.</returns>
20712071
public JToken DeepClone() =>
20722072
CloneToken();
2073-
}
2073+
}

src/Argon/Linq/JValue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,9 @@ public object? Value
657657
/// <param name="converters">A collection of <see cref="JsonConverter" />s which will be used when writing the token.</param>
658658
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
659659
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
660-
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
660+
public override void WriteTo(JsonWriter writer, params IList<JsonConverter> converters)
661661
{
662-
if (converters is {Length: > 0} && value != null)
662+
if (converters.Count > 0 && value != null)
663663
{
664664
var matchingConverter = JsonSerializer.GetMatchingConverter(converters, value.GetType());
665665
if (matchingConverter is {CanWrite: true})
@@ -819,7 +819,7 @@ public override int GetHashCode()
819819
/// </summary>
820820
/// <remarks>
821821
/// <c>ToString()</c> returns a non-JSON string value for tokens with a type of <see cref="JTokenType.String" />.
822-
/// If you want the JSON for all token types then you should use <see cref="WriteTo(JsonWriter, JsonConverter[])" />.
822+
/// If you want the JSON for all token types then you should use <see cref="WriteTo(JsonWriter, IList{JsonConverter})" />.
823823
/// </remarks>
824824
/// <returns>
825825
/// A <see cref="String" /> that represents this instance.

src/Argon/Serialization/JsonSerializerInternalWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void SerializeValue(JsonWriter writer, object? value, JsonContract? valueContrac
158158
case JsonContractType.Linq:
159159
var token = (JToken) value;
160160
OnSerializing(writer, token);
161-
token.WriteTo(writer, Serializer.Converters.ToArray());
161+
token.WriteTo(writer, Serializer.Converters);
162162
OnSerialized(writer, token);
163163
break;
164164
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2007 James Newton-King. All rights reserved.
2+
// Use of this source code is governed by The MIT License,
3+
// as found in the license.md file.
4+
5+
using BenchmarkDotNet.Attributes;
6+
7+
[MemoryDiagnoser]
8+
public class SerializeJTokenList
9+
{
10+
List<JObject> tokens = null!;
11+
JsonSerializer defaultSerializer = null!;
12+
JsonSerializer serializerWithConverters = null!;
13+
14+
[GlobalSetup]
15+
public void Setup()
16+
{
17+
tokens = [];
18+
for (var i = 0; i < 100; i++)
19+
{
20+
tokens.Add(new JObject
21+
{
22+
["id"] = i,
23+
["name"] = $"item-{i}",
24+
["active"] = i % 2 == 0,
25+
["score"] = i * 1.5
26+
});
27+
}
28+
29+
defaultSerializer = JsonSerializer.CreateDefault();
30+
31+
serializerWithConverters = JsonSerializer.CreateDefault();
32+
serializerWithConverters.Converters.Add(new StringEnumConverter());
33+
serializerWithConverters.Converters.Add(new IsoDateTimeConverter());
34+
}
35+
36+
[Benchmark]
37+
public string NoConverters()
38+
{
39+
using var stringWriter = new StringWriter();
40+
using var jsonWriter = new JsonTextWriter(stringWriter);
41+
defaultSerializer.Serialize(jsonWriter, tokens);
42+
return stringWriter.ToString();
43+
}
44+
45+
[Benchmark]
46+
public string WithConverters()
47+
{
48+
using var stringWriter = new StringWriter();
49+
using var jsonWriter = new JsonTextWriter(stringWriter);
50+
serializerWithConverters.Serialize(jsonWriter, tokens);
51+
return stringWriter.ToString();
52+
}
53+
}

src/Benchmark.Tests/Program.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66

77
public class Program
88
{
9-
public static void Main()
9+
public static void Main(string[] args)
1010
{
1111
var attribute = (AssemblyFileVersionAttribute)typeof(JsonConvert).Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))!;
1212
Console.WriteLine($"Json.NET Version: {attribute.Version}");
1313

14-
new BenchmarkSwitcher([typeof(WriteEscapedJavaScriptString)]).Run([ "*" ]);
14+
var switcher = new BenchmarkSwitcher([typeof(WriteEscapedJavaScriptString), typeof(SerializeJTokenList)]);
15+
if (args.Length == 0)
16+
{
17+
switcher.Run(["*"]);
18+
}
19+
else
20+
{
21+
switcher.Run(args);
22+
}
1523
}
1624
}

0 commit comments

Comments
 (0)