blob: f340591fd76b8d1684998a4640f3c43b2875332d [file] [log] [blame]
Christopher Dunne0d72242007-06-14 17:58:591/**
2\mainpage
3\section _intro Introduction
4
5<a HREF="http://www.json.org/">JSON (JavaScript Object Notation)</a>
Jordan Bayles7b286982019-08-14 05:41:436 is a lightweight data-interchange format.
Christopher Dunne0d72242007-06-14 17:58:597
8Here is an example of JSON data:
9\verbatim
Christopher Dunn948f2902015-01-25 18:19:3010{
11 "encoding" : "UTF-8",
12 "plug-ins" : [
13 "python",
14 "c++",
15 "ruby"
16 ],
17 "indent" : { "length" : 3, "use_space": true }
18}
19\endverbatim
20<b>JsonCpp</b> supports comments as <i>meta-data</i>:
21\code
Christopher Dunne0d72242007-06-14 17:58:5922// Configuration options
23{
24 // Default encoding for text
25 "encoding" : "UTF-8",
Jordan Bayles7b286982019-08-14 05:41:4326
Christopher Dunne0d72242007-06-14 17:58:5927 // Plug-ins loaded at start-up
28 "plug-ins" : [
29 "python",
Christopher Dunn948f2902015-01-25 18:19:3030 "c++", // trailing comment
Christopher Dunne0d72242007-06-14 17:58:5931 "ruby"
32 ],
Jordan Bayles7b286982019-08-14 05:41:4333
Christopher Dunne0d72242007-06-14 17:58:5934 // Tab indent size
Christopher Dunn948f2902015-01-25 18:19:3035 // (multi-line comment)
36 "indent" : { /*embedded comment*/ "length" : 3, "use_space": true }
Christopher Dunne0d72242007-06-14 17:58:5937}
Christopher Dunn948f2902015-01-25 18:19:3038\endcode
Christopher Dunne0d72242007-06-14 17:58:5939
40\section _features Features
41- read and write JSON document
Christopher Dunn35bea412014-07-05 19:13:3742- attach C++ style comments to element during parsing
Christopher Dunne0d72242007-06-14 17:58:5943- rewrite JSON document preserving original comments
44
Christopher Dunn948f2902015-01-25 18:19:3045Notes: Comments used to be supported in JSON but were removed for
Baptiste Lepilleure6a77412010-03-11 21:02:2646portability (C like comments are not supported in Python). Since
47comments are useful in configuration/input file, this feature was
48preserved.
49
50\section _example Code example
51
Christopher Dunne0d72242007-06-14 17:58:5952\code
Christopher Dunn66a8ba22015-02-09 07:29:4353Json::Value root; // 'root' will contain the root value after parsing.
Christopher Dunnf3b33582015-02-09 17:51:0654std::cin >> root;
Christopher Dunne0d72242007-06-14 17:58:5955
Christopher Dunn694dbcb2015-02-09 21:25:5756// You can also read into a particular sub-value.
57std::cin >> root["subtree"];
58
Christopher Dunnaa13a8b2015-02-13 15:37:3959// Get the value of the member of root named 'encoding',
60// and return 'UTF-8' if there is no such member.
Christopher Dunne0d72242007-06-14 17:58:5961std::string encoding = root.get("encoding", "UTF-8" ).asString();
Christopher Dunnaa13a8b2015-02-13 15:37:3962
63// Get the value of the member of root named 'plug-ins'; return a 'null' value if
Christopher Dunne0d72242007-06-14 17:58:5964// there is no such member.
65const Json::Value plugins = root["plug-ins"];
Christopher Dunnaa13a8b2015-02-13 15:37:3966
67// Iterate over the sequence elements.
68for ( int index = 0; index < plugins.size(); ++index )
Christopher Dunne0d72242007-06-14 17:58:5969 loadPlugIn( plugins[index].asString() );
Jordan Bayles7b286982019-08-14 05:41:4370
Christopher Dunnaa13a8b2015-02-13 15:37:3971// Try other datatypes. Some are auto-convertible to others.
Christopher Dunn694dbcb2015-02-09 21:25:5772foo::setIndentLength( root["indent"].get("length", 3).asInt() );
73foo::setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
Christopher Dunne0d72242007-06-14 17:58:5974
Christopher Dunnaa13a8b2015-02-13 15:37:3975// Since Json::Value has an implicit constructor for all value types, it is not
76// necessary to explicitly construct the Json::Value object.
Christopher Dunn694dbcb2015-02-09 21:25:5777root["encoding"] = foo::getCurrentEncoding();
78root["indent"]["length"] = foo::getCurrentIndentLength();
79root["indent"]["use_space"] = foo::getCurrentIndentUseSpace();
Christopher Dunne0d72242007-06-14 17:58:5980
Christopher Dunnc7b39c22015-01-26 00:45:5981// If you like the defaults, you can insert directly into a stream.
Christopher Dunnf3b33582015-02-09 17:51:0682std::cout << root;
83// Of course, you can write to `std::ostringstream` if you prefer.
Christopher Dunnc7b39c22015-01-26 00:45:5984
Christopher Dunn28a20912015-01-26 16:43:3985// If desired, remember to add a linefeed and flush.
86std::cout << std::endl;
Christopher Dunn8df98f62015-02-09 17:15:3987\endcode
Christopher Dunn28a20912015-01-26 16:43:3988
Christopher Dunn8df98f62015-02-09 17:15:3989\section _advanced Advanced usage
Christopher Dunn694dbcb2015-02-09 21:25:5790
91Configure *builders* to create *readers* and *writers*. For
92configuration, we use our own `Json::Value` (rather than
93standard setters/getters) so that we can add
94features without losing binary-compatibility.
95
Christopher Dunn8df98f62015-02-09 17:15:3996\code
Christopher Dunn694dbcb2015-02-09 21:25:5797// For convenience, use `writeString()` with a specialized builder.
Christopher Dunn8df98f62015-02-09 17:15:3998Json::StreamWriterBuilder wbuilder;
Christopher Dunn37dde9d2015-03-04 21:04:1999wbuilder["indentation"] = "\t";
Christopher Dunn694dbcb2015-02-09 21:25:57100std::string document = Json::writeString(wbuilder, root);
Christopher Dunn9da9f842015-01-26 01:20:43101
Christopher Dunn694dbcb2015-02-09 21:25:57102// Here, using a specialized Builder, we discard comments and
103// record errors as we parse.
Christopher Dunn8df98f62015-02-09 17:15:39104Json::CharReaderBuilder rbuilder;
Christopher Dunn37dde9d2015-03-04 21:04:19105rbuilder["collectComments"] = false;
Christopher Dunn8df98f62015-02-09 17:15:39106std::string errs;
Christopher Dunn694dbcb2015-02-09 21:25:57107bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs);
Christopher Dunne0d72242007-06-14 17:58:59108\endcode
109
Christopher Dunn3cf91752015-02-10 00:16:24110Yes, compile-time configuration-checking would be helpful,
111but `Json::Value` lets you
112write and read the builder configuration, which is better! In other words,
113you can configure your JSON parser using JSON.
114
115CharReaders and StreamWriters are not thread-safe, but they are re-usable.
116\code
117Json::CharReaderBuilder rbuilder;
118cfg >> rbuilder.settings_;
119std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader());
120reader->parse(start, stop, &value1, &errs);
121// ...
122reader->parse(start, stop, &value2, &errs);
123// etc.
124\endcode
125
Baptiste Lepilleura11e47d2010-03-12 10:17:46126\section _pbuild Build instructions
Jordan Bayles7b286982019-08-14 05:41:43127The build instructions are located in the file
Christopher Dunn5a651322014-07-06 03:44:33128<a HREF="https://github.com/open-source-parsers/jsoncpp/blob/master/README.md">README.md</a> in the top-directory of the project.
Christopher Dunne0d72242007-06-14 17:58:59129
Christopher Dunn35bea412014-07-05 19:13:37130The latest version of the source is available in the project's GitHub repository:
Christopher Dunnbef834e2014-07-06 02:05:10131<a HREF="https://github.com/open-source-parsers/jsoncpp/">
Christopher Dunn35bea412014-07-05 19:13:37132jsoncpp</a>
Christopher Dunne0d72242007-06-14 17:58:59133
Baptiste Lepilleur130730f2010-03-13 11:14:49134\section _news What's New?
Jordan Bayles7b286982019-08-14 05:41:43135The description of latest changes can be found in
Christopher Dunn5a651322014-07-06 03:44:33136<a HREF="https://github.com/open-source-parsers/jsoncpp/wiki/NEWS">
137 the NEWS wiki
138</a>.
Christopher Dunne0d72242007-06-14 17:58:59139
140\section _rlinks Related links
141- <a HREF="http://www.json.org/">JSON</a> Specification and alternate language implementations.
142- <a HREF="http://www.yaml.org/">YAML</a> A data format designed for human readability.
143- <a HREF="http://www.cl.cam.ac.uk/~mgk25/unicode.html">UTF-8 and Unicode FAQ</a>.
144
Christopher Dunn35bea412014-07-05 19:13:37145\section _plinks Old project links
146- <a href="https://sourceforge.net/projects/jsoncpp/">https://sourceforge.net/projects/jsoncpp/</a>
147- <a href="http://jsoncpp.sourceforge.net">http://jsoncpp.sourceforge.net</a>
148- <a href="http://sourceforge.net/projects/jsoncpp/files/">http://sourceforge.net/projects/jsoncpp/files/</a>
149- <a href="http://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/">http://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/</a>
150- <a href="http://jsoncpp.sourceforge.net/old.html">http://jsoncpp.sourceforge.net/old.html</a>
151
Christopher Dunne0d72242007-06-14 17:58:59152\section _license License
Christopher Dunnbef834e2014-07-06 02:05:10153See file <a href="https://github.com/open-source-parsers/jsoncpp/blob/master/LICENSE"><code>LICENSE</code></a> in the top-directory of the project.
Baptiste Lepilleur7469f1d2010-04-20 21:35:19154
Jordan Bayles7b286982019-08-14 05:41:43155Basically JsonCpp is licensed under MIT license, or public domain if desired
Baptiste Lepilleur7469f1d2010-04-20 21:35:19156and recognized in your jurisdiction.
Christopher Dunne0d72242007-06-14 17:58:59157
Christopher Dunn35bea412014-07-05 19:13:37158\author Baptiste Lepilleur <blep@users.sourceforge.net> (originator)
Christopher Dunnda0fcfb2015-02-12 17:18:23159\author Christopher Dunn <cdunn2001@gmail.com> (primary maintainer)
Christopher Dunn249fd182015-02-09 06:46:20160\version \include version
Christopher Dunn3cf91752015-02-10 00:16:24161We make strong guarantees about binary-compatibility, consistent with
162<a href="http://apr.apache.org/versioning.html">the Apache versioning scheme</a>.
Christopher Dunn249fd182015-02-09 06:46:20163\sa version.h
Christopher Dunne0d72242007-06-14 17:58:59164*/