www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15410] New: std.json documentation implies you *must* assign

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

          Issue ID: 15410
           Summary: std.json documentation implies you *must* assign an AA
                    literal to create an object
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: dhasenan gmail.com

opIndex(string) says:

"Throws JSONException if type is not JSON_TYPE.OBJECT."

This implies the correct way to create a JSONValue of type OBJECT, assuming you
don't want to use an AA literal, is:

---
JSONValue j;
j.type = JSON_TYPE.OBJECT;
j["key"] = "value";
---

However, when you try that, the compiler warns:
Deprecation: function std.json.JSONValue.type is deprecated - Please assign the
value with the adequate type to JSONValue directly.

In my use case, it would be unnatural and troublesome to assign an AA literal
directly. So now I'm looking for a magic incantation that will maybe work. I
could try something like:

---
JSONValue j = ["": ""];
j.remove("");;
---

Except JSONValue, as far as the documentation states, is append-only. (Is it
really? Who knows?)

That leaves only one method to create an empty JSON object:

---
JSONValue j = "{}".parseJSON;
---

That's terrible.

However! The documentation is actually wrong! I can just write:

---
JSONValue j;
j["key"] = "value";
---

That works! The documentation told me it would throw an exception, so there was
no reason for me to try it. It was guaranteed to me that it would fail. But
instead it succeeded.

This is making me bitter and jaded.

--
Dec 05 2015