Skip to content

Commit

Permalink
Merge pull request #919 from WildernessLabs/MicroJson
Browse files Browse the repository at this point in the history
Add MicroJson library
  • Loading branch information
ctacke authored Mar 8, 2024
2 parents a8a4c45 + eac7702 commit c4ce95f
Show file tree
Hide file tree
Showing 22 changed files with 1,755 additions and 1 deletion.
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);
}
}
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
}
}
Loading

0 comments on commit c4ce95f

Please sign in to comment.