condense.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. // JSON condenser example
  2. // This example parses JSON text from stdin with validation,
  3. // and re-output the JSON content to stdout without whitespace.
  4. #include <rtthread.h>
  5. #include "rapidjson/reader.h"
  6. #include "rapidjson/writer.h"
  7. #include "rapidjson/filereadstream.h"
  8. #include "rapidjson/filewritestream.h"
  9. #include "rapidjson/error/en.h"
  10. using namespace rapidjson;
  11. int condense(int, char*[]) {
  12. // Prepare JSON reader and input stream.
  13. Reader reader;
  14. char readBuffer[1024];
  15. FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
  16. // Prepare JSON writer and output stream.
  17. char writeBuffer[1024];
  18. FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
  19. Writer<FileWriteStream> writer(os);
  20. // JSON reader parse from the input stream and let writer generate the output.
  21. if (!reader.Parse(is, writer)) {
  22. fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
  23. return 1;
  24. }
  25. return 0;
  26. }
  27. MSH_CMD_EXPORT(condense, rapid json condense example);