Bug 15884 – Assigning char[] to std.json.JSONValue creates array, not string
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
Mac OS X
Creation time
2016-04-06T10:56:37Z
Last change time
2020-03-21T03:56:42Z
Keywords
pull
Assigned to
No Owner
Creator
Lionello Lunesu
Comments
Comment #0 by lio+bugzilla — 2016-04-06T10:56:37Z
Repro:
void main(){
import std.json;
JSONValue v = "asdf".dup;
assert(v.type == JSON_TYPE.STRING);//fails
assert(v.toString == `"asdf"`);//also fails
}
Expected: the JSONValue should be created as a string
Actual: the JSONValue is an array and gets encodes as [97,115,100,102]
Comment #1 by lio+bugzilla — 2016-04-06T11:15:09Z
Ugh, I understand why it's the way it is.
Should we do .idup in JSONValue? Either way there's going to be an allocation, either for the JSONValue[] or for the string.
The repro might seem convoluted, but I ran into it like this: a method returns a char[], in order to give the caller full r/w control, which is what such a "toString" function is supposed to do. Using this return value (which thanks to "auto" is not obvious) in a JSONValue results in a less than ideal JSON.
Comment #2 by b2.temp — 2016-04-09T07:51:41Z
(In reply to Lionello Lunesu from comment #1)
> Ugh, I understand why it's the way it is.
>
> Should we do .idup in JSONValue? Either way there's going to be an
> allocation, either for the JSONValue[] or for the string.
>
> The repro might seem convoluted, but I ran into it like this: a method
> returns a char[], in order to give the caller full r/w control, which is
> what such a "toString" function is supposed to do. Using this return value
> (which thanks to "auto" is not obvious) in a JSONValue results in a less
> than ideal JSON.
I have tested a fix and calling idup doesn't break current tests, actually the method that's involved to make it accepts char[] is a template and with attribute inference there is no problem. It's always pure nothrow @safe, safe is a new requirement on the master version of std.json.
Comment #3 by github-bugzilla — 2016-04-11T12:39:55Z
Ah, you're fast, but what about wchar[] dchar[]? I suggest we use std.utf.toUTF8() to handle all the non-string cases (it does a "idup" for char[], by the way.)