readFromStream.cpp 727 B

12345678910111213141516171819202122232425262728293031
  1. #include "json/json.h"
  2. #include <fstream>
  3. #include <iostream>
  4. /** \brief Parse from stream, collect comments and capture error info.
  5. * Example Usage:
  6. * $g++ readFromStream.cpp -ljsoncpp -std=c++11 -o readFromStream
  7. * $./readFromStream
  8. * // comment head
  9. * {
  10. * // comment before
  11. * "key" : "value"
  12. * }
  13. * // comment after
  14. * // comment tail
  15. */
  16. int main(int argc, char* argv[]) {
  17. Json::Value root;
  18. std::ifstream ifs;
  19. ifs.open(argv[1]);
  20. Json::CharReaderBuilder builder;
  21. builder["collectComments"] = true;
  22. JSONCPP_STRING errs;
  23. if (!parseFromStream(builder, ifs, &root, &errs)) {
  24. std::cout << errs << std::endl;
  25. return EXIT_FAILURE;
  26. }
  27. std::cout << root << std::endl;
  28. return EXIT_SUCCESS;
  29. }