json.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CRDTP_JSON_H_
  5. #define CRDTP_JSON_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "export.h"
  9. #include "parser_handler.h"
  10. namespace crdtp {
  11. namespace json {
  12. // =============================================================================
  13. // json::NewJSONEncoder - for encoding streaming parser events as JSON
  14. // =============================================================================
  15. // Returns a handler object which will write ascii characters to |out|.
  16. // |status->ok()| will be false iff the handler routine HandleError() is called.
  17. // In that case, we'll stop emitting output.
  18. // Except for calling the HandleError routine at any time, the client
  19. // code must call the Handle* methods in an order in which they'd occur
  20. // in valid JSON; otherwise we may crash (the code uses assert).
  21. CRDTP_EXPORT std::unique_ptr<ParserHandler> NewJSONEncoder(
  22. std::vector<uint8_t>* out,
  23. Status* status);
  24. CRDTP_EXPORT std::unique_ptr<ParserHandler> NewJSONEncoder(std::string* out,
  25. Status* status);
  26. // =============================================================================
  27. // json::ParseJSON - for receiving streaming parser events for JSON
  28. // =============================================================================
  29. CRDTP_EXPORT void ParseJSON(span<uint8_t> chars, ParserHandler* handler);
  30. CRDTP_EXPORT void ParseJSON(span<uint16_t> chars, ParserHandler* handler);
  31. // =============================================================================
  32. // json::ConvertCBORToJSON, json::ConvertJSONToCBOR - for transcoding
  33. // =============================================================================
  34. CRDTP_EXPORT Status ConvertCBORToJSON(span<uint8_t> cbor, std::string* json);
  35. CRDTP_EXPORT Status ConvertCBORToJSON(span<uint8_t> cbor,
  36. std::vector<uint8_t>* json);
  37. CRDTP_EXPORT Status ConvertJSONToCBOR(span<uint8_t> json,
  38. std::vector<uint8_t>* cbor);
  39. CRDTP_EXPORT Status ConvertJSONToCBOR(span<uint16_t> json,
  40. std::vector<uint8_t>* cbor);
  41. } // namespace json
  42. } // namespace crdtp
  43. #endif // CRDTP_JSON_H_