C
C#7mo ago
Blasphemy

✅ Basic Json issue

BlazeBin - picuzuelngyk
A tool for sharing your source code with the world!
64 Replies
333fred
333fred7mo ago
You'll need to provide an actual question
Tvde1
Tvde17mo ago
Do you get an error? If so, on which line?
Blasphemy
Blasphemy7mo ago
so if we look at line 36 when say my ButtonImagePaths1.json is either empty or the index isn't found in a saved object I add the object in said format, in fact the issue only happens once at least one object has been added to the file and the issue occurs in the LoadButtonImagePaths function List<ImagePathIndexPair> ImagePathPairIndexs = JsonSerializer.Deserialize<List<ImagePathIndexPair>>(fileContents);
Angius
Angius7mo ago
What's your JSON?
Blasphemy
Blasphemy7mo ago
people have been saying strange things like that , I think you're referring to -- public class ImagePathIndexPair { public string ImagePath { get; set; } public int Index { get; set; } }
333fred
333fred7mo ago
I think Z is asking for your literal json
Angius
Angius7mo ago
What's your JSON?
333fred
333fred7mo ago
The text
Blasphemy
Blasphemy7mo ago
oh you mean the file ? its blank it doesn't exist and is created for the purpose of saving this data for future use of the app. the information is for sure added to the file and ive been able to confrim that via this line of code in my main - -Process.Start("notepad.exe", "ButtonImagePaths1.json");
Angius
Angius7mo ago
Well, if it's blank then there's nothing to deserialize
Blasphemy
Blasphemy7mo ago
agreed ! but the issue only begins when something has been added, so I assume that must of been during an exe of line 36. the formatting looks alright ill show you a screenshot of that
Angius
Angius7mo ago
Also, not sure why you're even writing the JSON manually in the first place
Blasphemy
Blasphemy7mo ago
No description
Blasphemy
Blasphemy7mo ago
im writing it dynamically it find a file dialog file path I have selected then sends it to the save function with a predetermined index
Angius
Angius7mo ago
Well, then there's the issue of the JSON you posted here not being deserializeable to List<ImagePathIndexPair>
Blasphemy
Blasphemy7mo ago
indexes are compared and if said index is already in use then itll replace that file path with the new one
Angius
Angius7mo ago
To ImagePathIndexPair, yes. But not to a list of anything
Blasphemy
Blasphemy7mo ago
because only one object is saved within the file ?
333fred
333fred7mo ago
Correct
Blasphemy
Blasphemy7mo ago
so a list can never contain just one object? Also @ZZZZZZZZZZZZZZZZZZZZZZZZZ mentioned I was writing this manually how could it be better written ?
333fred
333fred7mo ago
A list can contain just one object, but you haven't written any list at all You've only written an object
Blasphemy
Blasphemy7mo ago
I mean what you just said I hate to sound like yoda but , A list can contain just one object You've only written an object but you haven't written any list at all in fact after reading that like that it made sense lol
333fred
333fred7mo ago
A list in json is surrounded by [] You haven't written those 🙂
Blasphemy
Blasphemy7mo ago
so rather then the using writer how would I add .add the object
333fred
333fred7mo ago
What we're saying isn't related to your C# Your json does not represent what you want it to represent
Blasphemy
Blasphemy7mo ago
I'd like the everything within the file to be part of the list so when I open the file and deserlize it I can access the different file paths via the Index value
333fred
333fred7mo ago
Well, then you need to actually create json that represents a list
Blasphemy
Blasphemy7mo ago
so rather then the lines ImagePathPairIndexs.Add(new ImagePathIndexPair { ImagePath = pair.ImagePath, Index = pair.Index }); writer.WriteLine($"{{"ImagePath": "{pair.ImagePath}", "Index": {pair.Index}}}"); i would need to write something along the lines of what ?
333fred
333fred7mo ago
again, this isn't related to your C# The json that you are feeding to it does not represent the thing you want it to represent This is wrong It is a single object. Not a list And it won't deserialize into a list
Blasphemy
Blasphemy7mo ago
understood, so I need to add the singleton object to a list but how if the file is currently empty or should I say the singleton object will instantiated a list
333fred
333fred7mo ago
Why are you trying to manually write the json? Don't do that. Just serialize the thing you want to serialize, and deserialize the thing you want to deserialize
Blasphemy
Blasphemy7mo ago
well i asking what it meant by manually and how it can be done better but you didn't say, Ill try what you said but my problem is if I haven't instantated a value for the list then I have no way to serialize it so I'd have to reword the code give me a minute no man i don't get it f see ok if its serlized how do I add it to the list/save it to the file
333fred
333fred7mo ago
Look, we're trying to help you here, but you're not helping us help you
Blasphemy
Blasphemy7mo ago
if its serlized how do I add it to the list/save it to the file
333fred
333fred7mo ago
You said that you are manually creating this file How
Blasphemy
Blasphemy7mo ago
well that is something I struggled to understand aswell but it just so happens all I had to do was open a made up file and it would be created realtive to the program
333fred
333fred7mo ago
That did not answer the question
Blasphemy
Blasphemy7mo ago
I know ? It just creates the file IKR! although I never said I was manually creating the file rather that I'm dynamically generting into the file the file dialogs I choose to have within the file in a object based format
333fred
333fred7mo ago
Ok, let's take a step back here. What are you actually trying to do High level goal
Blasphemy
Blasphemy7mo ago
a add button when select opens up file explorer and takes me to pictures, I select one then its associted with a index value and saved
333fred
333fred7mo ago
Ok, next question: none of that appears to have anything to do with json So why are we talking about json?
Blasphemy
Blasphemy7mo ago
so that next I run the program the index values can be accessed, and the index can be replaced with new file paths at any time so Json seemed like a good way to encapsulate the object in the what was a txt file but now a json file
333fred
333fred7mo ago
Ok. So the code you have is really way too complicated At the start of your program, read that file in and deserialize it into a list. When you need to add something new, add it to the list When you need to save, just serialize the entire list and write that out to file, overwriting whatever is in there already
Blasphemy
Blasphemy7mo ago
i mean yeah thats is what im trying to do
333fred
333fred7mo ago
The code you sent isn't anything like what I just said
Blasphemy
Blasphemy7mo ago
i believe in functions you know, program starts and calls the load function
333fred
333fred7mo ago
What
Blasphemy
Blasphemy7mo ago
has a add button that calls the save function the load function Ill show you one sec
333fred
333fred7mo ago
What you have in your save function isn't anything like I just said
Blasphemy
Blasphemy7mo ago
ohh hmm
333fred
333fred7mo ago
You are attempting to manually serialize and append to the existing content in the file Do not do this It cannot work with JSON
Blasphemy
Blasphemy7mo ago
just overwrite after reading and replacing ?
333fred
333fred7mo ago
Add to the literal List<ImagePathIndexPair> instance, and serialize the entire list Or, use a different format that can actually append without having to rewrite the entire file
Blasphemy
Blasphemy7mo ago
giveme 5-10 minutes i just lost the plot whats that website i use to show you
Blasphemy
Blasphemy7mo ago
BlazeBin - fwkeqxjbayyk
A tool for sharing your source code with the world!
Blasphemy
Blasphemy7mo ago
before you shout at me allow me to explain that to me this is what makes sense and yes it errors pretty much on start up because the .json file is in use also if the list is empty then it runs an error so it can not start empty
333fred
333fred7mo ago
You probably don't want to be doing that serialize and write inside the loop
Blasphemy
Blasphemy7mo ago
thank you ahhh boy still doesnt work same reason
333fred
333fred7mo ago
Be more specific
Blasphemy
Blasphemy7mo ago
ohohohohohohoh we are indeed moving in a postive direction!# I think that'll do it my next problem is a different topic but this is working @333fred thanks a ton!
333fred
333fred7mo ago
Yw
Blasphemy
Blasphemy7mo ago
should I close this thread and if so how ?
333fred
333fred7mo ago
$close
MODiX
MODiX7mo ago
Use the /close command to mark a forum thread as answered