Problem when calling toJSON()

Alex Herrmann via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 31 14:52:55 PST 2016


Hello all,
   I'm having a *really* weird problem (at least I think so). I 
have a function on a struct that converts it into JSON. 
Everything works, no big deal. But when I try to turn that 
JSONValue into a string, I get a UTF-8 Error. What makes it even 
weirder, is that if I call toJSON before I return it in my 
conversion function everything works.

Full GIST example is here: 
https://gist.github.com/alexmherrmann/f0441404497d5be4ba58

Here is my toJson function:

JSONValue fdToJson() {
     JSONValue[string] obj;

     auto outtime = JSONValue(modtime.date.toISOExtString());
     obj["modtime"] = outtime;

     auto fname = JSONValue(filename);
     obj["filename"] = fname;

     auto outmd5 = JSONValue(cast(string) toHexString(md5_hash));
     obj["md5"] = outmd5;

     auto jav = JSONValue(obj);

     err("tojson created:\n%s".format(jav.toPrettyString()));
     return jav;
   }

This works perfectly, the string is well formatted and nothing is 
wrong:

tojson created:
{
     "filename": "test.txt",
     "md5": "CF5B097A991BEEA7D5E2D1818DF1AADF",
     "modtime": "2016-01-31"
}

however, when I do this:

auto a = fd.fdToJson();
writeln(a.toPrettyString());

I get this error: 
core.exception.UnicodeException at src/rt/util/utf.d(290): invalid 
UTF-8 sequence

Note that I am running 2.070.0.

Is this a bug or am I doing something wrong?


More information about the Digitalmars-d-learn mailing list