C
C#2mo ago
rmg66

Issue with JSON serialization of a nested dictionary

I can update the allMediaSource nested dictionary fine and vs2022 debug shows it has new values, but when I serialize the nested dictionary, it has original values
Dictionary<string, Dictionary<string, Dictionary<string, string>>> allMediaSources = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
string jsonString = Properties.Settings.Default.MediaSources;

if (!string.IsNullOrEmpty(jsonString))
{
try
{
// Deserialize the JSON string into the nested dictionary structure
allMediaSources = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(jsonString)
?? new Dictionary<string, Dictionary<string, Dictionary<string, string>>>(); // Ensure a non-null dictionary
}
catch (Exception ex)
{
// Handle deserialization errors (optional logging or handling)
// For now, we'll just fall back to an empty dictionary
allMediaSources = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
}
}


// Ensure that the scraper key exists in allMediaSources
if (!allMediaSources.ContainsKey(scraper))
{
allMediaSources[scraper] = new Dictionary<string, Dictionary<string, string>>();
}

string newJsonString;

// Get the nested dictionary for the scraper
var nestedDictionary = allMediaSources[scraper];

// Check if the system key exists within the scraper's nested dictionary
if (!nestedDictionary.ContainsKey(system))
{
// If the system key doesn't exist, add the mediaSources dictionary
nestedDictionary.Add(system, mediaSources);
}
else
{
// If the system key exists, replace the existing dictionary with mediaSources
nestedDictionary[system] = mediaSources;
}

// Update property value
newJsonString = JsonSerializer.Serialize(allMediaSources);
Properties.Settings.Default.MediaSources = newJsonString;

// Save the changes to the settings
Properties.Settings.Default.Save();
Dictionary<string, Dictionary<string, Dictionary<string, string>>> allMediaSources = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
string jsonString = Properties.Settings.Default.MediaSources;

if (!string.IsNullOrEmpty(jsonString))
{
try
{
// Deserialize the JSON string into the nested dictionary structure
allMediaSources = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(jsonString)
?? new Dictionary<string, Dictionary<string, Dictionary<string, string>>>(); // Ensure a non-null dictionary
}
catch (Exception ex)
{
// Handle deserialization errors (optional logging or handling)
// For now, we'll just fall back to an empty dictionary
allMediaSources = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
}
}


// Ensure that the scraper key exists in allMediaSources
if (!allMediaSources.ContainsKey(scraper))
{
allMediaSources[scraper] = new Dictionary<string, Dictionary<string, string>>();
}

string newJsonString;

// Get the nested dictionary for the scraper
var nestedDictionary = allMediaSources[scraper];

// Check if the system key exists within the scraper's nested dictionary
if (!nestedDictionary.ContainsKey(system))
{
// If the system key doesn't exist, add the mediaSources dictionary
nestedDictionary.Add(system, mediaSources);
}
else
{
// If the system key exists, replace the existing dictionary with mediaSources
nestedDictionary[system] = mediaSources;
}

// Update property value
newJsonString = JsonSerializer.Serialize(allMediaSources);
Properties.Settings.Default.MediaSources = newJsonString;

// Save the changes to the settings
Properties.Settings.Default.Save();
18 Replies
rmg66
rmg662mo ago
debug showing updated allMediaSources, see circled item
rmg66
rmg662mo ago
No description
rmg66
rmg662mo ago
this is correct next code line serializes to a string, which has wrong info
newJsonString "{\"ArcadeDB\":{\"mame\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Cabinet\",\"VideoSource\":\"Standard\"},\"mastersystem\":{\"ImageSource\":\"Title\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Cabinet\",\"VideoSource\":\"Standard\"}},\"EmuMovies\":{\"mame\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Box 2D\",\"CartridgeSource\":\"Cartridge\",\"VideoSource\":\"Standard\"},\"mastersystem\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Box 2D\",\"CartridgeSource\":\"Cartridge\",\"VideoSource\":\"Standard\"}},\"ScreenScraper\":{\"mame\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Box 2D\",\"VideoSource\":\"Standard\"},\"mastersystem\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Box 2D\",\"VideoSource\":\"Standard\"}}}" string
newJsonString "{\"ArcadeDB\":{\"mame\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Cabinet\",\"VideoSource\":\"Standard\"},\"mastersystem\":{\"ImageSource\":\"Title\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Cabinet\",\"VideoSource\":\"Standard\"}},\"EmuMovies\":{\"mame\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Box 2D\",\"CartridgeSource\":\"Cartridge\",\"VideoSource\":\"Standard\"},\"mastersystem\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Box 2D\",\"CartridgeSource\":\"Cartridge\",\"VideoSource\":\"Standard\"}},\"ScreenScraper\":{\"mame\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Box 2D\",\"VideoSource\":\"Standard\"},\"mastersystem\":{\"ImageSource\":\"Screenshot\",\"LogoSource\":\"Clear Logo\",\"BoxSource\":\"Box 2D\",\"VideoSource\":\"Standard\"}}}" string
Imagesource should be Title in the newJsonString
maxmahem
maxmahem2mo ago
okay when you create nestedDictionary here:
var nestedDictionary = allMediaSources[scraper];
var nestedDictionary = allMediaSources[scraper];
You create a new variable that points to allMediaSources[scraper] however, reassigning this variable does not change the value of allMediaSources[scraper] it just assigns a new value to that varible. IE that variable now points to something else. if you want to update allMediaSources[scraper] you need to assign the new value to allMediaSources[scraper].
Aaron
Aaron2mo ago
they don't reassign that variable
maxmahem
maxmahem2mo ago
hmm... did I missread?
Aaron
Aaron2mo ago
where do you see them doing that
maxmahem
maxmahem2mo ago
sorry I missread this line: nestedDictionary[system] = mediaSources;
rmg66
rmg662mo ago
so in the new string "mastersystem":{"ImageSource":"Screenshot" is wrong but it's correct in the dictionary should be Title, not ScreenShot seems weird.... I dunno, I'm no expert lol maybe I should try newtonsoft.json and see if the behavior continues or not... maybe it's a cache issue of some sorts? i wanted to avoid as much third party in my program as I could
Aaron
Aaron2mo ago
no, there's just more going on in your code than you sent, I think
rmg66
rmg662mo ago
or the serialization is using the wrong reference, but I don't see that in my code
Aaron
Aaron2mo ago
wait, no it doesnt it's correct up at the top
Aaron
Aaron2mo ago
No description
Aaron
Aaron2mo ago
underlined line you just have several different mastersystem's in the json
rmg66
rmg662mo ago
yes, each is under a different scraper so the json is correct after all so easy to get confused with nesting my issue must be then how I am retrieving it later
maxmahem
maxmahem2mo ago
you might find it helpful to create some simple classes to help manage this. anyways, this code adds the mediaSource like you originally asked (not really changed from the original). https://dotnetfiddle.net/mTM9Kj
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
maxmahem
maxmahem2mo ago
also the [] syntax to create empty dictionaries (even nested ones)
rmg66
rmg662mo ago
thank you.. taking a break to rest my brain I finally found the issue in my code, an index for a combobox was not being created correctly i used linq to fix it... love linq
Want results from more Discord servers?
Add your server