[Issue 20385] New: Add opt* methods to std.json
    d-bugmail at puremagic.com 
    d-bugmail at puremagic.com
       
    Mon Nov 11 20:17:48 UTC 2019
    
    
  
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 at puremagic.com
          Reporter: andre at 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")
};
--
    
    
More information about the Digitalmars-d-bugs
mailing list