/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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. */ // // Docs: https://fburl.com/fbcref_json // /** * Serialize and deserialize folly::dynamic values as JSON. * * Basic JSON type system: * * Value : String | Bool | Null | Object | Array | Number * String : UTF-8 sequence * Object : (String, Value) pairs, with unique String keys * Array : ordered list of Values * Null : null * Bool : true | false * Number : (representation unspecified) * * For more information see http://json.org or look up RFC 4627. * * If your dynamic has anything illegal with regard to this type * system, the serializer will throw. * * @file json.h */ #pragma once #include <iosfwd> #include <string> #include <folly/Function.h> #include <folly/Range.h> #include <folly/json/dynamic.h> namespace folly { ////////////////////////////////////////////////////////////////////// namespace json { struct serialization_opts { … }; /** * Create bitmap for serialization_opts.extra_ascii_to_escape_bitmap. * * Generates a bitmap with bits set for each of the ASCII characters provided * for use in the serialization_opts extra_ascii_to_escape_bitmap option. If any * characters are not valid ASCII, they are ignored. */ std::array<uint64_t, 2> buildExtraAsciiToEscapeBitmap(StringPiece chars); /** * Serialize dynamic to json-string, with options. * * Main JSON serialization routine taking folly::dynamic parameters. * For the most common use cases there are simpler functions in the * main folly namespace. */ std::string serialize(dynamic const&, serialization_opts const&); /** * Escape a string so that it is legal to print it in JSON text. * * Append the result to out. */ void escapeString( StringPiece input, std::string& out, const serialization_opts& opts); /** * Strip all C99-like comments (i.e. // and / * ... * /) */ std::string stripComments(StringPiece jsonC); // Parse error can be thrown when deserializing json (ie., converting string // into json). class FOLLY_EXPORT parse_error : public std::runtime_error { … }; // Print error can be thrown when serializing json (ie., converting json into // string). class FOLLY_EXPORT print_error : public std::runtime_error { … }; // may be extened in future to include offset, col, etc. struct parse_location { … }; // may be extended in future to include end location struct parse_range { … }; struct parse_metadata { … }; metadata_map; } // namespace json ////////////////////////////////////////////////////////////////////// /** * Parse a json blob out of a range and produce a dynamic representing * it. */ dynamic parseJson(StringPiece, json::serialization_opts const&); dynamic parseJson(StringPiece); dynamic parseJsonWithMetadata(StringPiece range, json::metadata_map* map); dynamic parseJsonWithMetadata( StringPiece range, json::serialization_opts const& opts, json::metadata_map* map); /** * Serialize a dynamic into a json string. */ std::string toJson(dynamic const&); /** * Serialize a dynamic into a json string with indentation. * Note that the keys of all objects will be sorted. */ std::string toPrettyJson(dynamic const&); /** * Printer for GTest. * * Uppercase name to fill GTest's API, which calls this method through ADL. */ void PrintTo(const dynamic&, std::ostream*); ////////////////////////////////////////////////////////////////////// } // namespace folly