@@ -53,23 +53,26 @@ preserved.
53
53
Json::Value root; // 'root' will contain the root value after parsing.
54
54
std::cin >> root;
55
55
56
+ // You can also read into a particular sub-value.
57
+ std::cin >> root["subtree"];
58
+
56
59
// Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
57
60
// such member.
58
61
std::string encoding = root.get("encoding", "UTF-8" ).asString();
59
- // Get the value of the member of root named 'encoding', return a 'null' value if
62
+ // Get the value of the member of root named 'encoding'; return a 'null' value if
60
63
// there is no such member.
61
64
const Json::Value plugins = root["plug-ins"];
62
65
for ( int index = 0; index < plugins.size(); ++index ) // Iterates over the sequence elements.
63
66
loadPlugIn( plugins[index].asString() );
64
67
65
- setIndentLength( root["indent"].get("length", 3).asInt() );
66
- setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
68
+ foo:: setIndentLength( root["indent"].get("length", 3).asInt() );
69
+ foo:: setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
67
70
68
71
// Since Json::Value has implicit constructor for all value types, it is not
69
72
// necessary to explicitly construct the Json::Value object:
70
- root["encoding"] = getCurrentEncoding();
71
- root["indent"]["length"] = getCurrentIndentLength();
72
- root["indent"]["use_space"] = getCurrentIndentUseSpace();
73
+ root["encoding"] = foo:: getCurrentEncoding();
74
+ root["indent"]["length"] = foo:: getCurrentIndentLength();
75
+ root["indent"]["use_space"] = foo:: getCurrentIndentUseSpace();
73
76
74
77
// If you like the defaults, you can insert directly into a stream.
75
78
std::cout << root;
@@ -80,27 +83,24 @@ std::cout << std::endl;
80
83
\endcode
81
84
82
85
\section _advanced Advanced usage
83
- We are finalizing the new *Builder* API, which will be in versions
84
- `1.4.0` and `0.8.0` when released. Until then, you may continue to
85
- use the old API, include `Writer`, `Reader`, and `Feature`.
86
- \code
87
86
88
- // EXPERIMENTAL
89
- // Or use `writeString()` for convenience, with a specialized builder.
90
- Json::StreamWriterBuilder wbuilder;
91
- builder.indentation_ = "\t";
92
- std::string document = Json::writeString(root, wbuilder);
87
+ Configure *builders* to create *readers* and *writers*. For
88
+ configuration, we use our own `Json::Value` (rather than
89
+ standard setters/getters) so that we can add
90
+ features without losing binary-compatibility.
93
91
94
- // You can also read into a particular sub-value.
95
- std::cin >> root["subtree"];
92
+ \code
93
+ // For convenience, use `writeString()` with a specialized builder.
94
+ Json::StreamWriterBuilder wbuilder;
95
+ wbuilder.settings["indentation"] = "\t";
96
+ std::string document = Json::writeString(wbuilder, root);
96
97
97
- // EXPERIMENTAL
98
- // Here we use a specialized Builder, discard comments, and
99
- // record errors.
98
+ // Here, using a specialized Builder, we discard comments and
99
+ // record errors as we parse.
100
100
Json::CharReaderBuilder rbuilder;
101
- rbuilder.collectComments_ = false;
101
+ rbuilder.settings["collectComments"] = false;
102
102
std::string errs;
103
- Json::parseFromStream(rbuilder, std::cin, &root["subtree"] , &errs);
103
+ bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs);
104
104
\endcode
105
105
106
106
\section _pbuild Build instructions
0 commit comments