This repository was archived by the owner on Aug 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnlohmann_json.rb
More file actions
101 lines (84 loc) · 2.69 KB
/
nlohmann_json.rb
File metadata and controls
101 lines (84 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class NlohmannJson < Formula
desc "JSON for Modern C++"
homepage "https://github.com/nlohmann/json"
url "https://github.com/nlohmann/json/archive/v3.10.0.tar.gz"
sha256 "eb8b07806efa5f95b349766ccc7a8ec2348f3b2ee9975ad879259a371aea8084"
head "https://github.com/nlohmann/json.git", :branch => "develop"
depends_on "cmake" => [:optional]
deprecate! date: "2021-08-19", because: "formula is now part of core; use 'nlohmann-json'"
def install
if build.with? "cmake"
mkdir "build" do
system "cmake", "..", "-DJSON_BuildTests=OFF", *std_cmake_args
system "make", "install"
end
else
include.install "single_include/nlohmann"
end
end
def caveats
<<~EOS
If built with CMake support, you can use find_package to use the library.
Without it, please set your include path accordingly:
CPPFLAGS: -I#{include}
EOS
end
test do
(testpath/"test.cpp").write <<~EOS
#include <nlohmann/json.hpp>
using nlohmann::json;
int main() {
// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add an object inside the object
j["answer"]["everything"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };
// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
// a user-defined literal
json j3 = R"(
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
)"_json;
}
EOS
system ENV.cxx, "test.cpp", "-I#{include}", "-std=c++11", "-o", "test"
system "./test"
end
end