Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add TimeSpan support for reads
  • Loading branch information
Turnerj committed Dec 17, 2019
commit 2197c963789f666d6aae4582c4deb5ac264984ff
5 changes: 5 additions & 0 deletions Source/Schema.NET/ValuesJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ private static bool TryProcessTokenAsType(JsonReader reader, Type targetType, ou
success = DateTimeOffset.TryParse(valueString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var localResult);
result = localResult;
}
else if (targetType == typeof(TimeSpan))
{
success = TimeSpan.TryParse(valueString, CultureInfo.InvariantCulture, out var localResult);
result = localResult;
}
else if (targetType == typeof(Uri))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if Guid should also be here, if we are checking a bunch of types? Not that schema.org has any Guid types (that I know of) but people might want to create custom types with Guids.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, GUID might be a good one to add and wouldn't be hard to do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in d4f08f5 including test.

{
success = Uri.TryCreate(valueString, UriKind.Absolute, out var localResult);
Expand Down
8 changes: 8 additions & 0 deletions Tests/Schema.NET.Test/ValuesJsonConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ public void ReadJson_Values_SingleValue_DateTimeOffsetAsISO8601String()
Assert.Equal(new DateTimeOffset(2000, 1, 1, 12, 34, 0, TimeSpan.FromHours(1)), result.Value2.First());
}

[Fact]
public void ReadJson_Values_SingleValue_TimeSpanAsISO8601String()
{
var json = "{\"Property\":\"12:34\"}";
var result = this.DeserializeObject<Values<string, TimeSpan>>(json);
Assert.Equal(new TimeSpan(12, 34, 0), result.Value2.First());
}

[Fact]
public void ReadJson_ParseValueToken_UriAsString()
{
Expand Down