Skip to content
Closed
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
16 changes: 16 additions & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public readonly partial struct JsonElement
public sbyte GetSByte() { throw null; }
public float GetSingle() { throw null; }
public string? GetString() { throw null; }
public System.TimeSpan GetTimeSpan() { throw null; }
[System.CLSCompliantAttribute(false)]
public ushort GetUInt16() { throw null; }
[System.CLSCompliantAttribute(false)]
Expand All @@ -87,6 +88,7 @@ public readonly partial struct JsonElement
[System.CLSCompliantAttribute(false)]
public bool TryGetSByte(out sbyte value) { throw null; }
public bool TryGetSingle(out float value) { throw null; }
public bool TryGetTimeSpan(out System.TimeSpan value) { throw null; }
[System.CLSCompliantAttribute(false)]
public bool TryGetUInt16(out ushort value) { throw null; }
[System.CLSCompliantAttribute(false)]
Expand Down Expand Up @@ -345,6 +347,7 @@ public ref partial struct Utf8JsonReader
public sbyte GetSByte() { throw null; }
public float GetSingle() { throw null; }
public string? GetString() { throw null; }
public System.TimeSpan GetTimeSpan() { throw null; }
[System.CLSCompliantAttribute(false)]
public ushort GetUInt16() { throw null; }
[System.CLSCompliantAttribute(false)]
Expand All @@ -366,6 +369,7 @@ public void Skip() { }
[System.CLSCompliantAttribute(false)]
public bool TryGetSByte(out sbyte value) { throw null; }
public bool TryGetSingle(out float value) { throw null; }
public bool TryGetTimeSpan(out System.TimeSpan value) { throw null; }
[System.CLSCompliantAttribute(false)]
public bool TryGetUInt16(out ushort value) { throw null; }
[System.CLSCompliantAttribute(false)]
Expand Down Expand Up @@ -477,34 +481,39 @@ public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.Guid
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.ReadOnlySpan<byte> utf8Value) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.ReadOnlySpan<char> value) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, string? value) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.TimeSpan value) { }
public void WriteString(System.ReadOnlySpan<byte> utf8PropertyName, System.Text.Json.JsonEncodedText value) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.DateTime value) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.DateTimeOffset value) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.Guid value) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.ReadOnlySpan<byte> utf8Value) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.ReadOnlySpan<char> value) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, string? value) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.TimeSpan value) { }
public void WriteString(System.ReadOnlySpan<char> propertyName, System.Text.Json.JsonEncodedText value) { }
public void WriteString(string propertyName, System.DateTime value) { }
public void WriteString(string propertyName, System.DateTimeOffset value) { }
public void WriteString(string propertyName, System.Guid value) { }
public void WriteString(string propertyName, System.ReadOnlySpan<byte> utf8Value) { }
public void WriteString(string propertyName, System.ReadOnlySpan<char> value) { }
public void WriteString(string propertyName, string? value) { }
public void WriteString(string propertyName, System.TimeSpan value) { }
public void WriteString(string propertyName, System.Text.Json.JsonEncodedText value) { }
public void WriteString(System.Text.Json.JsonEncodedText propertyName, System.DateTime value) { }
public void WriteString(System.Text.Json.JsonEncodedText propertyName, System.DateTimeOffset value) { }
public void WriteString(System.Text.Json.JsonEncodedText propertyName, System.Guid value) { }
public void WriteString(System.Text.Json.JsonEncodedText propertyName, System.ReadOnlySpan<byte> utf8Value) { }
public void WriteString(System.Text.Json.JsonEncodedText propertyName, System.ReadOnlySpan<char> value) { }
public void WriteString(System.Text.Json.JsonEncodedText propertyName, string? value) { }
public void WriteString(System.Text.Json.JsonEncodedText propertyName, System.TimeSpan value) { }
public void WriteString(System.Text.Json.JsonEncodedText propertyName, System.Text.Json.JsonEncodedText value) { }
public void WriteStringValue(System.DateTime value) { }
public void WriteStringValue(System.DateTimeOffset value) { }
public void WriteStringValue(System.Guid value) { }
public void WriteStringValue(System.ReadOnlySpan<byte> utf8Value) { }
public void WriteStringValue(System.ReadOnlySpan<char> value) { }
public void WriteStringValue(string? value) { }
public void WriteStringValue(System.TimeSpan value) { }
public void WriteStringValue(System.Text.Json.JsonEncodedText value) { }
}
}
Expand Down Expand Up @@ -550,6 +559,7 @@ internal JsonNode() { }
public static explicit operator char (System.Text.Json.Nodes.JsonNode value) { throw null; }
public static explicit operator System.DateTime (System.Text.Json.Nodes.JsonNode value) { throw null; }
public static explicit operator System.DateTimeOffset (System.Text.Json.Nodes.JsonNode value) { throw null; }
public static explicit operator System.TimeSpan (System.Text.Json.Nodes.JsonNode value) { throw null; }
public static explicit operator decimal (System.Text.Json.Nodes.JsonNode value) { throw null; }
public static explicit operator double (System.Text.Json.Nodes.JsonNode value) { throw null; }
public static explicit operator System.Guid (System.Text.Json.Nodes.JsonNode value) { throw null; }
Expand All @@ -561,6 +571,7 @@ internal JsonNode() { }
public static explicit operator char? (System.Text.Json.Nodes.JsonNode? value) { throw null; }
public static explicit operator System.DateTimeOffset? (System.Text.Json.Nodes.JsonNode? value) { throw null; }
public static explicit operator System.DateTime? (System.Text.Json.Nodes.JsonNode? value) { throw null; }
public static explicit operator System.TimeSpan? (System.Text.Json.Nodes.JsonNode? value) { throw null; }
public static explicit operator decimal? (System.Text.Json.Nodes.JsonNode? value) { throw null; }
public static explicit operator double? (System.Text.Json.Nodes.JsonNode? value) { throw null; }
public static explicit operator System.Guid? (System.Text.Json.Nodes.JsonNode? value) { throw null; }
Expand Down Expand Up @@ -591,6 +602,7 @@ internal JsonNode() { }
public static implicit operator System.Text.Json.Nodes.JsonNode (char value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode (System.DateTime value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode (System.DateTimeOffset value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode (System.TimeSpan value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode (decimal value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode (double value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode (System.Guid value) { throw null; }
Expand All @@ -602,6 +614,7 @@ internal JsonNode() { }
public static implicit operator System.Text.Json.Nodes.JsonNode? (char? value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode? (System.DateTimeOffset? value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode? (System.DateTime? value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode? (System.TimeSpan? value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode? (decimal? value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode? (double? value) { throw null; }
public static implicit operator System.Text.Json.Nodes.JsonNode? (System.Guid? value) { throw null; }
Expand Down Expand Up @@ -672,6 +685,7 @@ public abstract partial class JsonValue : System.Text.Json.Nodes.JsonNode
public static System.Text.Json.Nodes.JsonValue Create(char value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue Create(System.DateTime value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue Create(System.DateTimeOffset value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue Create(System.TimeSpan value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue Create(decimal value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue Create(double value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue Create(System.Guid value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
Expand All @@ -683,6 +697,7 @@ public abstract partial class JsonValue : System.Text.Json.Nodes.JsonNode
public static System.Text.Json.Nodes.JsonValue? Create(char? value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue? Create(System.DateTimeOffset? value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue? Create(System.DateTime? value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue? Create(System.TimeSpan? value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue? Create(decimal? value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue? Create(double? value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public static System.Text.Json.Nodes.JsonValue? Create(System.Guid? value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
Expand Down Expand Up @@ -887,6 +902,7 @@ public static partial class JsonMetadataServices
public static System.Text.Json.Serialization.JsonConverter<sbyte> SByteConverter { get { throw null; } }
public static System.Text.Json.Serialization.JsonConverter<float> SingleConverter { get { throw null; } }
public static System.Text.Json.Serialization.JsonConverter<string> StringConverter { get { throw null; } }
public static System.Text.Json.Serialization.JsonConverter<System.TimeSpan> TimeSpanConverter { get { throw null; } }
[System.CLSCompliantAttribute(false)]
public static System.Text.Json.Serialization.JsonConverter<ushort> UInt16Converter { get { throw null; } }
[System.CLSCompliantAttribute(false)]
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@
<data name="FormatDateTimeOffset" xml:space="preserve">
<value>The JSON value is not in a supported DateTimeOffset format.</value>
</data>
<data name="FormatTimeSpan" xml:space="preserve">
<value>The JSON value is not in a supported TimeSpan format.</value>
</data>
<data name="FormatGuid" xml:space="preserve">
<value>The JSON value is not in a supported Guid format.</value>
</data>
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="System\Text\Json\JsonHelpers.cs" />
<Compile Include="System\Text\Json\JsonHelpers.Date.cs" />
<Compile Include="System\Text\Json\JsonHelpers.Escaping.cs" />
<Compile Include="System\Text\Json\JsonHelpers.TimeSpan.cs" />
<Compile Include="System\Text\Json\JsonTokenType.cs" />
<Compile Include="System\Text\Json\Nodes\JsonArray.cs" />
<Compile Include="System\Text\Json\Nodes\JsonArray.IList.cs" />
Expand Down Expand Up @@ -165,6 +166,7 @@
<Compile Include="System\Text\Json\Serialization\Converters\Value\SByteConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\SingleConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\StringConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\TimeSpanConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\UInt16Converter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\UInt32Converter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\UInt64Converter.cs" />
Expand Down Expand Up @@ -245,6 +247,7 @@
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Literal.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.SignedNumber.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.String.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.TimeSpan.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.UnsignedNumber.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Bytes.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Comment.cs" />
Expand All @@ -259,6 +262,7 @@
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Literal.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.SignedNumber.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.String.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.TimeSpan.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.UnsignedNumber.cs" />
<Compile Include="System\TypeExtensions.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs" Link="Common\System\Obsoletions.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,41 @@ internal bool TryGetValue(int index, out DateTimeOffset value)
return false;
}

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

DbRow row = _parsedData.Get(index);

CheckExpectedType(JsonTokenType.String, row.TokenType);

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

if (!JsonHelpers.IsValidTimeSpanParseLength(segment.Length))
{
value = default;
return false;
}

// Segment needs to be unescaped
if (row.HasComplexChildren)
{
return JsonReaderHelper.TryGetEscapedTimeSpan(segment, out value);
}

Debug.Assert(segment.IndexOf(JsonConstants.BackSlash) == -1);

if (JsonHelpers.TryParseAsConstantFormat(segment, out TimeSpan tmp))
{
value = tmp;
return true;
}

value = default;
return false;
}

internal bool TryGetValue(int index, out Guid value)
{
CheckNotDisposed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,57 @@ public DateTimeOffset GetDateTimeOffset()
throw ThrowHelper.GetFormatException();
}

/// <summary>
/// Attempts to represent the current JSON string as a <see cref="TimeSpan"/>.
/// </summary>
/// <param name="value">Receives the value.</param>
/// <remarks>
/// This method does not create a TimeSpan representation of values other than JSON strings.
/// </remarks>
/// <returns>
/// <see langword="true"/> if the string can be represented as a <see cref="TimeSpan"/>,
/// <see langword="false"/> otherwise.
/// </returns>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
public bool TryGetTimeSpan(out TimeSpan value)
{
CheckValidInstance();

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

/// <summary>
/// Gets the value of the element as a <see cref="TimeSpan"/>.
/// </summary>
/// <remarks>
/// This method does not create a TimeSpan representation of values other than JSON strings.
/// </remarks>
/// <returns>The value of the element as a <see cref="TimeSpan"/>.</returns>
/// <exception cref="InvalidOperationException">
/// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>.
/// </exception>
/// <exception cref="FormatException">
/// The value cannot be represented as a <see cref="TimeSpan"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The parent <see cref="JsonDocument"/> has been disposed.
/// </exception>
/// <seealso cref="ToString"/>
public TimeSpan GetTimeSpan()
{
if (TryGetTimeSpan(out TimeSpan value))
{
return value;
}

throw ThrowHelper.GetFormatException();
}

/// <summary>
/// Attempts to represent the current JSON string as a <see cref="Guid"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ internal static class JsonConstants
(DateTimeParseNumFractionDigits - DateTimeNumFractionDigits)); // Like StandardFormat 'O' for DateTimeOffset, but allowing 9 additional (up to 16) fraction digits.
public const int MinimumDateTimeParseLength = 10; // YYYY-MM-DD
public const int MaximumEscapedDateTimeOffsetParseLength = MaxExpansionFactorWhileEscaping * MaximumDateTimeOffsetParseLength;
public const int MinimumTimeSpanParseLength = 8; // hh:mm:ss
public const int MaximumTimeSpanParseLength = 28; // -dddddddddd.hh:mm:ss.fffffff
public const int MaximumEscapedTimeSpanParseLength = MaxExpansionFactorWhileEscaping * MaximumTimeSpanParseLength;

// Encoding Helpers
public const char HighSurrogateStart = '\ud800';
Expand Down
Loading