C
C#7mo ago
Mekasu0124

✅ 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;
}
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.
57 Replies
Mekasu0124
Mekasu01247mo ago
should I remove the [JsonPropertyName("default")] from the class decorators?
Jimmacle
Jimmacle7mo ago
well, it's saying you have 2 property names that conflict and you're giving 2 properties identical names
Mekasu0124
Mekasu01247mo ago
I wasn't sure if they both needed the decorator
Jimmacle
Jimmacle7mo ago
the attribute is for specifying a non-conventional name, it's not required at all
Mekasu0124
Mekasu01247mo ago
attribute. not decorator that's what I meant lol
Jimmacle
Jimmacle7mo ago
if it's not present the serializer will name the property based on the CLR property name and whatever naming convention you set in the options
Mekasu0124
Mekasu01247mo ago
ok well the function is supposed to create a json file that looks like this
{
"Username Here": {
"styles": {
"fontName": "some font name",
...
},
"settings": {
"autoCorrect": false
},
}
}
{
"Username Here": {
"styles": {
"fontName": "some font name",
...
},
"settings": {
"autoCorrect": false
},
}
}
so that as the user changes their custom settings, it can be added to replaced or deleted.
Jimmacle
Jimmacle7mo ago
i don't see any properties named "default" there
Mekasu0124
Mekasu01247mo ago
"default" would be present if the file is just newly created in place of "Username here". In the function parameters, string username = "default" is there
Jimmacle
Jimmacle7mo ago
the attributes name the properties they're put on, not the parent
Mekasu0124
Mekasu01247mo ago
it'll also remain there too as it's the style for the loading screen and the login screen ok so how should I fix my current code to get my desired output?
Jimmacle
Jimmacle7mo ago
remove the attributes you're currently trying to name the "styles" and "settings" keys to both be "default" instead, which would produce invalid json
Mekasu0124
Mekasu01247mo ago
ok they're removed. is there anything else I should change?
Jimmacle
Jimmacle7mo ago
did that fix your problem?
Mekasu0124
Mekasu01247mo ago
{
"StyleModel": [
{
"Username": "default",
"FontName": "Times New Roman",
"FontStyle": "regular",
"FontSize": "12",
"FontColor": "#CDCDCD",
"FontUnderline": "none",
"TextAlign": "left",
"BackgroundColor": "#000000",
"BorderColor": "#24de45"
}
],
"SettingsModel": [
{
"Username": "default",
"AutoCorrect": false
}
]
}
{
"StyleModel": [
{
"Username": "default",
"FontName": "Times New Roman",
"FontStyle": "regular",
"FontSize": "12",
"FontColor": "#CDCDCD",
"FontUnderline": "none",
"TextAlign": "left",
"BackgroundColor": "#000000",
"BorderColor": "#24de45"
}
],
"SettingsModel": [
{
"Username": "default",
"AutoCorrect": false
}
]
}
it did, however, it's a dictionary within a list instead of just a dictionary wait I guess that's ok
Want results from more Discord servers?
Add your server