-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1207864
commit a26ef63
Showing
12 changed files
with
828 additions
and
559 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ChatAAC.Converters; | ||
|
||
public class IntFromStringConverter : JsonConverter<int> | ||
{ | ||
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
try | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.Number: | ||
return reader.GetInt32(); | ||
case JsonTokenType.String: | ||
{ | ||
var stringValue = reader.GetString(); | ||
if (int.TryParse(stringValue, out int result)) | ||
{ | ||
return result; | ||
} | ||
|
||
break; | ||
} | ||
default: | ||
return 0; | ||
} | ||
} | ||
catch | ||
{ | ||
// Ignorujemy wyjątki i przechodzimy do zwrócenia wartości domyślnej | ||
} | ||
|
||
// Jeśli nie udało się sparsować, zwracamy domyślną wartość (np. 0) lub rzucamy wyjątek | ||
return 0; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteNumberValue(value); | ||
} | ||
} |
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
Oops, something went wrong.