The input does not contain any JSON tokens
public void AddNewCommand(object sender, EventArgs e)
{
var path = Xamarin.Essentials.FileSystem.AppDataDirectory;
string title = Title;
string presPath = $"{path}/presets/{title}";
toSave save = new toSave
{
size_1 = s_ize,
angle_1 = a_ngle,
auto_1 = automatic
};
DirectoryInfo inf = new DirectoryInfo(presPath);
for (int i = -1; i < inf.GetFiles().Length; i++)
{
Console.WriteLine(i);
if (!System.IO.File.Exists($"{presPath}/{i+1}.command"))
{
using (StreamWriter sw = new StreamWriter($"{presPath}/{i+1}.command"))
{
string str = JsonSerializer.Serialize(save, typeof(toSave));
sw.Write(str);
ReadCommand();
HideCommandTask();
}
}
}
}
Сериализация проходит в этом методе, здесь ошибок нету
public void ReadCommand()
{
CommandList = new List<Commands>();
var path = Xamarin.Essentials.FileSystem.AppDataDirectory;
string title = Title;
string presPath = $"{path}/presets/{title}";
DirectoryInfo inf = new DirectoryInfo(presPath);
for (int i = 0; i < inf.GetFiles().Length; i++)
{
string str = System.IO.File.ReadAllText($"{presPath}/{i}.command");
Console.WriteLine(str);
JsonSerializerOptions options = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
};
toSave saver = JsonSerializer.Deserialize<toSave>(str, options); // Здесь выдаёт ошибку "System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 |"
А вот в методе десеарелизации выдаёт ошибку Прошу помочь мне решить данную проблему.
Ответы (1 шт):
Автор решения: OrangeKeks
→ Ссылка
Решение скрывалось в проверке десереализуемой строки на содержание null значения или пустого текста: "".
if (str != "" & str != null)
{
var saver = JsonSerializer.Deserialize<toSave>(str, options);
CommandList.Add(new Commands { size = saver.size_1, angle = saver.angle_1, isAuto = saver.auto_1, number = i });
Comm_List.ItemsSource = null;
Comm_List.ItemsSource = CommandList;
}`
Проверка помогла исправить ошибку, тем, что начала отсеивать json-строки и пустые друг от друга.