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

[System.Text.Json] Move inline throw statements to ThrowHelper #61746

Merged
merged 5 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ private void CheckExpectedType(JsonTokenType expected, JsonTokenType actual)
{
if (expected != actual)
{
throw ThrowHelper.GetJsonElementWrongTypeException(expected, actual);
ThrowHelper.ThrowJsonElementWrongTypeException(expected, actual);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public int MaxDepth
set
{
if (value < 0)
throw ThrowHelper.GetArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
{
ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
}

_maxDepth = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System.Text.Json
{
Expand Down Expand Up @@ -333,7 +334,12 @@ public bool GetBoolean()
return
type == JsonTokenType.True ? true :
type == JsonTokenType.False ? false :
throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), type);
ThrowJsonElementWrongTypeException(type);

static bool ThrowJsonElementWrongTypeException(JsonTokenType actualType)
{
throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), actualType.ToValueKind());
}
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down Expand Up @@ -400,12 +406,12 @@ public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value)
/// <seealso cref="ToString"/>
public byte[] GetBytesFromBase64()
{
if (TryGetBytesFromBase64(out byte[]? value))
if (!TryGetBytesFromBase64(out byte[]? value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -645,12 +651,12 @@ public bool TryGetInt32(out int value)
/// </exception>
public int GetInt32()
{
if (TryGetInt32(out int value))
if (!TryGetInt32(out int value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -697,12 +703,12 @@ public bool TryGetUInt32(out uint value)
[CLSCompliant(false)]
public uint GetUInt32()
{
if (TryGetUInt32(out uint value))
if (!TryGetUInt32(out uint value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -747,12 +753,12 @@ public bool TryGetInt64(out long value)
/// </exception>
public long GetInt64()
{
if (TryGetInt64(out long value))
if (!TryGetInt64(out long value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -799,12 +805,12 @@ public bool TryGetUInt64(out ulong value)
[CLSCompliant(false)]
public ulong GetUInt64()
{
if (TryGetUInt64(out ulong value))
if (!TryGetUInt64(out ulong value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -866,12 +872,12 @@ public bool TryGetDouble(out double value)
/// </exception>
public double GetDouble()
{
if (TryGetDouble(out double value))
if (!TryGetDouble(out double value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -933,12 +939,12 @@ public bool TryGetSingle(out float value)
/// </exception>
public float GetSingle()
{
if (TryGetSingle(out float value))
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
if (!TryGetSingle(out float value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -985,12 +991,12 @@ public bool TryGetDecimal(out decimal value)
/// <seealso cref="GetRawText"/>
public decimal GetDecimal()
{
if (TryGetDecimal(out decimal value))
if (!TryGetDecimal(out decimal value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -1036,12 +1042,12 @@ public bool TryGetDateTime(out DateTime value)
/// <seealso cref="ToString"/>
public DateTime GetDateTime()
{
if (TryGetDateTime(out DateTime value))
if (!TryGetDateTime(out DateTime value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -1087,12 +1093,12 @@ public bool TryGetDateTimeOffset(out DateTimeOffset value)
/// <seealso cref="ToString"/>
public DateTimeOffset GetDateTimeOffset()
{
if (TryGetDateTimeOffset(out DateTimeOffset value))
if (!TryGetDateTimeOffset(out DateTimeOffset value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

/// <summary>
Expand Down Expand Up @@ -1138,12 +1144,12 @@ public bool TryGetGuid(out Guid value)
/// <seealso cref="ToString"/>
public Guid GetGuid()
{
if (TryGetGuid(out Guid value))
if (!TryGetGuid(out Guid value))
{
return value;
ThrowHelper.ThrowFormatException();
}

throw ThrowHelper.GetFormatException();
return value;
}

internal string GetPropertyName()
Expand Down Expand Up @@ -1326,7 +1332,7 @@ public ArrayEnumerator EnumerateArray()

if (tokenType != JsonTokenType.StartArray)
{
throw ThrowHelper.GetJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType);
ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType);
}

return new ArrayEnumerator(this);
Expand All @@ -1352,7 +1358,7 @@ public ObjectEnumerator EnumerateObject()

if (tokenType != JsonTokenType.StartObject)
{
throw ThrowHelper.GetJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType);
ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType);
}

return new ObjectEnumerator(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ IEnumerator IEnumerable.GetEnumerator()
}
}

public void Add(string propertyName) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
public void Add(string propertyName) => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly();

public void Clear() => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
public void Clear() => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly();

public bool Contains(string propertyName) => _parent.ContainsProperty(propertyName);

Expand Down Expand Up @@ -68,7 +68,7 @@ public IEnumerator<string> GetEnumerator()
}
}

bool ICollection<string>.Remove(string propertyName) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
bool ICollection<string>.Remove(string propertyName) => throw ThrowHelper.GetNotSupportedException_NodeCollectionIsReadOnly();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ IEnumerator IEnumerable.GetEnumerator()
}
}

public void Add(T? jsonNode) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
public void Add(T? jsonNode) => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly();

public void Clear() => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
public void Clear() => ThrowHelper.ThrowNotSupportedException_NodeCollectionIsReadOnly();

public bool Contains(T? jsonNode) => _parent.ContainsValue(jsonNode);

Expand Down Expand Up @@ -68,7 +68,7 @@ public void CopyTo(T?[] nodeArray, int index)
}
}

bool ICollection<T?>.Remove(T? node) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
bool ICollection<T?>.Remove(T? node) => throw ThrowHelper.GetNotSupportedException_NodeCollectionIsReadOnly();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public JsonCommentHandling CommentHandling
{
Debug.Assert(value >= 0);
if (value > JsonCommentHandling.Allow)
throw ThrowHelper.GetArgumentOutOfRangeException_CommentEnumMustBeInRange(nameof(value));
{
ThrowHelper.ThrowArgumentOutOfRangeException_CommentEnumMustBeInRange(nameof(value));
}

_commentHandling = value;
}
Expand All @@ -52,7 +54,9 @@ public int MaxDepth
set
{
if (value < 0)
throw ThrowHelper.GetArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
{
ThrowHelper.ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(nameof(value));
}

_maxDepth = value;
}
Expand Down
Loading