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 5 #ifndef CRDTP_JSON_H_ 6 #define CRDTP_JSON_H_ 7 8 #include <memory> 9 #include <vector> 10 #include "export.h" 11 #include "parser_handler.h" 12 13 namespace crdtp { 14 namespace json { 15 // ============================================================================= 16 // json::NewJSONEncoder - for encoding streaming parser events as JSON 17 // ============================================================================= 18 19 // Returns a handler object which will write ascii characters to |out|. 20 // |status->ok()| will be false iff the handler routine HandleError() is called. 21 // In that case, we'll stop emitting output. 22 // Except for calling the HandleError routine at any time, the client 23 // code must call the Handle* methods in an order in which they'd occur 24 // in valid JSON; otherwise we may crash (the code uses assert). 25 CRDTP_EXPORT std::unique_ptr<ParserHandler> NewJSONEncoder( 26 std::vector<uint8_t>* out, 27 Status* status); 28 29 CRDTP_EXPORT std::unique_ptr<ParserHandler> NewJSONEncoder(std::string* out, 30 Status* status); 31 32 // ============================================================================= 33 // json::ParseJSON - for receiving streaming parser events for JSON 34 // ============================================================================= 35 36 CRDTP_EXPORT void ParseJSON(span<uint8_t> chars, ParserHandler* handler); 37 38 CRDTP_EXPORT void ParseJSON(span<uint16_t> chars, ParserHandler* handler); 39 40 // ============================================================================= 41 // json::ConvertCBORToJSON, json::ConvertJSONToCBOR - for transcoding 42 // ============================================================================= 43 44 CRDTP_EXPORT Status ConvertCBORToJSON(span<uint8_t> cbor, std::string* json); 45 46 CRDTP_EXPORT Status ConvertCBORToJSON(span<uint8_t> cbor, 47 std::vector<uint8_t>* json); 48 49 CRDTP_EXPORT Status ConvertJSONToCBOR(span<uint8_t> json, 50 std::vector<uint8_t>* cbor); 51 52 CRDTP_EXPORT Status ConvertJSONToCBOR(span<uint16_t> json, 53 std::vector<uint8_t>* cbor); 54 } // namespace json 55 } // namespace crdtp 56 57 #endif // CRDTP_JSON_H_ 58