Bug 12969 – std.json: Lack of opIndexAssign operator for JSONValue may become a source of runtime errors

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2014-06-23T14:11:00Z
Last change time
2015-02-18T03:39:32Z
Assigned to
nobody
Creator
neuranuz

Comments

Comment #0 by neuranuz — 2014-06-23T14:11:08Z
Intuitive way to assign value to object by key is to use associative-array-like syntax. Currently it compiles that intuitively promises that it should work but fails in runtime with assertion. This problem will be a source of bugs for programmers that don't know details of implementation of std.json module. Following code compiles in DMD 2.065 but fails in runtime. import std.stdio, std.json; void main() { string[string] aa = ["creature": "goblin", "name": "John Smith"]; JSONValue json = aa; json["age"] = 100; //These lines will compile json["color"] = "green"; //but fail in runtime writeln( toJSON(&json) ); } This code produces following error message: core.exception.AssertError@/opt/compilers/dmd2/include/std/json.d(279): null this ---------------- ./f499(_d_assert_msg+0x45) [0x42aefd] ./f499(pure nothrow @safe void std.json.JSONValue.opAssign!(int).opAssign(int)+0x49) [0x42827d] ./f499(_Dmain+0xd7) [0x427783] ./f499(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll().void __lambda1()+0x18) [0x42c6a8] ./f499(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x42c602] ./f499(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x30) [0x42c668] ./f499(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x42c602] ./f499(_d_run_main+0x1a3) [0x42c583] ./f499(main+0x17) [0x4283a3] I propose to add opIndexAssign operator for JSONValue to fix this problem. I don't know is it really a bug or enhancement, but it's important for my project. So I'll try to create my first pull request to Phobos library. I don't know why it has not been implemented already. May be some problems exist there. So I wait for comments.
Comment #1 by justin — 2014-07-11T18:31:00Z
Comment #2 by github-bugzilla — 2014-10-31T20:17:41Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/f2bc851d7c16bfc242dff4c680cf5a24f47cf020 std.json: Fixed issue 12969. opIndexAssign operator for JSONValue added. Changed order of elements in enum JSON_TYPE so NULL is a default type when JSONValue is not initialized Signed-off-by: Uranuz <[email protected]> https://github.com/D-Programming-Language/phobos/commit/7dfe2885763801a1d8339243cdc61b3caf9d0cdf Merge pull request #2607 from Dicebot/uranuz_issue_12969 Fix issue 12969 : opIndexAssign operator for JSONValue
Comment #3 by github-bugzilla — 2015-02-18T03:39:32Z
Commits pushed to 2.067 at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/f2bc851d7c16bfc242dff4c680cf5a24f47cf020 std.json: Fixed issue 12969. opIndexAssign operator for JSONValue added. Changed order of elements in enum JSON_TYPE so NULL is a default type when JSONValue is not initialized https://github.com/D-Programming-Language/phobos/commit/7dfe2885763801a1d8339243cdc61b3caf9d0cdf Merge pull request #2607 from Dicebot/uranuz_issue_12969