[Issue 15331] New: std.json objects should implement opIn_r

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Nov 13 12:58:16 PST 2015


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 at puremagic.com
          Reporter: dhasenan at 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.

--


More information about the Digitalmars-d-bugs mailing list