On Mon, 22 Jan 2018 23:47:28 GMT, Taylor Gronka wrote:

(...)

What's a clean (and simple) way to deserialize this object? I was hoping to do this, but it fails:

struct MyResponse {
   string p;
   int n;
   Json animal;
}

auto res = deserializeJson!MyResponse(stringFromCurl);

This kind of nesting is generally supported and, if the kind of animal is not known upfront, this is what I'd usually also do. What does the error message say?

I also tried a template, but I've had trouble getting the logic to flow. In one method I keep getting stuck with the compile error Error: variable r cannot be read at compile time

struct MyResponse(T) {
   string p;
   int n;
   T animal;
}

struct Cat {
   string name;
   int age;
}

auto res = deserializeJson!(MyResponse!Cat(stringFromCurl));

I this case the parenthesis are just a bit off:

auto res = deserializeJson!(MyResponse!Cat)(stringFromCurl);

should work as long as the exact type is known upfront. BTW, this could also be written like this to avoid the chained parenthesis and to make it slightly nicer to read: stringFromCurl.deserializeJson!(MyResponse!Cat)