www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20385] New: Add opt* methods to std.json

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

          Issue ID: 20385
           Summary: Add opt* methods to std.json
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: andre s-e-a-p.de

Working with optional attributes in JSON is quite inconvenient to write:

Stream stream = {
    appId : js["appId"].str,
    // like this
    appVersion : js.object.get("appVersion", JSONValue("")).str,
    // or that
    startTimestamp : ("startTimestamp" in js is null) ? "" :
js["startTimestamp"].str,
};


Adding opt* methods like optStr(string key, defaultValue = string.init) would
make the usage a lot more readable:

Stream stream = {
    appId : js["appId"].str,
    appVersion : js.optStr("appVersion", "1.0"),
    startTimestamp : js.optStr("startTimestamp")
};

--
Nov 11 2019