www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12969] New: std.json: Lack of opIndexAssign operator for

https://issues.dlang.org/show_bug.cgi?id=12969

          Issue ID: 12969
           Summary: std.json: Lack of opIndexAssign operator for JSONValue
                    may become a source of runtime errors
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: neuranuz gmail.com

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.

--
Jun 23 2014