C
C#2y ago
Pdawg

❔ Adding logic into a JSON serializer

Hey everyone! I need to add logic into a json object, but I can't add any code in there or else it'll break. Here is what I mean: If I have a Dictionary named Icons, I need to say foreach item selected from the filepicker, create a new keypair in the Dictionary. Basically, the user selects a few .png files, the code needs to copy them to a folder and add those names to the dictionary. Here is my current code that is a static dictionary:
private async void SerializeJSON()
{
var model = new ObjectModel
{
ManifestVersion = 1,
Name = TileTitle,
Description = DescriptionTile,
Version = 1,
VersionString = "1",
Author = AuthorTile,
Organization = OrgTile,
ContactEmail = TileEmail,
TileIcon = new Dictionary<int, string>
{
[46] = "icons/tileIcon.png"
},
BadgeIcon = new Dictionary<int, string>
{
[24] = "icons/badgeIcon.png"
},
Icons = new Dictionary<string, string>
{
["iconTest"] = "newIcon1.png" // I need to add more items here when needed, based on what is pulled from the filepicker
},
};
}
private async void SerializeJSON()
{
var model = new ObjectModel
{
ManifestVersion = 1,
Name = TileTitle,
Description = DescriptionTile,
Version = 1,
VersionString = "1",
Author = AuthorTile,
Organization = OrgTile,
ContactEmail = TileEmail,
TileIcon = new Dictionary<int, string>
{
[46] = "icons/tileIcon.png"
},
BadgeIcon = new Dictionary<int, string>
{
[24] = "icons/badgeIcon.png"
},
Icons = new Dictionary<string, string>
{
["iconTest"] = "newIcon1.png" // I need to add more items here when needed, based on what is pulled from the filepicker
},
};
}
5 Replies
sunder.cc
sunder.cc2y ago
you could create the Dictionary before that and use that dictionary for the Icons or you can add a loop after assigning the Dictionary and add items to
model.Icons
model.Icons
Pdawg
PdawgOP2y ago
Oh okay, so then just assign the icons object to the dictionary? But create the dictionary and add content pre serialization
sunder.cc
sunder.cc2y ago
Yes Adding content after it being serialized is quite hard Youll have to deserialize it, add it and then serialize it again So if you can do it before serializing it would be the best
Pdawg
PdawgOP2y ago
Thanks! Will try it out. @Sunder Thanks so much! Works flawlessly 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server