pretty.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. // JSON pretty formatting example
  2. // This example can only handle UTF-8. For handling other encodings, see prettyauto example.
  3. #include <rtthread.h>
  4. #include "rapidjson/reader.h"
  5. #include "rapidjson/prettywriter.h"
  6. #include "rapidjson/filereadstream.h"
  7. #include "rapidjson/filewritestream.h"
  8. #include "rapidjson/error/en.h"
  9. using namespace rapidjson;
  10. int pretty(int, char*[]) {
  11. // Prepare reader and input stream.
  12. Reader reader;
  13. char readBuffer[1024];
  14. FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
  15. // Prepare writer and output stream.
  16. char writeBuffer[1024];
  17. FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
  18. PrettyWriter<FileWriteStream> writer(os);
  19. // JSON reader parse from the input stream and let writer generate the output.
  20. if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) {
  21. fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
  22. return 1;
  23. }
  24. return 0;
  25. }
  26. MSH_CMD_EXPORT(pretty, rapid json pretty example);