-
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.
Showing
11 changed files
with
450 additions
and
193 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,53 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ChatAAC.Converters; | ||
|
||
|
||
/// <summary> | ||
/// Custom JSON converter for handling 'id' values. Converts the value to an integer. | ||
/// </summary> | ||
public class IdConverter : JsonConverter<int> | ||
{ | ||
/// <summary> | ||
/// Reads and converts the JSON data to an integer. | ||
/// </summary> | ||
/// <param name="reader">The Utf8JsonReader to read from.</param> | ||
/// <param name="typeToConvert">The type to convert to. In this case, it's always int.</param> | ||
/// <param name="options">The JsonSerializerOptions to use during deserialization.</param> | ||
/// <returns>The converted integer value.</returns> | ||
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
// If the value is a number, return it as an int | ||
if (reader.TokenType == JsonTokenType.Number) | ||
{ | ||
return reader.GetInt32(); | ||
} | ||
// If the value is a string, try to convert it to an integer | ||
else if (reader.TokenType == JsonTokenType.String) | ||
{ | ||
if (int.TryParse(reader.GetString(), out var result)) | ||
{ | ||
return result; | ||
} | ||
throw new JsonException($"Unable to convert string to int: {reader.GetString()}"); | ||
} | ||
else | ||
{ | ||
throw new JsonException($"Unexpected token {reader.TokenType} when parsing id."); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Writes the integer value as JSON data. | ||
/// </summary> | ||
/// <param name="writer">The Utf8JsonWriter to write to.</param> | ||
/// <param name="value">The integer value to write.</param> | ||
/// <param name="options">The JsonSerializerOptions to use during serialization.</param> | ||
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) | ||
{ | ||
// Write as an int | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ChatAAC.Converters; | ||
|
||
/// <summary> | ||
/// Custom JSON converter for a 2D array of nullable integers. | ||
/// This converter is used to serialize and deserialize the 'order' property in a grid. | ||
/// </summary> | ||
public class OrderConverter : JsonConverter<int?[][]> | ||
{ | ||
/// <summary> | ||
/// Reads and converts the JSON to a 2D array of nullable integers. | ||
/// </summary> | ||
/// <param name="reader">The Utf8JsonReader to read from.</param> | ||
/// <param name="typeToConvert">The type to convert to.</param> | ||
/// <param name="options">The JsonSerializerOptions to use.</param> | ||
/// <returns>A 2D array of nullable integers.</returns> | ||
public override int?[][] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
// Ensure the token is a start array | ||
if (reader.TokenType != JsonTokenType.StartArray) | ||
{ | ||
throw new JsonException("Expected start of array"); | ||
} | ||
|
||
var result = new List<int?[]>(); | ||
|
||
// Read each row of the array | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndArray) | ||
break; | ||
|
||
if (reader.TokenType != JsonTokenType.StartArray) | ||
throw new JsonException("Expected start of inner array"); | ||
|
||
var innerList = new List<int?>(); | ||
|
||
// Read values in the array | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndArray) | ||
break; | ||
|
||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
innerList.Add(null); | ||
} | ||
else if (reader.TokenType == JsonTokenType.Number) | ||
{ | ||
innerList.Add(reader.GetInt32()); | ||
} | ||
else | ||
{ | ||
throw new JsonException("Expected number or null"); | ||
} | ||
} | ||
|
||
result.Add(innerList.ToArray()); | ||
} | ||
|
||
return result.ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Writes a 2D array of nullable integers as JSON. | ||
/// </summary> | ||
/// <param name="writer">The Utf8JsonWriter to write to.</param> | ||
/// <param name="value">The value to convert to JSON.</param> | ||
/// <param name="options">The JsonSerializerOptions to use.</param> | ||
public override void Write(Utf8JsonWriter writer, int?[][] value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartArray(); | ||
|
||
foreach (var innerArray in value) | ||
{ | ||
writer.WriteStartArray(); | ||
|
||
foreach (var item in innerArray) | ||
{ | ||
if (item.HasValue) | ||
{ | ||
writer.WriteNumberValue(item.Value); | ||
} | ||
else | ||
{ | ||
writer.WriteNullValue(); | ||
} | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,25 +1,37 @@ | ||
using System.Text.Json.Serialization; | ||
using ChatAAC.Converters; | ||
|
||
namespace ChatAAC.Models.Obf; | ||
|
||
// Class for Button | ||
public class Button | ||
namespace ChatAAC.Models.Obf | ||
{ | ||
[JsonPropertyName("id")] public string Id { get; set; } = string.Empty; | ||
// Klasa reprezentująca przycisk | ||
public class Button | ||
{ | ||
[JsonPropertyName("id")] | ||
[JsonConverter(typeof(IdConverter))] | ||
public int Id { get; set; } | ||
|
||
[JsonPropertyName("label")] public string Label { get; set; } = string.Empty; | ||
[JsonPropertyName("label")] | ||
public string Label { get; set; } = string.Empty; // Upewniamy się, że label jest stringiem | ||
|
||
[JsonPropertyName("image_id")] public string ImageId { get; set; } = string.Empty; | ||
[JsonPropertyName("image_id")] | ||
public string ImageId { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("border_color")] public string BorderColor { get; set; } = string.Empty; | ||
[JsonPropertyName("border_color")] | ||
public string BorderColor { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("background_color")] public string BackgroundColor { get; set; } = string.Empty; | ||
[JsonPropertyName("background_color")] | ||
public string BackgroundColor { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("vocalization")] public string Vocalization { get; set; } = string.Empty; | ||
[JsonPropertyName("vocalization")] | ||
public string Vocalization { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("load_board")] public LoadBoard? LoadBoard { get; set; } | ||
[JsonPropertyName("load_board")] | ||
public LoadBoard? LoadBoard { get; set; } | ||
|
||
[JsonPropertyName("action")] public string Action { get; set; } = string.Empty; | ||
[JsonPropertyName("action")] | ||
public string Action { get; set; } = string.Empty; | ||
|
||
[JsonIgnore] public Image? Image { get; set; } | ||
[JsonIgnore] | ||
public Image? Image { get; set; } | ||
} | ||
} |
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
Oops, something went wrong.