C#C
C#2y ago
Mek

✅ JSON names collide? Avalonia

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Diary.Models;

public class UserSetting
{
    [JsonPropertyName("default")]
    public List<StyleModel> StyleModel { get; set; }

    [JsonPropertyName("default")]
    public List<SettingsModel> SettingsModel { get; set; }
}

public List<StyleModel> CheckForStyleFile(string username = "default")
{
    var data = new UserSetting
    {
        StyleModel =
        [
            new StyleModel
            {
                Username = username,
                FontName = "Times New Roman",
                FontStyle = "regular",
                FontSize = "12",
                FontColor = "#CDCDCD",
                FontUnderline = "none",
                TextAlign = "left",
                BackgroundColor = "#000000",
                BorderColor = "#24de45",
            }
        ],
        SettingsModel =
        [
            new SettingsModel
            {
                Username = username,
                AutoCorrect = false
            }
        ]
    };

    if (!File.Exists(_stylesFile))
    {
        var json = JsonSerializer.Serialize(data);
        File.AppendAllText(_stylesFile, json);
    }
    else
    {
        var jsonObject = File.ReadAllText(_stylesFile);
        data = JsonSerializer.Deserialize<UserSetting>(jsonObject);
    }

    return data.StyleModel;
}
Between the model class and the function I'm using it in, I have something wrong. I get the error The JSON property name for 'Diary.Models.UserSetting.default' collieds with another property. This is a new error to me. I tried looking at https://stackoverflow.com/questions/69448540/net-core-the-json-property-name-for-collides-with-another-property and https://stackoverflow.com/questions/24887705/json-net-conflicting-property-name-when-using-jsonpropertyattribute but I can't determine what I have wrong. Thanks.
Was this page helpful?