View mode: basic / threaded / horizontal-split · Log in · Help
January 02, 2013
std.conv.parse too finicky?
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@.\..\..\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?
January 02, 2013
Re: std.conv.parse too finicky?
Chris:

> 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?

That parser is meant to be used to de-serialize simple D data 
structures printed (serialized) with writeln.

Bye,
bearophile
January 02, 2013
Re: std.conv.parse too finicky?
On Wednesday, 2 January 2013 at 17:06:50 UTC, bearophile wrote:
> Chris:
>
>> 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?
>
> That parser is meant to be used to de-serialize simple D data 
> structures printed (serialized) with writeln.
>
> Bye,
> bearophile

I see. Then it makes perfect sense. It would be nice, though, if 
it were more tolerant so that it could cater for other uses 
(human readable property files etc etc).
January 02, 2013
Re: std.conv.parse too finicky?
On Wednesday, 2 January 2013 at 17:02:48 UTC, Chris wrote:
> 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);
> 	}
> }

You could reduce burden by using raw strings:

auto asso = `["key1":"value1", "key2":"value2"]`;

> Or is there something I have overlooked?

What bearophile said.
January 02, 2013
Re: std.conv.parse too finicky?
On Wednesday, 2 January 2013 at 17:22:57 UTC, monarch_dodra wrote:
> On Wednesday, 2 January 2013 at 17:02:48 UTC, Chris wrote:
>
> You could reduce burden by using raw strings:
>
> auto asso = `["key1":"value1", "key2":"value2"]`;
>
Yes, I just thought it might be handy for reading user defined 
files like localization files etc, like this one (without quote).
[
Close:Cerrar,
Open:Abrir,
...
]

Sure, I can write my own parser as usual :-)
January 05, 2013
Re: std.conv.parse too finicky?
On 2013-01-02 19:23, Chris wrote:
> Yes, I just thought it might be handy for reading user defined files like
> localization files etc, like this one (without quote).
> [
> Close:Cerrar,
> Open:Abrir,
> ...
> ]

But then you would probably also want support for comments. ;)
January 05, 2013
Re: std.conv.parse too finicky?
On Saturday, 5 January 2013 at 13:10:29 UTC, FG wrote:
> On 2013-01-02 19:23, Chris wrote:
>> Yes, I just thought it might be handy for reading user defined 
>> files like
>> localization files etc, like this one (without quote).
>> [
>> Close:Cerrar,
>> Open:Abrir,
>> ...
>> ]
>
> But then you would probably also want support for comments. ;)

Yes, probably, but it depends an what you wanna do with it. If 
it's something like a Java properties file that loads user 
defined preferences or stuff like that, you don't need comments. 
Then again, if the program generates the files itself, the 
additional quotes wouldn't be a problem. I just thought it would 
be a nice-to-have feature that you can define the type, e.g. 
string[string], and the method parses accordingly, i.e. left 
value is string : right value is string. But hey, no big deal if 
it doesn't.
Top | Discussion index | About this forum | D home