-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from jan-johansson-mr/add-config
Added the same configuration option as is in LocalStorage
- Loading branch information
Showing
8 changed files
with
169 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Blazored.SessionStorage/JsonConverters/TimespanJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Blazored.SessionStorage.JsonConverters | ||
{ | ||
/// <summary> | ||
/// The new Json.NET doesn't support Timespan at this time | ||
/// https://github.com/dotnet/corefx/issues/38641 | ||
/// </summary> | ||
public class TimespanJsonConverter : JsonConverter<TimeSpan> | ||
{ | ||
/// <summary> | ||
/// Format: Days.Hours:Minutes:Seconds:Milliseconds | ||
/// </summary> | ||
public const string TimeSpanFormatString = @"d\.hh\:mm\:ss\:FFF"; | ||
|
||
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var s = reader.GetString(); | ||
if (string.IsNullOrWhiteSpace(s)) | ||
{ | ||
return TimeSpan.Zero; | ||
} | ||
else | ||
{ | ||
TimeSpan parsedTimeSpan; | ||
if (!TimeSpan.TryParseExact(s, TimeSpanFormatString, null, out parsedTimeSpan)) | ||
{ | ||
throw new FormatException($"Input timespan is not in an expected format : expected {Regex.Unescape(TimeSpanFormatString)}. Please retrieve this key as a string and parse manually."); | ||
} | ||
else | ||
{ | ||
return parsedTimeSpan; | ||
} | ||
} | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options) | ||
{ | ||
var timespanFormatted = $"{value.ToString(TimeSpanFormatString)}"; | ||
writer.WriteStringValue(timespanFormatted); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Blazored.SessionStorage/StorageOptions/SessionStorageOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json; | ||
|
||
namespace Blazored.SessionStorage.StorageOptions | ||
{ | ||
public class SessionStorageOptions | ||
{ | ||
public JsonSerializerOptions JsonSerializerOptions { get; } = new JsonSerializerOptions | ||
{ | ||
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, | ||
IgnoreNullValues = true, | ||
IgnoreReadOnlyProperties = true, | ||
PropertyNameCaseInsensitive = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
ReadCommentHandling = JsonCommentHandling.Skip, | ||
WriteIndented = false | ||
}; | ||
} | ||
} |