On Thursday, 24 July 2014 at 16:02:12 UTC, H. S. Teoh via Digitalmars-d-learn wrote:
On Thu, Jul 24, 2014 at 03:54:20PM +0000, Pavel via Digitalmars-d-learn wrote:
[...]
Guess what, here's a new snippet:

import std.stdio;
import std.json;

void main() {
  scope(failure) writeln("FaILED!!");
  string jsonStr = `{ "name": "1", "type": "r" }`;
  auto parsed = parseJSON(jsonStr).object;
  writeln("fail" in parsed);
}

Output is:
null

WAT?!

Ofcourse, writing like:

writeln(cast(bool)("fail" in parsed));

Produces "false"... but why on earth boolean expression would output null?

It's not a boolean expression. The 'in' operator returns a pointer.
Rationale: avoid double lookups, for example:

        if (auto ptr = "key" in assocArray) {
                doSomething(*ptr);
        }


T

Thanks once again. Now I finally get this thing done with code like this:

import std.stdio;
import std.json;

void main() {
  scope(failure) writeln("Failure!!");
  string jsonStr = `{ "name": "1", "type": "r" }`;
  auto parsed = parseJSON(jsonStr).object;
  auto found = "fail" in parsed;
  if (found !is null) {
    string s = found.str;
    writeln(s);
  } else {
    writeln("No such key");
  }
}

Still focus on wrapper class discussion :)

Reply via email to