1

I am attempting to learn how to use JSON Serialization to save an object to disk, but I am running into an issue when the object I want to serialize has other objects within it.

If use the following:

string output = JsonConvert.SerializeObject(CharacterRepository);

The CharacterRepository will be serialized, the List of CharacterRecords will be serialized, but the CharacterINI data inside the CharacterRecord will be invalid. When I used BitFormatter this was not an issue, so I am hoping someone would help me understand the proper way to use JSON Serialization to save my object.

My current (working) code below that uses BinaryFormatter...

CharacterINI class

[Serializable]
internal class CharacterINI
{
    internal readonly string FilePath = string.Empty;
    internal Dictionary<string, Dictionary<string, string>> RAWDATA { get; private set; } = new();
    internal Dictionary<string, string> Panels...
    internal Dictionary<string, string> Macros...
    internal Dictionary<string, string> Chat...
    internal Dictionary<string, string> NameOptions...
    internal Dictionary<string, string> QuickBinds...
    internal Dictionary<string, string> ToolTips...

    public CharacterINI(string filepath)...
    [...]
}

CharacterRecord class

[Serializable]
internal class CharacterRecord
{
    public string? Name...
    public string? Realm...
    public string? Class...
    public string? Description...
    public CharacterINI? characterINI...
    public string? Server...
    public int Index...
}

RecordRepository class

[Serializable]
internal class RecordRepository
{
    public List<CharacterRecord>? Characters...
    public int Count => Characters?.Count??-1;
    public bool Contains(string name) => Characters?.Where(x => x.Name == name).Count() > 0;
}

This is where I perform the serialization and save the object to disk

{
    CharacterINI characterIniData = new CharacterINI(characterPath);
    [...]
    var characterRecord = new CharacterRecord
    {
        Name = BackUpNameComboBox.Text,
        Realm = BackUpRealmComboBox.Text,
        Class = BackUpClassComboBox.Text,
        Description = BackUpDescriptionTextBox.Text,
        characterINI = characterIniData,
        Server = BackUpServerTextBox.Text,
        Index = (recordCount == 0) ? 0 : recordCount
    };
    
    CharacterRepository.Characters.Add(characterRecord); //CharacterRepository is type RecordRepository
    
    using (Stream stream = File.Open(RespositryFullPath, FileMode.Create))
    {
        var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        binaryFormatter.Serialize(stream, CharacterRepository);
    }
    [...]
}
Taldren
  • 11
  • 2
  • Does this answer your question? [How do I turn a C# object into a JSON string in .NET?](https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net) – Charlieface May 15 '23 at 15:08
  • Use JSON.Net, or System.Text.Json, or many other serializers. `BinaryFormatter` is the worng tool, and is in any case basically deprecated – Charlieface May 15 '23 at 15:09
  • @Charlieface I understand that, that is the very reason I am attempting to adapt my code, but when I serialize to JSON i am losing data. The CharacterINI data, specifically, is being serialized to '{}' and lost. – Taldren May 15 '23 at 15:31
  • Use `public` properties not `internal` – Charlieface May 15 '23 at 15:38
  • @Charlieface **If** those even _are_ properties. That's hidden behind "..." – Fildor May 15 '23 at 15:45
  • Don't you get an error about the `CharacterINI` type being less accessable than the property `characterINI` which is public? I would kind of expect that. – Fildor May 15 '23 at 15:46
  • 1
    @Fildor Unexpectedly, no. And looking at it, I really don't know why I didn't. – Taldren May 15 '23 at 16:58
  • @Charlieface Oh wow, that is exactly what it was. Once I set them to public the data is now serialized with the rest. Not sure why I didn't get a 'less accessible' error or how BinaryFormatter succeeded in functioning. Thank you for the help. – Taldren May 15 '23 at 17:01
  • 1
    @Fildor Possibly relevant dupe https://stackoverflow.com/questions/27509989/json-net-explicitly-include-a-single-private-property – Charlieface May 15 '23 at 19:41

0 Answers0