Search code examples
c#jsonserializationapp-confighjson

C# Hjson returns null after deserialization


I have a hjson file like this that I want to deserialize and work with:

{
"TestConfig": {
  "SecondConfig": {
    "ConnectionString": "Integrated Security = true; Data Source = dataPseudo; Initial Catalog = catalogPseudo; Connect Timeout = 180",
    "SpecificationPseudo": "pseudo",
    "NumberOfHintsPseudo": 300
  },

  "ThirdConfig": "pseudo"
}... // more hjson coming here.

I load it with the HjsonValue.Load method like this:

private static Foo convertJson()
{
var loadedValue = HjsonValue.Load("hjsonFile.hjson").ToString();
return new JsonSerializer<Foo>().DeserializeFromString(loadedValue);
// another failed method: return JsonConvert.DeserializeObject<Foo>(loadedValue);
// A third failed method: return JsonConvert.DeserializeObject<Dictionary<string, Foo>>(loadedValue);
}

I think my problem is in the 2 c#-coded lines, but can't figure what. Am I deserializing wrong or what seems to be the problem? I suspect that it's because it is a nested json, but can't find a way to deserialize it. Trying to use dictionary as it is a answer in a other stack-question, but it didn't work for me.

Note: The first and second tried return method don't return any errors, but they just return a nullreferenceexception since "SecondConfig" and "ThirdConfig" both are null..

Update (with help from er-sho): removed the "root"-element from the hjson (TestConfig), which solved the problem.


Solution

  • Removing "TestConfig" from the hjson fixed it, since it's root and the class I am working with.