jsoncpp.dox 5.7 KB

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