Issue 20385 - Add opt* methods to std.json
Summary: Add opt* methods to std.json
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-11-11 20:17 UTC by Andre
Modified: 2024-12-01 16:36 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andre 2019-11-11 20:17:48 UTC
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")
};
Comment 1 Seb 2019-11-13 01:47:21 UTC
std.json (and most of Phobos is maintainance-only).

The planned next version of std.data.json is here:

https://github.com/dlang-community/std_data_json
Comment 2 dlangBugzillaToGithub 2024-12-01 16:36:01 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10399

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB