www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15331] New: std.json objects should implement opIn_r

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

          Issue ID: 15331
           Summary: std.json objects should implement opIn_r
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: json
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: dhasenan gmail.com

How do I determine whether a JSON object contains a value with a particular
key?

---
JSONValue v;
bool contained;
try {
  v = jsonObj["foo"];
  contained = true;
} catch (JSONException) {
}
if (contained) {
  // do stuff with v
}
---

Alternatively:
---
JSONValue v;
bool contained;
foreach (string k, v2; jsonObj) {
  if (k == "foo") {
    v = v2;
    contained = true;
    break;
  }
}
if (contained) {
  // do stuff with v
}
---

There should be a convenient method to check whether an object contains a key.

Obligatory usecase: I'm consuming a JSON API schema to auto-generate an API
client for AWS. The schema omits the "input" key if no input is necessary and
the "output" key if no output is emitted from the API method.

--
Nov 13 2015