Skip to content

Commit

Permalink
Updated to JSON.NET 13.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed May 2, 2023
1 parent 6f33256 commit 01afbe7
Show file tree
Hide file tree
Showing 108 changed files with 1,528 additions and 717 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private static void EnsureReflectionObject(Type t)
{
// current token is already at base64 string
// unable to call ReadAsBytes so do it the old fashion way
string encodedData = reader.Value!.ToString();
string encodedData = reader.Value!.ToString()!;
data = Convert.FromBase64String(encodedData);
}
else
Expand All @@ -135,7 +135,7 @@ private static void EnsureReflectionObject(Type t)
}

Type t = (ReflectionUtils.IsNullableType(objectType))
? Nullable.GetUnderlyingType(objectType)
? Nullable.GetUnderlyingType(objectType)!
: objectType;

#if HAVE_LINQ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
// handle typed datasets
DataSet ds = (objectType == typeof(DataSet))
? new DataSet()
: (DataSet)Activator.CreateInstance(objectType);
: (DataSet)Activator.CreateInstance(objectType)!;

DataTableConverter converter = new DataTableConverter();

reader.ReadAndAssert();

while (reader.TokenType == JsonToken.PropertyName)
{
DataTable dt = ds.Tables[(string)reader.Value!];
DataTable? dt = ds.Tables[(string)reader.Value!];
bool exists = (dt != null);

dt = (DataTable)converter.ReadJson(reader, typeof(DataTable), dt, serializer)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
// handle typed datasets
dt = (objectType == typeof(DataTable))
? new DataTable()
: (DataTable)Activator.CreateInstance(objectType);
: (DataTable)Activator.CreateInstance(objectType)!;
}

// DataTable is inside a DataSet
Expand Down Expand Up @@ -144,7 +144,7 @@ private static void CreateRow(JsonReader reader, DataTable dt, JsonSerializer se

reader.ReadAndAssert();

DataColumn column = dt.Columns[columnName];
DataColumn? column = dt.Columns[columnName];
if (column == null)
{
Type columnType = GetColumnDataType(reader);
Expand Down Expand Up @@ -185,7 +185,7 @@ private static void CreateRow(JsonReader reader, DataTable dt, JsonSerializer se
reader.ReadAndAssert();
}

Array destinationArray = Array.CreateInstance(column.DataType.GetElementType(), o.Count);
Array destinationArray = Array.CreateInstance(column.DataType.GetElementType()!, o.Count);
((IList)o).CopyTo(destinationArray, 0);

dr[columnName] = destinationArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer

while (reader.TokenType == JsonToken.PropertyName)
{
string propertyName = reader.Value!.ToString();
string propertyName = reader.Value!.ToString()!;
if (string.Equals(propertyName, CasePropertyName, StringComparison.OrdinalIgnoreCase))
{
reader.ReadAndAssert();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static void ReadAndAssertProperty(JsonReader reader, string propertyName
reader.ReadAndAssert();
string? type = reader.Value?.ToString();

Type t = Type.GetType(type);
Type t = Type.GetType(type!)!;

ReadAndAssertProperty(reader, ValuePropertyName);
reader.ReadAndAssert();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private object ReadObject(JsonReader reader)
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string propertyName = reader.Value!.ToString();
string propertyName = reader.Value!.ToString()!;

if (!reader.Read())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer

#if HAVE_DATE_TIME_OFFSET
Type t = (nullable)
? Nullable.GetUnderlyingType(objectType)
? Nullable.GetUnderlyingType(objectType)!
: objectType;
#endif

Expand Down Expand Up @@ -167,6 +167,8 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
return null;
}

MiscellaneousUtils.Assert(dateText != null);

#if HAVE_DATE_TIME_OFFSET
if (t == typeof(DateTimeOffset))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer

#if HAVE_DATE_TIME_OFFSET
Type t = (ReflectionUtils.IsNullableType(objectType))
? Nullable.GetUnderlyingType(objectType)
? Nullable.GetUnderlyingType(objectType)!
: objectType;
if (t == typeof(DateTimeOffset))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
reader.ReadAndAssert();

Type t = ReflectionUtils.IsNullableType(objectType)
? Nullable.GetUnderlyingType(objectType)
? Nullable.GetUnderlyingType(objectType)!
: objectType;

ReflectionObject reflectionObject = ReflectionObjectPerType.Get(t);
Expand All @@ -111,7 +111,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer

while (reader.TokenType == JsonToken.PropertyName)
{
string propertyName = reader.Value!.ToString();
string propertyName = reader.Value!.ToString()!;
if (string.Equals(propertyName, KeyName, StringComparison.OrdinalIgnoreCase))
{
reader.ReadForTypeAndAssert(keyContract, false);
Expand Down Expand Up @@ -145,7 +145,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
public override bool CanConvert(Type objectType)
{
Type t = (ReflectionUtils.IsNullableType(objectType))
? Nullable.GetUnderlyingType(objectType)
? Nullable.GetUnderlyingType(objectType)!
: objectType;

if (t.IsValueType() && t.IsGenericType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private Regex ReadRegexObject(JsonReader reader, JsonSerializer serializer)
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string propertyName = reader.Value!.ToString();
string propertyName = reader.Value!.ToString()!;

if (!reader.Read())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
}

bool isNullable = ReflectionUtils.IsNullableType(objectType);
Type t = isNullable ? Nullable.GetUnderlyingType(objectType) : objectType;
Type t = isNullable ? Nullable.GetUnderlyingType(objectType)! : objectType;

try
{
Expand Down Expand Up @@ -267,7 +267,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
public override bool CanConvert(Type objectType)
{
Type t = (ReflectionUtils.IsNullableType(objectType))
? Nullable.GetUnderlyingType(objectType)
? Nullable.GetUnderlyingType(objectType)!
: objectType;

return t.IsEnum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ internal class UnixDateTimeConverter : DateTimeConverterBase
{
internal static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

/// <summary>
/// Gets or sets a value indicating whether the dates before Unix epoch
/// should converted to and from JSON.
/// </summary>
/// <value>
/// <c>true</c> to allow converting dates before Unix epoch to and from JSON;
/// <c>false</c> to throw an exception when a date being converted to or from JSON
/// occurred before Unix epoch. The default value is <c>false</c>.
/// </value>
public bool AllowPreEpoch { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="UnixDateTimeConverter"/> class.
/// </summary>
public UnixDateTimeConverter() : this(false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnixDateTimeConverter"/> class.
/// </summary>
/// <param name="allowPreEpoch">
/// <c>true</c> to allow converting dates before Unix epoch to and from JSON;
/// <c>false</c> to throw an exception when a date being converted to or from JSON
/// occurred before Unix epoch. The default value is <c>false</c>.
/// </param>
public UnixDateTimeConverter(bool allowPreEpoch)
{
AllowPreEpoch = allowPreEpoch;
}

/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
Expand All @@ -61,7 +92,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
throw new JsonSerializationException("Expected date object value.");
}

if (seconds < 0)
if (!AllowPreEpoch && seconds < 0)
{
throw new JsonSerializationException("Cannot convert date value that is before Unix epoch of 00:00:00 UTC on 1 January 1970.");
}
Expand Down Expand Up @@ -108,12 +139,12 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected Integer or String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
}

if (seconds >= 0)
if (AllowPreEpoch || seconds >= 0)
{
DateTime d = UnixEpoch.AddSeconds(seconds);

#if HAVE_DATE_TIME_OFFSET
Type t = (nullable)
Type? t = (nullable)
? Nullable.GetUnderlyingType(objectType)
: objectType;
if (t == typeof(DateTimeOffset))
Expand Down
Loading

0 comments on commit 01afbe7

Please sign in to comment.