-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restsharp: Use System.Text.Json deserializer
- Loading branch information
Showing
7 changed files
with
173 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace GiantBomb.Api.Serialization | ||
{ | ||
internal class BoolConverter : JsonConverter<bool> | ||
{ | ||
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
// return literal boolean | ||
if (reader.TokenType == JsonTokenType.True || reader.TokenType == JsonTokenType.False) | ||
{ | ||
return reader.GetBoolean(); | ||
} | ||
|
||
// string encoded booleans | ||
if (reader.TokenType == JsonTokenType.String) | ||
{ | ||
string boolString = reader.GetString(); | ||
if (bool.TryParse(boolString, out bool result)) | ||
{ | ||
return result; | ||
} | ||
|
||
return Convert.ToInt32(boolString) != 0; | ||
} | ||
|
||
// final fallback to number, expected to throw if this doesn't work. | ||
return Convert.ToInt32(reader.GetInt32()) != 0; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options) | ||
{ | ||
// we don't need to write anything. | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace GiantBomb.Api.Serialization | ||
{ | ||
internal class DateTimeConverter : JsonConverter<DateTime> | ||
{ | ||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return DateTime.Parse(reader.GetString()); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss")); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace GiantBomb.Api.Serialization | ||
{ | ||
internal class SnakeCaseNamingPolicy : JsonNamingPolicy | ||
{ | ||
internal static SnakeCaseNamingPolicy SnakeCase { get; } = new SnakeCaseNamingPolicy(); | ||
|
||
// SnakeCase implementation copied from Newtonsoft.Json | ||
private enum SeparatedCaseState | ||
{ | ||
Start, | ||
Lower, | ||
Upper, | ||
NewWord | ||
} | ||
|
||
private static string ToSeparatedCase(string s, char separator) | ||
{ | ||
if (string.IsNullOrEmpty(s)) | ||
{ | ||
return s; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
SeparatedCaseState state = SeparatedCaseState.Start; | ||
|
||
for (int i = 0; i < s.Length; i++) | ||
{ | ||
if (s[i] == ' ') | ||
{ | ||
if (state != SeparatedCaseState.Start) | ||
{ | ||
state = SeparatedCaseState.NewWord; | ||
} | ||
} | ||
else if (char.IsUpper(s[i])) | ||
{ | ||
switch (state) | ||
{ | ||
case SeparatedCaseState.Upper: | ||
bool hasNext = (i + 1 < s.Length); | ||
if (i > 0 && hasNext) | ||
{ | ||
char nextChar = s[i + 1]; | ||
if (!char.IsUpper(nextChar) && nextChar != separator) | ||
{ | ||
sb.Append(separator); | ||
} | ||
} | ||
break; | ||
case SeparatedCaseState.Lower: | ||
case SeparatedCaseState.NewWord: | ||
sb.Append(separator); | ||
break; | ||
} | ||
|
||
char c; | ||
c = char.ToLowerInvariant(s[i]); | ||
sb.Append(c); | ||
|
||
state = SeparatedCaseState.Upper; | ||
} | ||
else if (s[i] == separator) | ||
{ | ||
sb.Append(separator); | ||
state = SeparatedCaseState.Start; | ||
} | ||
else | ||
{ | ||
if (state == SeparatedCaseState.NewWord) | ||
{ | ||
sb.Append(separator); | ||
} | ||
|
||
sb.Append(s[i]); | ||
state = SeparatedCaseState.Lower; | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
public override string ConvertName(string name) => ToSeparatedCase(name, '_'); | ||
} | ||
} |