Closed NimaiMalle closed 5 years ago
That the attempt to serialize a JObject threw an exception was a bug that was fixed by commit ed16019. But serializing the JObject will produce the following content which surely is not what you expect
Type = "Object"
HasValues = true
First = [[]]
Last = [[]]
Count = 2
Root = [[[]], [[]]]
Path = ""
To get the expected content you have to do:
const string jsonString = @"{""MongoURL"":""localhost"",""ElasticsearchUrls"":""http://localhost:9200""}";
JObject configurationObject = (JObject)JsonConvert.DeserializeObject(jsonString);
var serMe = configurationObject.ToObject<Dictionary<string, object>>();
string contents = Toml.WriteString(serMe);
Same a little bit shorter:
const string jsonString = @"{""MongoURL"":""localhost"",""ElasticsearchUrls"":""http://localhost:9200""}";
var configurationObject = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
string contents = Toml.WriteString(configurationObject);
This two variants should also work with the currently released version of the lib that does not contain the JObject fix.
Thank you! Just what I needed. I had taken a look inside of TomlTable.RootTabl.cs
and was considering adding support for JObject much like IDictionary has a special case. So, something like the following:
if ((object)obj is IDictionary dict)
{
CreateFromDictionary();
}
else if ((object)obj is JObject)
{
CreateFromJObject(obj as JObject);
}
else
{
CreateFromCustomClrObject();
}
The create function would be almost identical to the IDictionary one. The approach you gave works just fine, though, so maybe not needed.
I don't think that a method like this is needed for the following reasons
Thank you for the explanation. Makes total sense.
I have the following code which, given a Json String, creates an object then attempts to get a Toml string from that object:
The value of
jsonString
happens to be{"MongoURL":"localhost","ElasticsearchUrls":"http://localhost:9200"}
The Exception is: Parameter count mismatch.
Maybe there's a better way to go from Json (or dynamic object) to Toml? I'll see about adding a test case for this, or let me know if what I'm doing is not the right approach.