std.conv.parse too finicky?

Chris wendlec at tcd.ie
Wed Jan 2 09:02:47 PST 2013


I was playing around with std.conv.parse's mechanism for parsing 
associative arrays from strings (cf. 
http://dlang.org/phobos/std_conv.html#parse). A handy feature as 
it would allow user-friendly input formats that can be 
transformed into a D-array. However, the parser is very finicky 
and expects the string to be exactly as if it were a hard-coded 
D-array:

void main(string[] args) {
	auto asso = "[\"key1\":\"value1\", \"key2\":\"value2\"]";
	auto array = parse!(string[string], string)(asso);
	foreach (k, v; array) {
		writefln("%s : %s", k, v);
	}
}

If you just write:

auto asso "[key1:value1, ...]";

i.e. withouth the quotes it says (dmd2.060):

std.conv.ConvException at .\..\..\src\phobos\std\conv.d(2973): Can't 
parse string:
""" is missing

Couldn't the parser infer from string[string] that the key:value 
pairs should be treated as strings, regardless of whether they 
are quoted or not? Having to use quotes is not really 
user-friendly, e.g. in a text file like this:
[
customer1 : Wellington Street,
customer2 : Mountain Road,
]

is easier to maintain than:

[
"customer1" : "Wellington Street",
"customer2" : "Mountain Road",
]

Or is there something I have overlooked?


More information about the Digitalmars-d mailing list