-
Notifications
You must be signed in to change notification settings - Fork 69
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 #919 from WildernessLabs/MicroJson
Add MicroJson library
- Loading branch information
Showing
22 changed files
with
1,755 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
....Foundation.Libraries_and_Frameworks/Serialization.MicroJson/Driver/DateTimeConverters.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,67 @@ | ||
using System; | ||
using System.Globalization; | ||
|
||
namespace Meadow.Foundation.Serialization; | ||
|
||
internal static class DateTimeConverters | ||
{ | ||
/// <summary> | ||
/// Converts an ISO 8601 time/input formatted string into a DateTime object. | ||
/// </summary> | ||
/// <param name="input">Date time string in ISO 8601 format.</param> | ||
/// <returns>A new DateTime object representing the input date and time.</returns> | ||
/// <exception cref="ArgumentException">Thrown when the input string is not in the expected ISO 8601 format.</exception> | ||
public static DateTime FromIso8601(string input) | ||
{ | ||
// Check if format contains the timezone ID, UTC reference | ||
bool isUtc = input.EndsWith("Z"); | ||
|
||
if (DateTime.TryParseExact(input, "yyyy-MM-ddTHH:mm:ss.FFFK", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var dateTime)) | ||
{ | ||
if (!isUtc && input.Length > 19) | ||
{ | ||
string offset = input[19..]; | ||
TimeSpan timeOffset = TimeSpan.Parse(offset); | ||
dateTime = dateTime.Add(timeOffset); | ||
} | ||
|
||
return dateTime; | ||
} | ||
|
||
throw new ArgumentException("Invalid ISO 8601 format", nameof(input)); | ||
} | ||
|
||
/// <summary> | ||
/// Converts a DateTime object into an ISO 8601 string in UTC format. | ||
/// </summary> | ||
/// <param name="dateTime">The DateTime to convert.</param> | ||
/// <returns>DateTime as an ISO 8601 string in UTC format.</returns> | ||
public static string ToIso8601(DateTime dateTime) | ||
{ | ||
return $"{dateTime:yyyy-MM-ddTHH:mm:ss.FFFZ}"; | ||
} | ||
|
||
/// <summary> | ||
/// Converts a DateTime to the ASP.NET Ajax JSON format. | ||
/// </summary> | ||
/// <param name="dateTime">The DateTime to convert.</param> | ||
/// <returns>A string representation of the DateTime in ASP.NET Ajax JSON format.</returns> | ||
public static string ToASPNetAjax(DateTime dateTime) | ||
{ | ||
return $@"\/Date({dateTime.Ticks})\/"; | ||
} | ||
|
||
/// <summary> | ||
/// Converts an ASP.NET Ajax JSON string to DateTime | ||
/// </summary> | ||
/// <param name="ajax">The ajax formatted date time string</param> | ||
/// <returns>A new DateTime object representing the input date and time.</returns> | ||
public static DateTime FromASPNetAjax(string ajax) | ||
{ | ||
string[] parts = ajax.Split(new char[] { '(', ')' }); | ||
|
||
long ticks = Convert.ToInt64(parts[1]); | ||
|
||
return new DateTime(ticks, DateTimeKind.Utc); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...dow.Foundation.Libraries_and_Frameworks/Serialization.MicroJson/Driver/MicroJson.Enums.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,26 @@ | ||
namespace Meadow.Foundation.Serialization; | ||
|
||
internal enum NumberStyle | ||
{ | ||
Decimal = 1, | ||
Hexadecimal | ||
} | ||
|
||
public static partial class MicroJson | ||
{ | ||
/// <summary> | ||
/// Enumeration of the popular formats of time and date with Json. | ||
/// </summary> | ||
public enum DateTimeFormat | ||
{ | ||
/// <summary> | ||
/// ISO 8601 format for time and date representation in JSON. | ||
/// </summary> | ||
ISO8601, | ||
|
||
/// <summary> | ||
/// ASP.NET Ajax JSON format for time and date representation. | ||
/// </summary> | ||
Ajax | ||
} | ||
} |
Oops, something went wrong.