Vladimir Panteleev Wrote: > if (resource == "/getfoo") > { > struct FooResult { int n; string s; } > return toJSON(FooResult(42, "bar")); // {"n":42,"s":"bar"} > }
What kind of code did you use there? My app does something similar using wrappers of std.json. JSONValue toJsonValue(T)(T a) { JSONValue val; static if(is(T == JSONValue)) { val = a; } else static if(__traits(compiles, a.makeJsonValue())) { val = a.makeJsonValue(); } else static if(isIntegral!(T)) { val.type = JSON_TYPE.INTEGER; val.integer = to!long(a); [......] And it goes right through a variety of types, including structs where it does __traits(allMembers), and ultimately settles for to!string if nothing else fits. My program also reads json, but I had some trouble with std.json, so I had to fork it there. It has a helper function jsonValueToVariant (which just became fully usable in dmd 2.050, shortening my code a lot, thanks phobos/dmd devs!) which pulls it into a std.variant for easy using later. The trouble I had was std.json.parseJSON claims to be able to handle arbitrary input ranges, but when I actually instantiated it on plain old string, it refused to compile. I made it work by switching it to just normal strings in my private fork. What's really cool about these templates is it enables automatic calling of functions from the outside. You write a function like: struct User { ... } User getUserInfo(int id) { .... } And then this is accessible, through template magic, as: /app/get-user-info?id=1 Returns a full HTML document with the info /app/get-user-info?id=1&format=json Returns the User struct converted to json /app/get-user-info?id=1&format=xml The struct as a kind of xml (I didn't spend much time on this so it still sucks) And more. Way cool. Anyway, I thought about committing some of my json changes back to std.json, but removing the range capability goes against the grain there, and adding the templates seems pointless since everyone says std.json is going to be trashed anyway. I thought I might have actually been the only one using it! I'm curious what you did in your code. Is it a custom module or did you build off the std.json too?