Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
APIs in JsonElement and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WinCPP committed Jun 15, 2019
1 parent 09d619e commit 55fdc18
Show file tree
Hide file tree
Showing 4 changed files with 543 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,50 @@ public readonly partial struct JsonElement
public System.Text.Json.JsonElement.ObjectEnumerator EnumerateObject() { throw null; }
public int GetArrayLength() { throw null; }
public bool GetBoolean() { throw null; }
public byte GetByte() { throw null; }
public byte[] GetBytesFromBase64() { throw null; }
public System.DateTime GetDateTime() { throw null; }
public System.DateTimeOffset GetDateTimeOffset() { throw null; }
public decimal GetDecimal() { throw null; }
public double GetDouble() { throw null; }
public System.Guid GetGuid() { throw null; }
public int GetInt16() { throw null; }
public int GetInt32() { throw null; }
public long GetInt64() { throw null; }
public System.Text.Json.JsonElement GetProperty(System.ReadOnlySpan<byte> utf8PropertyName) { throw null; }
public System.Text.Json.JsonElement GetProperty(System.ReadOnlySpan<char> propertyName) { throw null; }
public System.Text.Json.JsonElement GetProperty(string propertyName) { throw null; }
public string GetRawText() { throw null; }
[System.CLSCompliantAttribute(false)]
public sbyte GetSByte() { throw null; }
public float GetSingle() { throw null; }
public string GetString() { throw null; }
[System.CLSCompliantAttribute(false)]
public uint GetUInt16() { throw null; }
[System.CLSCompliantAttribute(false)]
public uint GetUInt32() { throw null; }
[System.CLSCompliantAttribute(false)]
public ulong GetUInt64() { throw null; }
public override string ToString() { throw null; }
public bool TryGetByte(out byte value) { throw null; }
public bool TryGetBytesFromBase64(out byte[] value) { throw null; }
public bool TryGetDateTime(out System.DateTime value) { throw null; }
public bool TryGetDateTimeOffset(out System.DateTimeOffset value) { throw null; }
public bool TryGetDecimal(out decimal value) { throw null; }
public bool TryGetDouble(out double value) { throw null; }
public bool TryGetGuid(out System.Guid value) { throw null; }
public bool TryGetInt16(out short value) { throw null; }
public bool TryGetInt32(out int value) { throw null; }
public bool TryGetInt64(out long value) { throw null; }
public bool TryGetProperty(System.ReadOnlySpan<byte> utf8PropertyName, out System.Text.Json.JsonElement value) { throw null; }
public bool TryGetProperty(System.ReadOnlySpan<char> propertyName, out System.Text.Json.JsonElement value) { throw null; }
public bool TryGetProperty(string propertyName, out System.Text.Json.JsonElement value) { throw null; }
[System.CLSCompliantAttribute(false)]
public bool TryGetSByte(out sbyte value) { throw null; }
public bool TryGetSingle(out float value) { throw null; }
[System.CLSCompliantAttribute(false)]
public bool TryGetUInt16(out ushort value) { throw null; }
[System.CLSCompliantAttribute(false)]
public bool TryGetUInt32(out uint value) { throw null; }
[System.CLSCompliantAttribute(false)]
public bool TryGetUInt64(out ulong value) { throw null; }
Expand Down
88 changes: 88 additions & 0 deletions src/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,94 @@ internal bool TryGetValue(int index, out byte[] value)
return JsonReaderHelper.TryDecodeBase64(segment, out value);
}

internal bool TryGetValue(int index, out sbyte value)
{
CheckNotDisposed();

DbRow row = _parsedData.Get(index);

CheckExpectedType(JsonTokenType.Number, row.TokenType);

ReadOnlySpan<byte> data = _utf8Json.Span;
ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength);

if (Utf8Parser.TryParse(segment, out sbyte tmp, out int consumed) &&
consumed == segment.Length)
{
value = tmp;
return true;
}

value = 0;
return false;
}

internal bool TryGetValue(int index, out byte value)
{
CheckNotDisposed();

DbRow row = _parsedData.Get(index);

CheckExpectedType(JsonTokenType.Number, row.TokenType);

ReadOnlySpan<byte> data = _utf8Json.Span;
ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength);

if (Utf8Parser.TryParse(segment, out byte tmp, out int consumed) &&
consumed == segment.Length)
{
value = tmp;
return true;
}

value = 0;
return false;
}

internal bool TryGetValue(int index, out short value)
{
CheckNotDisposed();

DbRow row = _parsedData.Get(index);

CheckExpectedType(JsonTokenType.Number, row.TokenType);

ReadOnlySpan<byte> data = _utf8Json.Span;
ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength);

if (Utf8Parser.TryParse(segment, out short tmp, out int consumed) &&
consumed == segment.Length)
{
value = tmp;
return true;
}

value = 0;
return false;
}

internal bool TryGetValue(int index, out ushort value)
{
CheckNotDisposed();

DbRow row = _parsedData.Get(index);

CheckExpectedType(JsonTokenType.Number, row.TokenType);

ReadOnlySpan<byte> data = _utf8Json.Span;
ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength);

if (Utf8Parser.TryParse(segment, out ushort tmp, out int consumed) &&
consumed == segment.Length)
{
value = tmp;
return true;
}

value = 0;
return false;
}

internal bool TryGetValue(int index, out int value)
{
CheckNotDisposed();
Expand Down
198 changes: 198 additions & 0 deletions src/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,204 @@ public byte[] GetBytesFromBase64()
throw new FormatException();
}

/// <summary>
/// Attempts to represent the current JSON number as an <see cref="sbyte"/>.
/// </summary>
/// <param name="value">Receives the value.</param>
/// <remarks>
/// This method does not parse the contents of a JSON string value.
/// </remarks>
/// <returns>
/// <see langword="true"/> if the number can be represented as an <see cref="sbyte"/>,
/// <see langword="false"/> otherwise.
/// </returns>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="Type"/> is not <see cref="JsonValueType.Number"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
[CLSCompliant(false)]
public bool TryGetSByte(out sbyte value)
{
CheckValidInstance();

return _parent.TryGetValue(_idx, out value);
}

/// <summary>
/// Gets the current JSON number as an <see cref="sbyte"/>.
/// </summary>
/// <returns>The current JSON number as an <see cref="sbyte"/>.</returns>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="Type"/> is not <see cref="JsonValueType.Number"/>.
/// </exception>
/// <exception cref="FormatException">
/// The value cannot be represented as an <see cref="sbyte"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
[CLSCompliant(false)]
public sbyte GetSByte()
{
if (TryGetSByte(out sbyte value))
{
return value;
}

throw new FormatException();
}

/// <summary>
/// Attempts to represent the current JSON number as a <see cref="byte"/>.
/// </summary>
/// <param name="value">Receives the value.</param>
/// <remarks>
/// This method does not parse the contents of a JSON string value.
/// </remarks>
/// <returns>
/// <see langword="true"/> if the number can be represented as a <see cref="byte"/>,
/// <see langword="false"/> otherwise.
/// </returns>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="Type"/> is not <see cref="JsonValueType.Number"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
public bool TryGetByte(out byte value)
{
CheckValidInstance();

return _parent.TryGetValue(_idx, out value);
}

/// <summary>
/// Gets the current JSON number as a <see cref="byte"/>.
/// </summary>
/// <returns>The current JSON number as a <see cref="byte"/>.</returns>
/// <remarks>
/// This method does not parse the contents of a JSON string value.
/// </remarks>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="Type"/> is not <see cref="JsonValueType.Number"/>.
/// </exception>
/// <exception cref="FormatException">
/// The value cannot be represented as a <see cref="byte"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
public byte GetByte()
{
if (TryGetByte(out byte value))
{
return value;
}

throw new FormatException();
}

/// <summary>
/// Attempts to represent the current JSON number as an <see cref="short"/>.
/// </summary>
/// <param name="value">Receives the value.</param>
/// <remarks>
/// This method does not parse the contents of a JSON string value.
/// </remarks>
/// <returns>
/// <see langword="true"/> if the number can be represented as an <see cref="short"/>,
/// <see langword="false"/> otherwise.
/// </returns>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="Type"/> is not <see cref="JsonValueType.Number"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
public bool TryGetInt16(out short value)
{
CheckValidInstance();

return _parent.TryGetValue(_idx, out value);
}

/// <summary>
/// Gets the current JSON number as an <see cref="short"/>.
/// </summary>
/// <returns>The current JSON number as an <see cref="short"/>.</returns>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="Type"/> is not <see cref="JsonValueType.Number"/>.
/// </exception>
/// <exception cref="FormatException">
/// The value cannot be represented as an <see cref="short"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
public int GetInt16()
{
if (TryGetInt16(out short value))
{
return value;
}

throw new FormatException();
}

/// <summary>
/// Attempts to represent the current JSON number as a <see cref="ushort"/>.
/// </summary>
/// <param name="value">Receives the value.</param>
/// <remarks>
/// This method does not parse the contents of a JSON string value.
/// </remarks>
/// <returns>
/// <see langword="true"/> if the number can be represented as a <see cref="ushort"/>,
/// <see langword="false"/> otherwise.
/// </returns>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="Type"/> is not <see cref="JsonValueType.Number"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
[CLSCompliant(false)]
public bool TryGetUInt16(out ushort value)
{
CheckValidInstance();

return _parent.TryGetValue(_idx, out value);
}

/// <summary>
/// Gets the current JSON number as a <see cref="ushort"/>.
/// </summary>
/// <returns>The current JSON number as a <see cref="ushort"/>.</returns>
/// <remarks>
/// This method does not parse the contents of a JSON string value.
/// </remarks>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="Type"/> is not <see cref="JsonValueType.Number"/>.
/// </exception>
/// <exception cref="FormatException">
/// The value cannot be represented as a <see cref="ushort"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
[CLSCompliant(false)]
public uint GetUInt16()
{
if (TryGetUInt16(out ushort value))
{
return value;
}

throw new FormatException();
}

/// <summary>
/// Attempts to represent the current JSON number as an <see cref="int"/>.
/// </summary>
Expand Down
Loading

0 comments on commit 55fdc18

Please sign in to comment.