✅ JSON names collide? Avalonia
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
should I remove the
[JsonPropertyName("default")]
from the class decorators?well, it's saying you have 2 property names that conflict and you're giving 2 properties identical names
I wasn't sure if they both needed the decorator
the attribute is for specifying a non-conventional name, it's not required at all
attribute. not decorator that's what I meant lol
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
ok well the function is supposed to create a json file that looks like this
so that as the user changes their custom settings, it can be added to replaced or deleted.
i don't see any properties named "default" there
"default"
would be present if the file is just newly created in place of "Username here"
. In the function parameters, string username = "default"
is therethe attributes name the properties they're put on, not the parent
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?
remove the attributes
you're currently trying to name the "styles" and "settings" keys to both be "default" instead, which would produce invalid json
ok they're removed.
is there anything else I should change?
did that fix your problem?
it did, however, it's a dictionary within a list instead of just a dictionary
wait
I guess that's ok
that json matches your
UserSetting
modelbecause doing it that way will allow multiple local users so like people living within the same house hold that share a computer can create new logins and have their own individual settings, but I'm not sure on how to access the information
if you want a dictionary of these keyed on usernames you'll need to actually have a
Dictionary<string, UserSetting>
to serializeso this instead of that?
try it and see
correct
your data has to actually model the json you want to produce
you can't force a
UserSetting
to be serialized as a Dictionary<string, UserSetting>
so first I need to change the function line from
List<StyleModel>
to Dictionary<string, StyleModel>
right?start with the line you shared,
var json = JsonSerializer.Serialize(data);
data
needs to be an instance of the type you want to serialize
if you want it to be a dictionary you have to make it a dictionaryok so far I have changed
and so far my errors are
I'm just going to put it back to being a dictionary within a list. I can deal with that. This is causing more errors than it's worth lol
why did you add dictionaries inside your user settings model?
and i mean, this
Dictionary<string, UserSEtting> data = new UserSetting
is obviously not going to workok so going to back to what I had shown above, when I go to pull the settings and styles by the default username, how would I do that?
that is not a collection of user settings
that's a single user settings object
so you can't get anything by anything, because there's only one thing to get
i'm confused why you're separating "style" and "settings" into 2 collections
I don't have to
if the goal is to look up all a user's settings by name, then the optimal way to structure that is a dictionary where the keys are the usernames and the values are the users' settings
ok so let's go with the desired output of the json file to be
how would I need to structure my function and how it writes it to the json file?
I'm pretty sure I can get rid of the UserSetting class
you can't
that models your users' settings
ok so question then
take some time to really think about how this data is structured and how that would translate to C# classes/collections
take some time to really think about how this data is structured and how that would translate to C# classes/collectionsI was trying to do that. The reason I said I could likely get rid of the SettingsModel and StyleModel (if I didn't say those specifically, apologies, they're what I meant) because I was going to restructure the user model to hold all the needed information, build all of the information into a user who's username is default and then just append the user to the json file
right, you can restructure it to just hold the settings themselves and make that the value of the dictionary
and they don't need the username inside them because that's going to be the key
ok so remove all the usernames from the 3 classes that aren't the User class
yeah
done. ok so how would I restucture the user class (thinking outloud) brb let me see if I can get this right
ok so my logic is that the UserSetting model will already be holding the StyleModel and the SettingsModel information when called, and given that the UserSetting class holds two lists, I figured it needed to be a List<> itself so I made it a list too. How far off am I?
but why does usersetting hold 2 lists?
does one user have mulitple sets of settings?
that's how it's setup. I was trying to find the thread from the last two weeks range that someone helped me build this function because I wanted it as lists instead of dictionaries as they're easier to index, but after listening to a podcast, I learned that dictionaries are faster so now I want dictionaries lol I'm just hung up on trying to make the changes
I'm presuming that they don't have to be lists since it's only going to be one user's settings not a list of user's and their settings
ok so I made that change as it just felt right to do lol
ok so I've gone through all of my models and made them single-entity use. Now I'm back to working on the function and here's where I'm at so far.
so right now it writes the json file with no problems when the json file doesn't exist. However, when running the project a second time, I get the error
is this where I would possibly use something like IObservableList<string>?
I believe this is because it's not saved in the correct format. All the information about the user should be a dictionary as the value to the username key pair like
JsonSerializer.Serialize<User>(user)
vs.
JsonSerializer.Deserialize<List<User>>(jsonObject)
I was trying to make it be a list of users in case there is more than one user in the household that makes more than one account
also, it's not going to serialize into a dictionary if you don't literally use a dictionary in the correct place in your model
so like it would read the json file and then take each user in the json file and add them to the list of users
oh. ok
decide on whether you want a list or dictionary
you can't have it both ways unless you want to do extra processing after deserializing
I'd rather have a dictionary like
I can change the code to iterate through .Keys() no problem
yes, that's what i was getting at here https://discord.com/channels/143867839282020352/1211023309722878043/1211026060934910013
Jimmacle
if you want a dictionary of these keyed on usernames you'll need to actually have a
Dictionary<string, UserSetting>
to serializeQuoted by
<@901546879530172498> from #JSON names collide? Avalonia (click here)
React with ❌ to remove this embed.
that JSON structure maps to a C# dictionary
it's best to just put that code in your IDE and see if it's even valid
(it's not)
the json serializer de/serializes a single object into JSON
if you want the output JSON to be a dictionary, the input object has to be a dictionary
yes
ok so now the else statement. This is for when the styles file does exists. It's supposed to find the custom styles for the user and return the default styles if no custom settings exist under the current username
to clarify, this is the full if statement
the return user at the bottom is outside of the if statement so that no matter how the if statement triggers, either user will be returned so I guess I have that taken care of
forgot that I went back to dictionaries. Here's the updated code
you're using a dictionary like a list
you don't get the performance benefits that way
a dictionary's main benefit is O(1) search, but you're iterating over the entire collection making the search O(n)
the shallow copy of the user in your code is also unnecessary
also, when accessing a dictionary just look it up once and use that reference, you're doing a dictionary lookup 6 times for one object
ok so running into other problems while trying to get the program to run so that I can test things, my first issue is this is my LoginViewModel. It returns the user model back to the MainWindowViewModel to be pass on to whatever screen the user goes to. That is done like this
I'm currently getting the error on the
.Merge
part of the Observable
that says `The type arguments for method 'Observable.Merge<TSource>(IObservable<IObservable<TSource>>, int)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
nevermind I got it ifugred out. Thanks for all your help ❤️