Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes #2852

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 33 additions & 52 deletions Src/Newtonsoft.Json/JsonConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,57 +463,36 @@ public static string ToString(object? value)

PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(value.GetType());

switch (typeCode)
return typeCode switch
{
case PrimitiveTypeCode.String:
return ToString((string)value);
case PrimitiveTypeCode.Char:
return ToString((char)value);
case PrimitiveTypeCode.Boolean:
return ToString((bool)value);
case PrimitiveTypeCode.SByte:
return ToString((sbyte)value);
case PrimitiveTypeCode.Int16:
return ToString((short)value);
case PrimitiveTypeCode.UInt16:
return ToString((ushort)value);
case PrimitiveTypeCode.Int32:
return ToString((int)value);
case PrimitiveTypeCode.Byte:
return ToString((byte)value);
case PrimitiveTypeCode.UInt32:
return ToString((uint)value);
case PrimitiveTypeCode.Int64:
return ToString((long)value);
case PrimitiveTypeCode.UInt64:
return ToString((ulong)value);
case PrimitiveTypeCode.Single:
return ToString((float)value);
case PrimitiveTypeCode.Double:
return ToString((double)value);
case PrimitiveTypeCode.DateTime:
return ToString((DateTime)value);
case PrimitiveTypeCode.Decimal:
return ToString((decimal)value);
PrimitiveTypeCode.String => ToString((string)value),
PrimitiveTypeCode.Char => ToString((char)value),
PrimitiveTypeCode.Boolean => ToString((bool)value),
PrimitiveTypeCode.SByte => ToString((sbyte)value),
PrimitiveTypeCode.Int16 => ToString((short)value),
PrimitiveTypeCode.UInt16 => ToString((ushort)value),
PrimitiveTypeCode.Int32 => ToString((int)value),
PrimitiveTypeCode.Byte => ToString((byte)value),
PrimitiveTypeCode.UInt32 => ToString((uint)value),
PrimitiveTypeCode.Int64 => ToString((long)value),
PrimitiveTypeCode.UInt64 => ToString((ulong)value),
PrimitiveTypeCode.Single => ToString((float)value),
PrimitiveTypeCode.Double => ToString((double)value),
PrimitiveTypeCode.DateTime => ToString((DateTime)value),
PrimitiveTypeCode.Decimal => ToString((decimal)value),
#if HAVE_DB_NULL_TYPE_CODE
case PrimitiveTypeCode.DBNull:
return Null;
PrimitiveTypeCode.DBNull => Null,
#endif
#if HAVE_DATE_TIME_OFFSET
case PrimitiveTypeCode.DateTimeOffset:
return ToString((DateTimeOffset)value);
PrimitiveTypeCode.DateTimeOffset => ToString((DateTimeOffset)value),
#endif
case PrimitiveTypeCode.Guid:
return ToString((Guid)value);
case PrimitiveTypeCode.Uri:
return ToString((Uri)value);
case PrimitiveTypeCode.TimeSpan:
return ToString((TimeSpan)value);
PrimitiveTypeCode.Guid => ToString((Guid)value),
PrimitiveTypeCode.Uri => ToString((Uri)value),
PrimitiveTypeCode.TimeSpan => ToString((TimeSpan)value),
#if HAVE_BIG_INTEGER
case PrimitiveTypeCode.BigInteger:
return ToStringInternal((BigInteger)value);
PrimitiveTypeCode.BigInteger => ToString((BigInteger)value),
#endif
}
};

throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
}
Expand Down Expand Up @@ -553,7 +532,7 @@ public static string SerializeObject(object? value, Formatting formatting)
[DebuggerStepThrough]
public static string SerializeObject(object? value, params JsonConverter[] converters)
{
JsonSerializerSettings? settings = (converters != null && converters.Length > 0)
JsonSerializerSettings? settings = (converters?.Length > 0)
? new JsonSerializerSettings { Converters = converters }
: null;

Expand All @@ -570,7 +549,7 @@ public static string SerializeObject(object? value, params JsonConverter[] conve
[DebuggerStepThrough]
public static string SerializeObject(object? value, Formatting formatting, params JsonConverter[] converters)
{
JsonSerializerSettings? settings = (converters != null && converters.Length > 0)
JsonSerializerSettings? settings = (converters?.Length > 0)
? new JsonSerializerSettings { Converters = converters }
: null;

Expand Down Expand Up @@ -797,7 +776,7 @@ private static string SerializeObjectInternal(object? value, Type? type, JsonSer
[DebuggerStepThrough]
public static object? DeserializeObject(string value, Type type, params JsonConverter[] converters)
{
JsonSerializerSettings? settings = (converters != null && converters.Length > 0)
JsonSerializerSettings? settings = (converters?.Length > 0)
? new JsonSerializerSettings { Converters = converters }
: null;

Expand Down Expand Up @@ -862,7 +841,7 @@ public static void PopulateObject(string value, object target, JsonSerializerSet
{
jsonSerializer.Populate(jsonReader, target);

if (settings != null && settings.CheckAdditionalContent)
if (settings?.CheckAdditionalContent)
{
while (jsonReader.Read())
{
Expand Down Expand Up @@ -971,10 +950,12 @@ public static string SerializeXmlNode(XmlNode? node, Formatting formatting, bool
/// <returns>The deserialized <see cref="XmlNode"/>.</returns>
public static XmlDocument? DeserializeXmlNode(string value, string? deserializeRootElementName, bool writeArrayAttribute, bool encodeSpecialCharacters)
{
XmlNodeConverter converter = new XmlNodeConverter();
converter.DeserializeRootElementName = deserializeRootElementName;
converter.WriteArrayAttribute = writeArrayAttribute;
converter.EncodeSpecialCharacters = encodeSpecialCharacters;
XmlNodeConverter converter = new XmlNodeConverter()
{
DeserializeRootElementName = deserializeRootElementName,
WriteArrayAttribute = writeArrayAttribute,
EncodeSpecialCharacters = encodeSpecialCharacters
};

return (XmlDocument?)DeserializeObject(value, typeof(XmlDocument), converter);
}
Expand Down
17 changes: 7 additions & 10 deletions Src/Newtonsoft.Json/JsonPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,13 @@ public JsonPosition(JsonContainerType type)

internal int CalculateLength()
{
switch (Type)
return Type switch
{
case JsonContainerType.Object:
return PropertyName!.Length + 5;
case JsonContainerType.Array:
case JsonContainerType.Constructor:
return MathUtils.IntLength((ulong)Position) + 2;
default:
throw new ArgumentOutOfRangeException(nameof(Type));
}
JsonContainerType.Object => PropertyName!.Length + 5,
JsonContainerType.Array or JsonContainerType.Constructor
=> MathUtils.IntLength((ulong)Position) + 2,
_ => throw new ArgumentOutOfRangeException(nameof(Type))
};
}

internal void WriteTo(StringBuilder sb, ref StringWriter? writer, ref char[]? buffer)
Expand Down Expand Up @@ -164,7 +161,7 @@ internal static string FormatMessage(IJsonLineInfo? lineInfo, string path, strin

message += "Path '{0}'".FormatWith(CultureInfo.InvariantCulture, path);

if (lineInfo != null && lineInfo.HasLineInfo())
if (lineInfo?.HasLineInfo())
{
message += ", line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);
}
Expand Down
4 changes: 2 additions & 2 deletions Src/Newtonsoft.Json/JsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public CultureInfo Culture

internal JsonPosition GetPosition(int depth)
{
if (_stack != null && depth < _stack.Count)
if (depth < _stack?.Count)
{
return _stack[depth];
}
Expand Down Expand Up @@ -364,7 +364,7 @@ private void Push(JsonContainerType value)
private JsonContainerType Pop()
{
JsonPosition oldPosition;
if (_stack != null && _stack.Count > 0)
if (_stack?.Count > 0)
{
oldPosition = _currentPosition;
_currentPosition = _stack[_stack.Count - 1];
Expand Down
34 changes: 13 additions & 21 deletions Src/Newtonsoft.Json/JsonValidatingReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ private IList<JsonSchemaModel> CurrentMemberSchemas
{
if (!schema.PositionalItemsValidation)
{
if (schema.Items != null && schema.Items.Count > 0)
if (schema.Items?.Count > 0)
{
schemas.Add(schema.Items[0]);
}
}
else
{
if (schema.Items != null && schema.Items.Count > 0)
if (schema.Items?.Count > 0)
{
if (schema.Items.Count > (_currentScope.ArrayItemCount - 1))
{
Expand Down Expand Up @@ -354,25 +354,17 @@ private void ValidateNotDisallowed(JsonSchemaModel schema)

private JsonSchemaType? GetCurrentNodeSchemaType()
{
switch (_reader.TokenType)
{
case JsonToken.StartObject:
return JsonSchemaType.Object;
case JsonToken.StartArray:
return JsonSchemaType.Array;
case JsonToken.Integer:
return JsonSchemaType.Integer;
case JsonToken.Float:
return JsonSchemaType.Float;
case JsonToken.String:
return JsonSchemaType.String;
case JsonToken.Boolean:
return JsonSchemaType.Boolean;
case JsonToken.Null:
return JsonSchemaType.Null;
default:
return null;
}
return _reader.TokenType switch
{
JsonToken.StartObject => JsonSchemaType.Object,
JsonToken.StartArray => JsonSchemaType.Array,
JsonToken.Integer => JsonSchemaType.Integer,
JsonToken.Float => JsonSchemaType.Float,
JsonToken.String => JsonSchemaType.String,
JsonToken.Boolean => JsonSchemaType.Boolean,
JsonToken.Null => JsonSchemaType.Null,
_ => null
};
}

/// <summary>
Expand Down
74 changes: 23 additions & 51 deletions Src/Newtonsoft.Json/JsonWriter.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,14 @@ public virtual Task WriteEndAsync(CancellationToken cancellationToken = default)
internal Task WriteEndInternalAsync(CancellationToken cancellationToken)
{
JsonContainerType type = Peek();
switch (type)
return type switch
{
case JsonContainerType.Object:
return WriteEndObjectAsync(cancellationToken);
case JsonContainerType.Array:
return WriteEndArrayAsync(cancellationToken);
case JsonContainerType.Constructor:
return WriteEndConstructorAsync(cancellationToken);
default:
if (cancellationToken.IsCancellationRequested)
{
return cancellationToken.FromCanceled();
}

throw JsonWriterException.Create(this, "Unexpected type when writing end: " + type, null);
}
JsonContainerType.Object => WriteEndObjectAsync(cancellationToken),
JsonContainerType.Array => WriteEndArrayAsync(cancellationToken),
JsonContainerType.Constructor => WriteEndConstructorAsync(cancellationToken),
_ => cancellationToken.IsCancellationRequested ? cancellationToken.FromCanceled()
: throw JsonWriterException.Create(this, "Unexpected type when writing end: " + type, null)
};
}

internal Task InternalWriteEndAsync(JsonContainerType type, CancellationToken cancellationToken)
Expand Down Expand Up @@ -1652,43 +1644,23 @@ protected Task SetWriteStateAsync(JsonToken token, object value, CancellationTok
return cancellationToken.FromCanceled();
}

switch (token)
return token switch
{
case JsonToken.StartObject:
return InternalWriteStartAsync(token, JsonContainerType.Object, cancellationToken);
case JsonToken.StartArray:
return InternalWriteStartAsync(token, JsonContainerType.Array, cancellationToken);
case JsonToken.StartConstructor:
return InternalWriteStartAsync(token, JsonContainerType.Constructor, cancellationToken);
case JsonToken.PropertyName:
if (!(value is string s))
{
throw new ArgumentException("A name is required when setting property name state.", nameof(value));
}

return InternalWritePropertyNameAsync(s, cancellationToken);
case JsonToken.Comment:
return InternalWriteCommentAsync(cancellationToken);
case JsonToken.Raw:
return AsyncUtils.CompletedTask;
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.String:
case JsonToken.Boolean:
case JsonToken.Date:
case JsonToken.Bytes:
case JsonToken.Null:
case JsonToken.Undefined:
return InternalWriteValueAsync(token, cancellationToken);
case JsonToken.EndObject:
return InternalWriteEndAsync(JsonContainerType.Object, cancellationToken);
case JsonToken.EndArray:
return InternalWriteEndAsync(JsonContainerType.Array, cancellationToken);
case JsonToken.EndConstructor:
return InternalWriteEndAsync(JsonContainerType.Constructor, cancellationToken);
default:
throw new ArgumentOutOfRangeException(nameof(token));
}
JsonToken.StartObject => InternalWriteStartAsync(token, JsonContainerType.Object, cancellationToken),
JsonToken.StartArray => InternalWriteStartAsync(token, JsonContainerType.Array, cancellationToken),
JsonToken.StartConstructor => InternalWriteStartAsync(token, JsonContainerType.Constructor, cancellationToken),
JsonToken.PropertyName => value is string s ? InternalWritePropertyNameAsync(s, cancellationToken)
: throw new ArgumentException("A name is required when setting property name state.", nameof(value)),
JsonToken.Comment => InternalWriteCommentAsync(cancellationToken),
JsonToken.Raw => AsyncUtils.CompletedTask,
JsonToken.Integer or JsonToken.Float or JsonToken.String
or JsonToken.Boolean or JsonToken.Date or JsonToken.Bytes
or JsonToken.Null or JsonToken.Undefined => InternalWriteValueAsync(token, cancellationToken),
JsonToken.EndObject => InternalWriteEndAsync(JsonContainerType.Object, cancellationToken),
JsonToken.EndArray => InternalWriteEndAsync(JsonContainerType.Array, cancellationToken),
JsonToken.EndConstructor => InternalWriteEndAsync(JsonContainerType.Constructor, cancellationToken),
_ => throw new ArgumentOutOfRangeException(nameof(token))
};
}

internal static Task WriteValueAsync(JsonWriter writer, PrimitiveTypeCode typeCode, object value, CancellationToken cancellationToken)
Expand Down