File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Dysnomia.Common.WebAPIWrapper/Helpers Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments