// // // Copyright 2015 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // #include <grpc/support/port_platform.h> #include <stdint.h> #include <stdlib.h> #include <map> #include <string> #include <utility> #include <vector> #include "absl/strings/string_view.h" #include "src/core/lib/json/json.h" namespace grpc_core { namespace { // The idea of the writer is basically symmetrical of the reader. While the // reader emits various calls to your code, the writer takes basically the // same calls and emit json out of it. It doesn't try to make any check on // the order of the calls you do on it. Meaning you can theorically force // it to generate invalid json. // // Also, unlike the reader, the writer expects UTF-8 encoded input strings. // These strings will be UTF-8 validated, and any invalid character will // cut the conversion short, before any invalid UTF-8 sequence, thus forming // a valid UTF-8 string overall. // class JsonWriter { … }; // This function checks if there's enough space left in the output buffer, // and will enlarge it if necessary. We're only allocating chunks of 256 // bytes at a time (or multiples thereof). // void JsonWriter::OutputCheck(size_t needed) { … } void JsonWriter::OutputChar(char c) { … } void JsonWriter::OutputString(const absl::string_view str) { … } void JsonWriter::OutputIndent() { … } void JsonWriter::ValueEnd() { … } void JsonWriter::EscapeUtf16(uint16_t utf16) { … } void JsonWriter::EscapeString(const std::string& string) { … } void JsonWriter::ContainerBegins(Json::Type type) { … } void JsonWriter::ContainerEnds(Json::Type type) { … } void JsonWriter::ObjectKey(const std::string& string) { … } void JsonWriter::ValueRaw(const std::string& string) { … } void JsonWriter::ValueString(const std::string& string) { … } void JsonWriter::DumpObject(const Json::Object& object) { … } void JsonWriter::DumpArray(const Json::Array& array) { … } void JsonWriter::DumpValue(const Json& value) { … } std::string JsonWriter::Dump(const Json& value, int indent) { … } } // namespace std::string Json::Dump(int indent) const { … } } // namespace grpc_core