www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem when calling toJSON()

reply Alex Herrmann <alexandermherrmann gmail.com> writes:
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 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?
Jan 31 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 01/31/2016 02:52 PM, Alex Herrmann wrote:

 Note that I am running 2.070.0.
Then it's probably a bug related to this change: http://dlang.org/changelog/2.070.0.html#json-encode-control-characters Ali
Jan 31 2016
parent reply Alex Herrmann <alexandermherrmann gmail.com> writes:
On Monday, 1 February 2016 at 00:24:06 UTC, Ali Çehreli wrote:
 On 01/31/2016 02:52 PM, Alex Herrmann wrote:

 Note that I am running 2.070.0.
Then it's probably a bug related to this change: http://dlang.org/changelog/2.070.0.html#json-encode-control-characters Ali
This problem is solved! Sorry for not updating the question. It was actually a problem with me not iduping it, and the memory being reclaimed or something similar. I did also change to stdx.data.json (std_data_json on dub).
Jan 31 2016
parent anonymous <anonymous example.com> writes:
On 01.02.2016 01:29, Alex Herrmann wrote:
 This problem is solved! Sorry for not updating the question.

 It was actually a problem with me not iduping it, and the memory being
 reclaimed or something similar. I did also change to stdx.data.json
 (std_data_json on dub).
You don't seem to be clear on what exactly went wrong. So here's an explanation: On 31.01.2016 23:52, Alex Herrmann wrote:
      auto outmd5 = JSONValue(cast(string) toHexString(md5_hash));
That cast is bad. You're casting a fixed-size char array to string, effectively slicing it. The data will get corrupted when the fixed-size array goes out of scope. Which happens immediately here, as you don't store the result of the toHexString call anywhere. As you mentioned, .idup instead of cast is the proper way to do this. std.conv.to!string is another option. Generally, casts should be avoided. It's easy to mess up with them in subtle ways.
Jan 31 2016