jsonwrap 1.0.1
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:
"dependencies":
{
"jsonwrap" : "*"
}
How to
It works using UCFS on standard JSONValue. Basic usage:
import std.json;
import jsonwrap;
// Standard way to parse json
JSONValue json = parseJSON(`{"hello":"world"}`);
// Read a value
string world = json.get!string("hello");
// Write a value
json.put("foo", "bar");
// Now json == {"hello":"world", "foo":"bar"}
More ways to read:
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"}]
}
`);
{
string user = json.get!string("user"); // Read a string from json
assert(user == "foo");
}
{
// Read a string, user is a SafeValue!string
auto user = json.get!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.get!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.get!int("user");
assert(wrong.ok == false);
assert(wrong.exists == true);
assert(wrong == int.init);
}
{
// I can read deep fields
assert(json.get!string("/address/city") == "venice");
// also inside an array
assert(json.get!string("/tags/2/key") == "value");
}
{
// Using as!T you can convert field
assert(json.as!string("/tags/1") == "3"); // On json "/tags/1" is an int.
}
// get!T and as!T are nothrow functions and they return a SafeValue!T.
// read!T throw an exception on error
{
import std.exception: assertThrown;
assert(json.read!string("/tags/2/key") == "value");
assertThrown(json.read!int("/blah/blah"));
}
More functions:
{
// You can check if a field exists or not
assert(json.exists("/address/country") == true);
// You can remove it
json.remove("/address/country");
// It doesn't exists anymore
assert(json.exists("/address/country") == false);
}
{
// You can write using put.
json.put("/address/country", "italy"); // Restore deleted field
json.put("/this/is/a/deep/value", 100); // It create the whole tree
json.put("/this/is/an/array/5", "hello"); // Ditto
assert(json.get!int("/this/is/a/deep/value") == 100);
assert(json.get!string("/this/is/an/array/5") == "hello"); // elements 0,1,2,3,4 are nulled
}
Object and Array builders (CTFE-compatible):
{
// A fast way to build object CTFE compatible.
// JSOB is an alias for JsonObjectBuilder and JSAB for JsonArrayBuilder
enum jv = JSOB
(
"array", [1,2,3],
"key", "value",
"mixed_array", JSAB(1, "hello", 3.0f),
"obj", JSOB("subkey", 3)
);
/* jv ==
{
"array":[1,2,3],
"key":"value",
"mixed_array":[1,"hello",3],
"obj": { "subkey" : 3 }
}
*/
}
- Registered by Andrea Fontana
- 1.0.1 released 7 years 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
-
1 downloads this week
-
1 downloads this month
-
630 downloads total
-
- Score:
- 1.1
- Short URL:
- jsonwrap.dub.pm