0

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.

nelion
  • 1,712
  • 4
  • 17
  • 37
  • Is anything special in your json that you used `Hjson` instead on `json` only? – er-sho Mar 27 '19 at 10:21
  • Nothing but the ability to comment the json. That shouldn't be the problem though, since it loads the file correctly. – nelion Mar 27 '19 at 10:55
  • could you please show the value in `loadedValue`? – er-sho Mar 27 '19 at 11:03
  • Yes, question is updated with the value. – nelion Mar 27 '19 at 11:13
  • It just converts from hjson to json - removing the comments. – nelion Mar 27 '19 at 11:59
  • so do you want your json can be deserialized with `newtonsoft` right? – er-sho Mar 27 '19 at 12:10
  • Yes, well I want to be able to use the elements "SecondConfig" and "ThirdConfig", which are both null even though they obviously have a value in json. – nelion Mar 27 '19 at 12:12
  • So how much those elements? its only 2 that you said above or may be undetermined. – er-sho Mar 27 '19 at 12:17
  • just 2, The rest of the json/hjson has no value for this method. So only SecondConfig and ThirdConfig.. – nelion Mar 27 '19 at 12:33
  • Try these classes => `public class SecondConfig { public string ConnectionString { get; set; } public string SpecificationPseudo { get; set; } public int NumberOfHintsPseudo { get; set; } } public class TestConfiguration { public SecondConfig SecondConfig { get; set; } public string ThirdConfig { get; set; } } public class RootObject { public TestConfiguration TestConfiguration { get; set; } }` – er-sho Mar 27 '19 at 12:34
  • And deserialize it like => `return JsonConvert.DeserializeObject(loadedValue);` – er-sho Mar 27 '19 at 12:35
  • Ah! You lead me in the right direction! I removed the "TestConfig" from the hjson since it's root and the class that I am working with. This solved the problem and now it returns the correct values. Thanks a lot. – nelion Mar 27 '19 at 13:02
  • glad to hear :) – er-sho Mar 27 '19 at 13:04

1 Answers1

1

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

nelion
  • 1,712
  • 4
  • 17
  • 37