Skip to content

Commit 66b30d9

Browse files
Modernize System.Text.Json product code
Use C# 14 field-backed properties for private state used only by accessors, and expression-bodied members for single-expression methods. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 69ae6b80-f573-41bd-8cf9-e0f858510bbe
1 parent af85196 commit 66b30d9

8 files changed

Lines changed: 56 additions & 115 deletions

File tree

src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,10 +2603,8 @@ private bool SkipMultiLineCommentMultiSegment(ReadOnlySpan<byte> localBuffer)
26032603
}
26042604
}
26052605

2606-
private PartialStateForRollback CaptureState()
2607-
{
2608-
return new PartialStateForRollback(_totalConsumed, _bytePositionInLine, _consumed, _currentPosition);
2609-
}
2606+
private PartialStateForRollback CaptureState() =>
2607+
new PartialStateForRollback(_totalConsumed, _bytePositionInLine, _consumed, _currentPosition);
26102608

26112609
private readonly struct PartialStateForRollback
26122610
{

src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,56 +45,39 @@ public JsonSchema() { }
4545
/// </summary>
4646
private readonly bool? _trueOrFalse;
4747

48-
public string? Ref { get => _ref; set { VerifyMutable(); _ref = value; } }
49-
private string? _ref;
48+
public string? Ref { get; set { VerifyMutable(); field = value; } }
5049

51-
public string? Comment { get => _comment; set { VerifyMutable(); _comment = value; } }
52-
private string? _comment;
50+
public string? Comment { get; set { VerifyMutable(); field = value; } }
5351

54-
public JsonSchemaType Type { get => _type; set { VerifyMutable(); _type = value; } }
55-
private JsonSchemaType _type = JsonSchemaType.Any;
52+
public JsonSchemaType Type { get; set { VerifyMutable(); field = value; } } = JsonSchemaType.Any;
5653

57-
public string? Format { get => _format; set { VerifyMutable(); _format = value; } }
58-
private string? _format;
54+
public string? Format { get; set { VerifyMutable(); field = value; } }
5955

60-
public string? Pattern { get => _pattern; set { VerifyMutable(); _pattern = value; } }
61-
private string? _pattern;
56+
public string? Pattern { get; set { VerifyMutable(); field = value; } }
6257

63-
public JsonNode? Constant { get => _constant; set { VerifyMutable(); _constant = value; } }
64-
private JsonNode? _constant;
58+
public JsonNode? Constant { get; set { VerifyMutable(); field = value; } }
6559

66-
public List<KeyValuePair<string, JsonSchema>>? Properties { get => _properties; set { VerifyMutable(); _properties = value; } }
67-
private List<KeyValuePair<string, JsonSchema>>? _properties;
60+
public List<KeyValuePair<string, JsonSchema>>? Properties { get; set { VerifyMutable(); field = value; } }
6861

69-
public List<string>? Required { get => _required; set { VerifyMutable(); _required = value; } }
70-
private List<string>? _required;
62+
public List<string>? Required { get; set { VerifyMutable(); field = value; } }
7163

72-
public JsonSchema? Items { get => _items; set { VerifyMutable(); _items = value; } }
73-
private JsonSchema? _items;
64+
public JsonSchema? Items { get; set { VerifyMutable(); field = value; } }
7465

75-
public JsonSchema? AdditionalProperties { get => _additionalProperties; set { VerifyMutable(); _additionalProperties = value; } }
76-
private JsonSchema? _additionalProperties;
66+
public JsonSchema? AdditionalProperties { get; set { VerifyMutable(); field = value; } }
7767

78-
public JsonArray? Enum { get => _enum; set { VerifyMutable(); _enum = value; } }
79-
private JsonArray? _enum;
68+
public JsonArray? Enum { get; set { VerifyMutable(); field = value; } }
8069

81-
public JsonSchema? Not { get => _not; set { VerifyMutable(); _not = value; } }
82-
private JsonSchema? _not;
70+
public JsonSchema? Not { get; set { VerifyMutable(); field = value; } }
8371

84-
public List<JsonSchema>? AnyOf { get => _anyOf; set { VerifyMutable(); _anyOf = value; } }
85-
private List<JsonSchema>? _anyOf;
72+
public List<JsonSchema>? AnyOf { get; set { VerifyMutable(); field = value; } }
8673

87-
public bool HasDefaultValue { get => _hasDefaultValue; set { VerifyMutable(); _hasDefaultValue = value; } }
88-
private bool _hasDefaultValue;
74+
public bool HasDefaultValue { get; set { VerifyMutable(); field = value; } }
8975

90-
public JsonNode? DefaultValue { get => _defaultValue; set { VerifyMutable(); _defaultValue = value; } }
91-
private JsonNode? _defaultValue;
76+
public JsonNode? DefaultValue { get; set { VerifyMutable(); field = value; } }
9277

93-
public int? MinLength { get => _minLength; set { VerifyMutable(); _minLength = value; } }
94-
private int? _minLength;
78+
public int? MinLength { get; set { VerifyMutable(); field = value; } }
9579

96-
public int? MaxLength { get => _maxLength; set { VerifyMutable(); _maxLength = value; } }
97-
private int? _maxLength;
80+
public int? MaxLength { get; set { VerifyMutable(); field = value; } }
9881

9982
public bool? Deprecated { get => _deprecated; set { VerifyMutable(); _deprecated = value; } }
10083
private bool? _deprecated;

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ConfigurationList.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,16 @@ public void Clear()
6262
OnCollectionModified();
6363
}
6464

65-
public bool Contains(TItem item)
66-
{
67-
return _list.Contains(item);
68-
}
65+
public bool Contains(TItem item) => _list.Contains(item);
6966

7067
public void CopyTo(TItem[] array, int arrayIndex)
7168
{
7269
_list.CopyTo(array, arrayIndex);
7370
}
7471

75-
public List<TItem>.Enumerator GetEnumerator()
76-
{
77-
return _list.GetEnumerator();
78-
}
72+
public List<TItem>.Enumerator GetEnumerator() => _list.GetEnumerator();
7973

80-
public int IndexOf(TItem item)
81-
{
82-
return _list.IndexOf(item);
83-
}
74+
public int IndexOf(TItem item) => _list.IndexOf(item);
8475

8576
public void Insert(int index, TItem item)
8677
{

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ internal JsonConverter()
3939

4040
internal ConverterStrategy ConverterStrategy
4141
{
42-
get => _converterStrategy;
42+
get;
4343
init
4444
{
4545
CanUseDirectReadOrWrite = value == ConverterStrategy.Value && IsInternalConverter;
4646
RequiresReadAhead = value == ConverterStrategy.Value;
47-
_converterStrategy = value;
47+
field = value;
4848
}
4949
}
5050

51-
private ConverterStrategy _converterStrategy;
52-
5351
/// <summary>
5452
/// Invoked by the base contructor to populate the initial value of the <see cref="ConverterStrategy"/> property.
5553
/// Used for declaring the default strategy for specific converter hierarchies without explicitly setting in a constructor.

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPolymorphismOptions.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ namespace System.Text.Json.Serialization.Metadata
1313
public class JsonPolymorphismOptions
1414
{
1515
private DerivedTypeList? _derivedTypes;
16-
private bool _ignoreUnrecognizedTypeDiscriminators;
17-
private JsonUnknownDerivedTypeHandling _unknownDerivedTypeHandling;
18-
private string? _typeDiscriminatorPropertyName;
1916
private bool _isConfigured;
2017

2118
/// <summary>
@@ -40,12 +37,12 @@ public JsonPolymorphismOptions()
4037
/// </exception>
4138
public bool IgnoreUnrecognizedTypeDiscriminators
4239
{
43-
get => _ignoreUnrecognizedTypeDiscriminators;
40+
get;
4441
set
4542
{
4643
VerifyMutable();
4744
_isConfigured = true;
48-
_ignoreUnrecognizedTypeDiscriminators = value;
45+
field = value;
4946
}
5047
}
5148

@@ -57,12 +54,12 @@ public bool IgnoreUnrecognizedTypeDiscriminators
5754
/// </exception>
5855
public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling
5956
{
60-
get => _unknownDerivedTypeHandling;
57+
get;
6158
set
6259
{
6360
VerifyMutable();
6461
_isConfigured = true;
65-
_unknownDerivedTypeHandling = value;
62+
field = value;
6663
}
6764
}
6865

@@ -76,12 +73,12 @@ public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling
7673
[AllowNull]
7774
public string TypeDiscriminatorPropertyName
7875
{
79-
get => _typeDiscriminatorPropertyName ?? JsonSerializer.TypePropertyName;
76+
get => field ?? JsonSerializer.TypePropertyName;
8077
set
8178
{
8279
VerifyMutable();
8380
_isConfigured = true;
84-
_typeDiscriminatorPropertyName = value;
81+
field = value;
8582
}
8683
}
8784

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ internal JsonConverter EffectiveConverter
4747
/// </remarks>
4848
public JsonConverter? CustomConverter
4949
{
50-
get => _customConverter;
50+
get;
5151
set
5252
{
5353
VerifyMutable();
54-
_customConverter = value;
54+
field = value;
5555
}
5656
}
5757

58-
private JsonConverter? _customConverter;
59-
6058
/// <summary>
6159
/// Gets or sets a getter delegate for the property.
6260
/// </summary>
@@ -201,7 +199,7 @@ public ICustomAttributeProvider? AttributeProvider
201199
/// </remarks>
202200
public JsonObjectCreationHandling? ObjectCreationHandling
203201
{
204-
get => _objectCreationHandling;
202+
get;
205203
set
206204
{
207205
VerifyMutable();
@@ -214,11 +212,10 @@ public JsonObjectCreationHandling? ObjectCreationHandling
214212
}
215213
}
216214

217-
_objectCreationHandling = value;
215+
field = value;
218216
}
219217
}
220218

221-
private JsonObjectCreationHandling? _objectCreationHandling;
222219
internal JsonObjectCreationHandling EffectiveObjectCreationHandling { get; private set; }
223220

224221
internal string? MemberName { get; set; } // Do not rename (legacy schema generation)
@@ -316,7 +313,7 @@ public bool IsSetNullable
316313
/// </remarks>
317314
public bool IsExtensionData
318315
{
319-
get => _isExtensionDataProperty;
316+
get;
320317
set
321318
{
322319
VerifyMutable();
@@ -326,12 +323,10 @@ public bool IsExtensionData
326323
ThrowHelper.ThrowInvalidOperationException_SerializationDataExtensionPropertyInvalid(this);
327324
}
328325

329-
_isExtensionDataProperty = value;
326+
field = value;
330327
}
331328
}
332329

333-
private bool _isExtensionDataProperty;
334-
335330
/// <summary>
336331
/// Specifies whether the current property is required for deserialization to be successful.
337332
/// </summary>
@@ -832,16 +827,14 @@ public string Name
832827
/// </remarks>
833828
public int Order
834829
{
835-
get => _order;
830+
get;
836831
set
837832
{
838833
VerifyMutable();
839-
_order = value;
834+
field = value;
840835
}
841836
}
842837

843-
private int _order;
844-
845838
internal bool ReadJsonAndAddExtensionProperty(
846839
object obj,
847840
scoped ref ReadStack state,
@@ -1035,16 +1028,14 @@ internal JsonTypeInfo JsonTypeInfo
10351028
/// </remarks>
10361029
public JsonNumberHandling? NumberHandling
10371030
{
1038-
get => _numberHandling;
1031+
get;
10391032
set
10401033
{
10411034
VerifyMutable();
1042-
_numberHandling = value;
1035+
field = value;
10431036
}
10441037
}
10451038

1046-
private JsonNumberHandling? _numberHandling;
1047-
10481039
/// <summary>
10491040
/// Number handling after considering options and declaring type number handling
10501041
/// </summary>

0 commit comments

Comments
 (0)