Strange behaviour of to!string and JSON

Sönke Ludwig via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 3 02:17:17 PST 2015


Am 03.12.2015 um 09:46 schrieb Suliman:
> void login(HTTPServerRequest req, HTTPServerResponse res)
> {
>   Json request = req.json;
>   writeln(to!string(request["username"]));
>   writeln(request["username"].to!string);
> }
>
> Why first code print output with quotes, and second not?
> "asd"
> asd

The first one calls std.conv.to, which in turn calls Json.toString(), 
while the second calls Json.to!string(), which converts the value that 
is stored in the Json object to a string.

Probably the "to" method needs to be renamed to something else 
("convertTo"?). But isn't there a way to customize std.conv.to!T for T 
other than string? I guess that was my hope when I named it like that 
four years ago.


More information about the Digitalmars-d mailing list