Skip to content

Commit cc0f7db

Browse files
committed
Added the amazing (no) WhateverToStringConverter, because Steam wants to have weird type in their json ...
1 parent c187c5f commit cc0f7db

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Dysnomia.Common.WebAPIWrapper.Helpers {
6+
public class WhateverToStringConverter : JsonConverter<string> {
7+
public override string Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) {
8+
if (reader.TokenType == JsonTokenType.Number) {
9+
return reader.GetUInt64().ToString();
10+
}
11+
12+
if (reader.TokenType == JsonTokenType.String) {
13+
return reader.GetString();
14+
}
15+
16+
if (reader.TokenType == JsonTokenType.False) {
17+
return "false";
18+
}
19+
20+
if (reader.TokenType == JsonTokenType.True) {
21+
return "true";
22+
}
23+
24+
if (reader.TokenType == JsonTokenType.Null) {
25+
return null;
26+
}
27+
28+
using (JsonDocument document = JsonDocument.ParseValue(ref reader)) {
29+
throw new Exception($"unable to parse {document.RootElement.ToString()} to string");
30+
}
31+
}
32+
33+
public override bool CanConvert(Type typeToConvert) {
34+
return true;
35+
}
36+
37+
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) {
38+
JsonSerializer.Serialize(writer, value, options);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)