Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 1 | /** |
| 2 | \mainpage |
| 3 | \section _intro Introduction |
| 4 | |
| 5 | <a HREF="http://www.json.org/">JSON (JavaScript Object Notation)</a> |
| 6 | is a lightweight data-interchange format. |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 7 | |
| 8 | Here is an example of JSON data: |
| 9 | \verbatim |
Christopher Dunn | 948f290 | 2015-01-25 12:19:30 -0600 | [diff] [blame] | 10 | { |
| 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 Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 22 | // Configuration options |
| 23 | { |
| 24 | // Default encoding for text |
| 25 | "encoding" : "UTF-8", |
| 26 | |
| 27 | // Plug-ins loaded at start-up |
| 28 | "plug-ins" : [ |
| 29 | "python", |
Christopher Dunn | 948f290 | 2015-01-25 12:19:30 -0600 | [diff] [blame] | 30 | "c++", // trailing comment |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 31 | "ruby" |
| 32 | ], |
| 33 | |
| 34 | // Tab indent size |
Christopher Dunn | 948f290 | 2015-01-25 12:19:30 -0600 | [diff] [blame] | 35 | // (multi-line comment) |
| 36 | "indent" : { /*embedded comment*/ "length" : 3, "use_space": true } |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 37 | } |
Christopher Dunn | 948f290 | 2015-01-25 12:19:30 -0600 | [diff] [blame] | 38 | \endcode |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 39 | |
| 40 | \section _features Features |
| 41 | - read and write JSON document |
Christopher Dunn | 35bea41 | 2014-07-05 12:13:37 -0700 | [diff] [blame] | 42 | - attach C++ style comments to element during parsing |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 43 | - rewrite JSON document preserving original comments |
| 44 | |
Christopher Dunn | 948f290 | 2015-01-25 12:19:30 -0600 | [diff] [blame] | 45 | Notes: Comments used to be supported in JSON but were removed for |
Baptiste Lepilleur | e6a7741 | 2010-03-11 21:02:26 +0000 | [diff] [blame] | 46 | portability (C like comments are not supported in Python). Since |
| 47 | comments are useful in configuration/input file, this feature was |
| 48 | preserved. |
| 49 | |
| 50 | \section _example Code example |
| 51 | |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 52 | \code |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 53 | Json::Value root; // 'root' will contain the root value after parsing. |
Christopher Dunn | f3b3358 | 2015-02-09 11:51:06 -0600 | [diff] [blame] | 54 | std::cin >> root; |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 55 | |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 56 | // You can also read into a particular sub-value. |
| 57 | std::cin >> root["subtree"]; |
| 58 | |
Christopher Dunn | aa13a8b | 2015-02-13 09:37:39 -0600 | [diff] [blame] | 59 | // Get the value of the member of root named 'encoding', |
| 60 | // and return 'UTF-8' if there is no such member. |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 61 | std::string encoding = root.get("encoding", "UTF-8" ).asString(); |
Christopher Dunn | aa13a8b | 2015-02-13 09:37:39 -0600 | [diff] [blame] | 62 | |
| 63 | // Get the value of the member of root named 'plug-ins'; return a 'null' value if |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 64 | // there is no such member. |
| 65 | const Json::Value plugins = root["plug-ins"]; |
Christopher Dunn | aa13a8b | 2015-02-13 09:37:39 -0600 | [diff] [blame] | 66 | |
| 67 | // Iterate over the sequence elements. |
| 68 | for ( int index = 0; index < plugins.size(); ++index ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 69 | loadPlugIn( plugins[index].asString() ); |
| 70 | |
Christopher Dunn | aa13a8b | 2015-02-13 09:37:39 -0600 | [diff] [blame] | 71 | // Try other datatypes. Some are auto-convertible to others. |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 72 | foo::setIndentLength( root["indent"].get("length", 3).asInt() ); |
| 73 | foo::setIndentUseSpace( root["indent"].get("use_space", true).asBool() ); |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 74 | |
Christopher Dunn | aa13a8b | 2015-02-13 09:37:39 -0600 | [diff] [blame] | 75 | // Since Json::Value has an implicit constructor for all value types, it is not |
| 76 | // necessary to explicitly construct the Json::Value object. |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 77 | root["encoding"] = foo::getCurrentEncoding(); |
| 78 | root["indent"]["length"] = foo::getCurrentIndentLength(); |
| 79 | root["indent"]["use_space"] = foo::getCurrentIndentUseSpace(); |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 80 | |
Christopher Dunn | c7b39c2 | 2015-01-25 18:45:59 -0600 | [diff] [blame] | 81 | // If you like the defaults, you can insert directly into a stream. |
Christopher Dunn | f3b3358 | 2015-02-09 11:51:06 -0600 | [diff] [blame] | 82 | std::cout << root; |
| 83 | // Of course, you can write to `std::ostringstream` if you prefer. |
Christopher Dunn | c7b39c2 | 2015-01-25 18:45:59 -0600 | [diff] [blame] | 84 | |
Christopher Dunn | 28a2091 | 2015-01-26 10:43:39 -0600 | [diff] [blame] | 85 | // If desired, remember to add a linefeed and flush. |
| 86 | std::cout << std::endl; |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame] | 87 | \endcode |
Christopher Dunn | 28a2091 | 2015-01-26 10:43:39 -0600 | [diff] [blame] | 88 | |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame] | 89 | \section _advanced Advanced usage |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 90 | |
| 91 | Configure *builders* to create *readers* and *writers*. For |
| 92 | configuration, we use our own `Json::Value` (rather than |
| 93 | standard setters/getters) so that we can add |
| 94 | features without losing binary-compatibility. |
| 95 | |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame] | 96 | \code |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 97 | // For convenience, use `writeString()` with a specialized builder. |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame] | 98 | Json::StreamWriterBuilder wbuilder; |
Christopher Dunn | 37dde9d | 2015-03-04 15:04:19 -0600 | [diff] [blame] | 99 | wbuilder["indentation"] = "\t"; |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 100 | std::string document = Json::writeString(wbuilder, root); |
Christopher Dunn | 9da9f84 | 2015-01-25 19:20:43 -0600 | [diff] [blame] | 101 | |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 102 | // Here, using a specialized Builder, we discard comments and |
| 103 | // record errors as we parse. |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame] | 104 | Json::CharReaderBuilder rbuilder; |
Christopher Dunn | 37dde9d | 2015-03-04 15:04:19 -0600 | [diff] [blame] | 105 | rbuilder["collectComments"] = false; |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame] | 106 | std::string errs; |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 107 | bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs); |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 108 | \endcode |
| 109 | |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 110 | Yes, compile-time configuration-checking would be helpful, |
| 111 | but `Json::Value` lets you |
| 112 | write and read the builder configuration, which is better! In other words, |
| 113 | you can configure your JSON parser using JSON. |
| 114 | |
| 115 | CharReaders and StreamWriters are not thread-safe, but they are re-usable. |
| 116 | \code |
| 117 | Json::CharReaderBuilder rbuilder; |
| 118 | cfg >> rbuilder.settings_; |
| 119 | std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader()); |
| 120 | reader->parse(start, stop, &value1, &errs); |
| 121 | // ... |
| 122 | reader->parse(start, stop, &value2, &errs); |
| 123 | // etc. |
| 124 | \endcode |
| 125 | |
Baptiste Lepilleur | a11e47d | 2010-03-12 10:17:46 +0000 | [diff] [blame] | 126 | \section _pbuild Build instructions |
Baptiste Lepilleur | e6a7741 | 2010-03-11 21:02:26 +0000 | [diff] [blame] | 127 | The build instructions are located in the file |
Christopher Dunn | 5a65132 | 2014-07-05 20:44:33 -0700 | [diff] [blame] | 128 | <a HREF="https://github.com/open-source-parsers/jsoncpp/blob/master/README.md">README.md</a> in the top-directory of the project. |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 129 | |
Christopher Dunn | 35bea41 | 2014-07-05 12:13:37 -0700 | [diff] [blame] | 130 | The latest version of the source is available in the project's GitHub repository: |
Christopher Dunn | bef834e | 2014-07-05 19:05:10 -0700 | [diff] [blame] | 131 | <a HREF="https://github.com/open-source-parsers/jsoncpp/"> |
Christopher Dunn | 35bea41 | 2014-07-05 12:13:37 -0700 | [diff] [blame] | 132 | jsoncpp</a> |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 133 | |
Baptiste Lepilleur | 130730f | 2010-03-13 11:14:49 +0000 | [diff] [blame] | 134 | \section _news What's New? |
| 135 | The description of latest changes can be found in |
Christopher Dunn | 5a65132 | 2014-07-05 20:44:33 -0700 | [diff] [blame] | 136 | <a HREF="https://github.com/open-source-parsers/jsoncpp/wiki/NEWS"> |
| 137 | the NEWS wiki |
| 138 | </a>. |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 139 | |
| 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 Dunn | 35bea41 | 2014-07-05 12:13:37 -0700 | [diff] [blame] | 145 | \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 Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 152 | \section _license License |
Christopher Dunn | bef834e | 2014-07-05 19:05:10 -0700 | [diff] [blame] | 153 | See 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 Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 154 | |
| 155 | Basically JsonCpp is licensed under MIT license, or public domain if desired |
| 156 | and recognized in your jurisdiction. |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 157 | |
Christopher Dunn | 35bea41 | 2014-07-05 12:13:37 -0700 | [diff] [blame] | 158 | \author Baptiste Lepilleur <blep@users.sourceforge.net> (originator) |
Christopher Dunn | da0fcfb | 2015-02-12 11:18:23 -0600 | [diff] [blame] | 159 | \author Christopher Dunn <cdunn2001@gmail.com> (primary maintainer) |
Christopher Dunn | 249fd18 | 2015-02-09 00:46:20 -0600 | [diff] [blame] | 160 | \version \include version |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 161 | We make strong guarantees about binary-compatibility, consistent with |
| 162 | <a href="http://apr.apache.org/versioning.html">the Apache versioning scheme</a>. |
Christopher Dunn | 249fd18 | 2015-02-09 00:46:20 -0600 | [diff] [blame] | 163 | \sa version.h |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 164 | */ |