✅ Basic Json issue
BlazeBin - picuzuelngyk
A tool for sharing your source code with the world!
64 Replies
You'll need to provide an actual question
Do you get an error? If so, on which line?
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);
What's your JSON?
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; }
}
I think Z is asking for your literal json
What's your JSON?
The text
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");
Well, if it's blank then there's nothing to deserialize
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
Also, not sure why you're even writing the JSON manually in the first place
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
Well, then there's the issue of the JSON you posted here not being deserializeable to
List<ImagePathIndexPair>
indexes are compared and if said index is already in use then itll replace that file path with the new one
To
ImagePathIndexPair
, yes. But not to a list of anythingbecause only one object is saved within the file ?
Correct
so a list can never contain just one object?
Also @ZZZZZZZZZZZZZZZZZZZZZZZZZ mentioned I was writing this manually how could it be better written ?
A list can contain just one object, but you haven't written any list at all
You've only written an object
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
A list in json is surrounded by []
You haven't written those 🙂
so rather then the using writer how would I add .add the object
What we're saying isn't related to your C#
Your json does not represent what you want it to represent
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
Well, then you need to actually create json that represents a list
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 ?
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
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
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
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
Look, we're trying to help you here, but you're not helping us help you
if its serlized how do I add it to the list/save it to the file
You said that you are manually creating this file
How
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
That did not answer the question
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
Ok, let's take a step back here. What are you actually trying to do
High level goal
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
Ok, next question: none of that appears to have anything to do with json
So why are we talking about json?
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
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
i mean yeah thats is what im trying to do
The code you sent isn't anything like what I just said
i believe in functions you know, program starts and calls the load function
What
has a add button that calls the save function
the load function Ill show you one sec
What you have in your save function isn't anything like I just said
ohh hmm
You are attempting to manually serialize and append to the existing content in the file
Do not do this
It cannot work with JSON
just overwrite after reading and replacing ?
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 filegiveme 5-10 minutes
i just lost the plot
whats that website i use to show you
BlazeBin - fwkeqxjbayyk
A tool for sharing your source code with the world!
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
You probably don't want to be doing that serialize and write inside the loop
thank you
ahhh boy still doesnt work
same reason
Be more specific
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!
Yw
should I close this thread and if so how ?
$close
Use the /close command to mark a forum thread as answered