0

I've checked, and the collection I'm inputting into the serializer does not have null values, however I get an empty object when I try to serialize it. This only happens when I try to serialize collections of objects, not the individual objects themselves.

Output:

[
  [],
  []
]

Code:

public class CustomCommands : List<CustomCommands>
    {
        [JsonProperty]
        public string CommandName { get; set; }
        [JsonProperty]
        public string CommandResponse { get; set; }
        [JsonProperty]
        public bool IsModCommand { get; set; }
     
        public static void SaveCommands(List<CustomCommands> CommandsList, string FileName)
        {
            string JsonString = JsonConvert.SerializeObject(CommandsList, Newtonsoft.Json.Formatting.Indented);
            File.WriteAllText(FileName, JsonString);
        }
        public static List<CustomCommands> LoadCommands(string FileName)
        {
            string JsonString = File.ReadAllText(FileName);
            return JsonConvert.DeserializeObject<CustomCommands>(JsonString);
        }
    }
DNVIC
  • 11
  • 2
  • 9
    Please provide a [mcve]. It's hard to help you without more information. (The fact that your `CustomCommands` class *derives from* `List` may well be part of the problem though - I would strongly encourage you to redesign to avoid that. Can it not just be a single `CustomCommand`, and then you'd have a `List`?) – Jon Skeet Jul 30 '20 at 20:28
  • In JSON, an array cannot also be an object at the same time. – TimTIM Wong Jul 30 '20 at 20:37
  • @JonSkeet The question is not edited, and already provide a reproducible example. I can run it in LINQPad by just adding 2 `CustomCommands` and invoke `SaveCommands`. – TimTIM Wong Jul 30 '20 at 20:50
  • @TimTimWong: It's not a complete example. I can't copy/paste/compile/run. We shouldn't have to guess at things in order to reproduce the problem, IMO - the questioner should put effort into making it as *easy as possible* to reproduce it. – Jon Skeet Jul 30 '20 at 21:00
  • 1
    (As an aside, I'd also strongly suggest learning about and following .NET naming conventions - in particular using `camelCase` for parameters and local variables.) – Jon Skeet Jul 30 '20 at 21:01
  • See also: [How do I get json.net to serialize members of a class deriving from List?](https://stackoverflow.com/q/21265629/10263) – Brian Rogers Jul 31 '20 at 03:01

1 Answers1

1

Do not implement List<CustomCommands> on CustomCommands, but deserialize List<> instead.

public class CustomCommands // do not : List<CustomCommands>
{
    [JsonProperty]
    public string CommandName { get; set; }
    [JsonProperty]
    public string CommandResponse { get; set; }
    [JsonProperty]
    public bool IsModCommand { get; set; }
 
    public static void SaveCommands(List<CustomCommands> commandsList, string fileName)
    {
        string jsonString = JsonConvert.SerializeObject(commandsList, Newtonsoft.Json.Formatting.Indented);
        File.WriteAllText(fileName, jsonString);
    }
    public static List<CustomCommands> LoadCommands(string fileName)
    {
        string jsonString = File.ReadAllText(fileName);
        return JsonConvert.DeserializeObject<List<CustomCommands>>(jsonString); // do List<>
    }
}
TimTIM Wong
  • 788
  • 5
  • 16