jsonwrap 1.0.5
Just a simple wrap over dlang std.json
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
jsonwrap
Just a wrap over dlang std.json.
Setup
Add jsonwrap to your dub project:
## Quickstart
jsonwrap just adds some useful methods to ```JSONValue``` from ```std.json```.
import jsonwrap;
void main() { import std.exception :assertThrown;
// This works with CTFE, too. auto j = JSOB(
"field1", "value1",
"field2", JSOB(
"subfield1", "value2",
"subfield2", 3,
"subfield3", [1,2,3],
),
"field3", JSAB("mixed", 10, "array", JSOB("obj", 15))
);
// Or
// auto j = parseJSON({"field" : "value1"}
);
// Read will throw on error assert(j.read!string("/field2/subfield1") == "value2"); assert(j.read!int("/field3/1") == 10); assert(j.read!int("/field3/3/obj") == 15); assertThrown(j.read!string("/field2/subfield2")); // Wrong type
// Safe return default value on error assert(j.safe!string("/field2/subfield2") == string.init); // subfield2 is a int, wrong type. assert(j.safe!string("/field2/wrong/path") == string.init); assert(j.safe!string("/field2/wrong/path", "default") == "default");
// Like safe, but it tries to convert assert(j.as!string("/field2/subfield1"), "value2"); assert(j.as!string("/field2/subfield2"), "3");
// Check if a key exists assert(j.exists("/field2/subfield1") == true); assert(j.exists("/field3/test") == false);
// Remove a key assert(j.exists("/field2/subfield2") == true); j.remove("/field2/subfield2"); assert(j.exists("/field2/subfield2") == false);
// Add a new value, recreating the whole tree j.put("hello/world/so/deep", "yay!"); assert(j.exists("hello/world/so/deep") == true);
}
## One more thing
import std.json; import jsonwrap;
// std.json way to parse json JSONValue json = parseJSON(` { "user" : "foo", "address" : {
"city" : "venice",
"country" : "italy"
}, "tags" : ["hello" , 3 , {"key" : "value"}] } `);
{ // Read a string, user is a SafeValue!string auto user = json.safe!string("user"); assert(user.ok == true); assert(user.exists == true);
// This field doesn't exists on json // I can set a default value auto notfound = json.safe!string("blah", "my default value"); assert(notfound.ok == false); assert(notfound.exists == false); assert(notfound == "my default value");
// This field exists but it's not an int, it's a string auto wrong = json.safe!int("user"); assert(wrong.ok == false); assert(wrong.exists == true); assert(wrong == int.init); }
Documentation available [here](https://jsonwrap.dpldocs.info/jsonwrap.html)
- 1.0.5 released a year ago
- 2night/jsonwrap
- MIT License
- Copyright © 2015-2016, 2night SpA
- Authors:
- Dependencies:
- none
- Versions:
-
1.0.5 2023-Mar-14 1.0.4 2023-Mar-14 1.0.3 2022-Oct-20 1.0.2 2019-Jul-02 1.0.1 2017-Oct-19 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
1 downloads this month
-
643 downloads total
-
- Score:
- 1.3
- Short URL:
- jsonwrap.dub.pm