Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
"yyyy'-'MM'-'dd",
"yyyyMMdd"
"yyyy'-'MM'-'dd"
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ namespace {{packageName}}.{{clientPackage}}
/// <param name="options"></param>
/// <returns></returns>
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.TokenType == JsonTokenType.Null)
throw new NotSupportedException();
if (reader.TokenType != JsonTokenType.String)
throw new JsonException($"Expected a JSON string for date, but found {reader.TokenType}.");

string value = reader.GetString(){{nrt!}};

foreach(string format in Formats)
if (DateOnly.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly result))
return result;

throw new NotSupportedException();
throw new JsonException("The JSON string is not a valid date.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{{>partial_header}}
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -12,14 +11,6 @@ namespace {{packageName}}.{{clientPackage}}
/// </summary>
{{>visibility}} class DateOnlyNullableJsonConverter : JsonConverter<DateOnly?>
{
/// <summary>
/// The formats used to deserialize the date
/// </summary>
public static string[] Formats { get; } = {
{{>DateFormats}}

};

/// <summary>
/// Returns a DateOnly from the Json object
/// </summary>
Expand All @@ -31,13 +22,7 @@ namespace {{packageName}}.{{clientPackage}}
if (reader.TokenType == JsonTokenType.Null)
return null;

string value = reader.GetString(){{nrt!}};

foreach(string format in Formats)
if (DateOnly.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly result))
return result;

throw new NotSupportedException();
return JsonSerializer.Deserialize<DateOnly>(ref reader, options);
}

/// <summary>
Expand All @@ -51,7 +36,7 @@ namespace {{packageName}}.{{clientPackage}}
if (dateOnlyValue == null)
writer.WriteNullValue();
else
writer.WriteStringValue(dateOnlyValue.Value.ToString("{{{dateFormat}}}", CultureInfo.InvariantCulture));
JsonSerializer.Serialize(writer, dateOnlyValue.Value, options);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ssK",
{{^supportsDateOnly}}
"yyyy'-'MM'-'dd",
{{/supportsDateOnly}}
"yyyyMMddTHHmmss.fffffffK",
"yyyyMMddTHHmmss.ffffffK",
"yyyyMMddTHHmmss.fffffK",
"yyyyMMddTHHmmss.ffffK",
"yyyyMMddTHHmmss.fffK",
"yyyyMMddTHHmmss.ffK",
"yyyyMMddTHHmmss.fK",
"yyyyMMddTHHmmssK",
{{^supportsDateOnly}}
"yyyyMMdd"
{{/supportsDateOnly}}
{{^supportsDateOnly}}
"yyyy'-'MM'-'dd"
{{/supportsDateOnly}}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@ namespace {{packageName}}.{{clientPackage}}
/// <param name="options"></param>
/// <returns></returns>
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.TokenType == JsonTokenType.Null)
throw new NotSupportedException();
if (reader.TokenType != JsonTokenType.String)
throw new JsonException($"Expected a JSON string for date-time, but found {reader.TokenType}.");

string value = reader.GetString(){{nrt!}};

// Use System.Text.Json for standard ISO 8601 values with timezone information.
// System.Text.Json also accepts offset-less ISO 8601 values, but OpenAPI
// date-time values follow RFC 3339, which requires an offset. Let those
// non-standard values fall through so custom formats can explicitly permit them.
if (reader.TryGetDateTime(out DateTime dateTime) && dateTime.Kind != DateTimeKind.Unspecified)
return dateTime.ToUniversalTime();

// Use the customizable formats as a compatibility fallback for basic,
// date-only, and explicitly permitted offset-less values.
foreach(string format in Formats)
if (DateTime.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out DateTime result))
return result;

throw new NotSupportedException();
throw new JsonException("The JSON string is not a valid date-time.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{{>partial_header}}
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -12,14 +11,6 @@ namespace {{packageName}}.{{clientPackage}}
/// </summary>
{{>visibility}} class DateTimeNullableJsonConverter : JsonConverter<DateTime?>
{
/// <summary>
/// The formats used to deserialize the date
/// </summary>
public static string[] Formats { get; } = {
{{>DateTimeFormats}}

};

/// <summary>
/// Returns a DateTime from the Json object
/// </summary>
Expand All @@ -31,13 +22,7 @@ namespace {{packageName}}.{{clientPackage}}
if (reader.TokenType == JsonTokenType.Null)
return null;

string value = reader.GetString(){{nrt!}};

foreach(string format in Formats)
if (DateTime.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out DateTime result))
return result;

return null;
return JsonSerializer.Deserialize<DateTime>(ref reader, options);
}

/// <summary>
Expand All @@ -51,7 +36,7 @@ namespace {{packageName}}.{{clientPackage}}
if (dateTimeValue == null)
writer.WriteNullValue();
else
writer.WriteStringValue(dateTimeValue.Value.ToString("{{{dateTimeFormat}}}", CultureInfo.InvariantCulture));
JsonSerializer.Serialize(writer, dateTimeValue.Value, options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public class DateOnlyJsonConverter : JsonConverter<DateOnly>
/// The formats used to deserialize the date
/// </summary>
public static string[] Formats { get; } = {
"yyyy'-'MM'-'dd",
"yyyyMMdd"
"yyyy'-'MM'-'dd"

};

Expand All @@ -37,16 +36,16 @@ public class DateOnlyJsonConverter : JsonConverter<DateOnly>
/// <param name="options"></param>
/// <returns></returns>
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.TokenType == JsonTokenType.Null)
throw new NotSupportedException();
if (reader.TokenType != JsonTokenType.String)
throw new JsonException($"Expected a JSON string for date, but found {reader.TokenType}.");

string value = reader.GetString()!;

foreach(string format in Formats)
if (DateOnly.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly result))
return result;

throw new NotSupportedException();
throw new JsonException("The JSON string is not a valid date.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -20,15 +19,6 @@ namespace Org.OpenAPITools.Client
/// </summary>
public class DateOnlyNullableJsonConverter : JsonConverter<DateOnly?>
{
/// <summary>
/// The formats used to deserialize the date
/// </summary>
public static string[] Formats { get; } = {
"yyyy'-'MM'-'dd",
"yyyyMMdd"

};

/// <summary>
/// Returns a DateOnly from the Json object
/// </summary>
Expand All @@ -40,13 +30,7 @@ public class DateOnlyNullableJsonConverter : JsonConverter<DateOnly?>
if (reader.TokenType == JsonTokenType.Null)
return null;

string value = reader.GetString()!;

foreach(string format in Formats)
if (DateOnly.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly result))
return result;

throw new NotSupportedException();
return JsonSerializer.Deserialize<DateOnly>(ref reader, options);
}

/// <summary>
Expand All @@ -60,7 +44,7 @@ public override void Write(Utf8JsonWriter writer, DateOnly? dateOnlyValue, JsonS
if (dateOnlyValue == null)
writer.WriteNullValue();
else
writer.WriteStringValue(dateOnlyValue.Value.ToString("yyyy'-'MM'-'dd", CultureInfo.InvariantCulture));
JsonSerializer.Serialize(writer, dateOnlyValue.Value, options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ public class DateTimeJsonConverter : JsonConverter<DateTime>
/// The formats used to deserialize the date
/// </summary>
public static string[] Formats { get; } = {
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ssK",
"yyyyMMddTHHmmss.fffffffK",
"yyyyMMddTHHmmss.ffffffK",
"yyyyMMddTHHmmss.fffffK",
"yyyyMMddTHHmmss.ffffK",
"yyyyMMddTHHmmss.fffK",
"yyyyMMddTHHmmss.ffK",
"yyyyMMddTHHmmss.fK",
"yyyyMMddTHHmmssK",

};

Expand All @@ -51,16 +35,25 @@ public class DateTimeJsonConverter : JsonConverter<DateTime>
/// <param name="options"></param>
/// <returns></returns>
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.TokenType == JsonTokenType.Null)
throw new NotSupportedException();
if (reader.TokenType != JsonTokenType.String)
throw new JsonException($"Expected a JSON string for date-time, but found {reader.TokenType}.");

string value = reader.GetString()!;

// Use System.Text.Json for standard ISO 8601 values with timezone information.
// System.Text.Json also accepts offset-less ISO 8601 values, but OpenAPI
// date-time values follow RFC 3339, which requires an offset. Let those
// non-standard values fall through so custom formats can explicitly permit them.
if (reader.TryGetDateTime(out DateTime dateTime) && dateTime.Kind != DateTimeKind.Unspecified)
return dateTime.ToUniversalTime();

// Use the customizable formats as a compatibility fallback for basic,
// date-only, and explicitly permitted offset-less values.
foreach(string format in Formats)
if (DateTime.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out DateTime result))
return result;

throw new NotSupportedException();
throw new JsonException("The JSON string is not a valid date-time.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -20,29 +19,6 @@ namespace Org.OpenAPITools.Client
/// </summary>
public class DateTimeNullableJsonConverter : JsonConverter<DateTime?>
{
/// <summary>
/// The formats used to deserialize the date
/// </summary>
public static string[] Formats { get; } = {
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ssK",
"yyyyMMddTHHmmss.fffffffK",
"yyyyMMddTHHmmss.ffffffK",
"yyyyMMddTHHmmss.fffffK",
"yyyyMMddTHHmmss.ffffK",
"yyyyMMddTHHmmss.fffK",
"yyyyMMddTHHmmss.ffK",
"yyyyMMddTHHmmss.fK",
"yyyyMMddTHHmmssK",

};

/// <summary>
/// Returns a DateTime from the Json object
/// </summary>
Expand All @@ -54,13 +30,7 @@ public class DateTimeNullableJsonConverter : JsonConverter<DateTime?>
if (reader.TokenType == JsonTokenType.Null)
return null;

string value = reader.GetString()!;

foreach(string format in Formats)
if (DateTime.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out DateTime result))
return result;

return null;
return JsonSerializer.Deserialize<DateTime>(ref reader, options);
}

/// <summary>
Expand All @@ -74,7 +44,7 @@ public override void Write(Utf8JsonWriter writer, DateTime? dateTimeValue, JsonS
if (dateTimeValue == null)
writer.WriteNullValue();
else
writer.WriteStringValue(dateTimeValue.Value.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK", CultureInfo.InvariantCulture));
JsonSerializer.Serialize(writer, dateTimeValue.Value, options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public class DateOnlyJsonConverter : JsonConverter<DateOnly>
/// The formats used to deserialize the date
/// </summary>
public static string[] Formats { get; } = {
"yyyy'-'MM'-'dd",
"yyyyMMdd"
"yyyy'-'MM'-'dd"

};

Expand All @@ -37,16 +36,16 @@ public class DateOnlyJsonConverter : JsonConverter<DateOnly>
/// <param name="options"></param>
/// <returns></returns>
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.TokenType == JsonTokenType.Null)
throw new NotSupportedException();
if (reader.TokenType != JsonTokenType.String)
throw new JsonException($"Expected a JSON string for date, but found {reader.TokenType}.");

string value = reader.GetString()!;

foreach(string format in Formats)
if (DateOnly.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly result))
return result;

throw new NotSupportedException();
throw new JsonException("The JSON string is not a valid date.");
}

/// <summary>
Expand Down
Loading
Loading