Lets say I have a model like:
public class MyModel
{
public string Name { get; set; }
public string[] Size { get; set; }
public string Weight { get; set; }
}
And Json like this:
{
"name" : "widget",
"details" : {
"size" : [
"XL","M","S",
]
"weight" : "heavy"
}
}
I have been trying to work out a way to serialize my object without making one model for the "name" and one model for the "details" as this doesn't map nicely to my database so involves a little juggling to get class populated.
I can make multiple passes at JsonConvert.PopulateObject() like:
var mod = new MyModel();
JsonConvert.PopulateObject(json.ToString(), mod);
JsonConvert.PopulateObject(json["details"].ToString(), mod);
But in my real code I am running multiple threads and PopulateObject is not thread safe, it jams the app. The comments for PopulateJsonAsync() say not to use it but instead to use Task.Run() on PopulateObject().
This does not work and still locks the app when I call it like:
await Task.Run(() => JsonConvert.PopulateObject(response.ToString(), productDetail));
if (response["results"].HasValues)
{
await Task.Run(() => JsonConvert.PopulateObject(response["results"][0].ToString(), productDetail));
}
A few get through but eventually the app gets completely thread locked. If I remove PopulateObject the threads all terminate fine so I am pretty sure this function is not thread safe.
Is there a neat threadsafe approach to populating my object in a single step?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…