Skip to content

Commit 189d011

Browse files
author
Justin
committed
Adding DateTime parsing checks because Valve's API returns bad dates (such as June 31).
1 parent c55550b commit 189d011

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

SourceSchemaParser/Dota2/DotaSchemaItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class DotaSchemaItemPriceInfo
4747
[JsonProperty("category_tags")]
4848
public string CategoryTags { get; set; }
4949

50+
[JsonConverter(typeof(DateTimeJsonConverter))]
5051
[JsonProperty("date")]
5152
public DateTime? Date { get; set; }
5253

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
5+
namespace SourceSchemaParser.JsonConverters
6+
{
7+
/// <summary>
8+
/// Had to add a JsonConverter specifically for DateTimes to handle DateTimes that aren't valid. Valve doesn't do checks on their dates apparently since I've seen schemas with June 31 in them.
9+
/// </summary>
10+
public class DateTimeJsonConverter : JsonConverter
11+
{
12+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
13+
{
14+
throw new NotImplementedException();
15+
}
16+
17+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
18+
{
19+
if (reader.TokenType == JsonToken.Null)
20+
{
21+
return null;
22+
}
23+
24+
JValue token = (JValue)JToken.Load(reader);
25+
26+
DateTime dateTime = DateTime.Now;
27+
bool success = DateTime.TryParse(token.Value.ToString(), out dateTime);
28+
if (success)
29+
{
30+
return dateTime;
31+
}
32+
else
33+
{
34+
return null;
35+
}
36+
}
37+
38+
public override bool CanWrite { get { return false; } }
39+
40+
public override bool CanConvert(Type objectType)
41+
{
42+
return typeof(DateTime).IsAssignableFrom(objectType);
43+
}
44+
}
45+
}

SourceSchemaParser/SchemaItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class SchemaItem
3030
[JsonProperty("creation_date")]
3131
public DateTime? CreationDate { get; set; }
3232

33+
[JsonConverter(typeof(DateTimeJsonConverter))]
3334
[JsonProperty("expiration_date")]
3435
public DateTime? ExpirationDate { get; set; }
3536
}

SourceSchemaParser/SourceSchemaParser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="DOTA2\DotaSchemaContainer.cs" />
6161
<Compile Include="DOTA2\DotaSchemaItemAutograph.cs" />
6262
<Compile Include="DOTA2\DotaSchemaItemSet.cs" />
63+
<Compile Include="JsonConverters\DateTimeJsonConverter.cs" />
6364
<Compile Include="JsonConverters\DotaSchemaItemCreationDateJsonConverter.cs" />
6465
<Compile Include="JsonConverters\DotaSchemaItemSetItemsJsonConverter.cs" />
6566
<Compile Include="JsonConverters\DotaSchemaItemSetJsonConverter.cs" />

0 commit comments

Comments
 (0)