/* * 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. */ #pragma once #include #include #include #include #include namespace facebook { namespace thrift { /** * Describes the format of the data contained in the `folly::dynamic`. */ enum class dynamic_format { /** * A data format that's aimed at being more portable and easier to read by * humans. */ PORTABLE, /** * A data format that's compatible with `readFromJson()` and * `TSimpleJSONProtocol` from Thrift 1. */ JSON_1 }; /** * Tells how much a decoder should adhere to the data format specification. */ enum class format_adherence { /** * Demands the data to strictly follow the given format. Any deviation from * the format will be rejected. */ STRICT, /** * Accepts data that deviates from the format, as long as the deviation is not * ambiguous and can be safely interpreted by the decoder. */ LENIENT }; /** * Converts an object to its `folly::dynamic` representation using Thrift's * always-on reflection support. * * The root object is output to the given `folly::dynamic` output parameter. */ template void to_dynamic(folly::dynamic& out, T&& input, dynamic_format format) { detail::dynamic_converter_impl::to(out, std::forward(input), format); } template void to_dynamic(folly::dynamic& out, T&& input, dynamic_format format) { using Tag = apache::thrift::type::infer_tag; return to_dynamic(out, std::forward(input), format); } /** * Converts an object to its `folly::dynamic` representation using Thrift's * always-on reflection support. */ template folly::dynamic to_dynamic(T&& input, dynamic_format format) { folly::dynamic result(folly::dynamic::object); to_dynamic(result, std::forward(input), format); return result; } template folly::dynamic to_dynamic(T&& input, dynamic_format format) { folly::dynamic result(folly::dynamic::object); to_dynamic(result, std::forward(input), format); return result; } /** * Converts an object from its `folly::dynamic` representation using Thrift's * always-on reflection support. * NOTE: this function doesn't clear the output parameter prior decoding and requires a cleared object to be passed in * * The decoded object is output to the given `out` parameter. */ template void from_dynamic( T& out, const folly::dynamic& input, dynamic_format format, format_adherence adherence = format_adherence::STRICT) { detail::dynamic_converter_impl::from(out, input, format, adherence); } template void from_dynamic( T& out, const folly::dynamic& input, dynamic_format format, format_adherence adherence = format_adherence::STRICT) { using Tag = apache::thrift::type::infer_tag; from_dynamic(out, input, format, adherence); } template void from_dynamic( T& out, folly::StringPiece input, dynamic_format format, format_adherence adherence = format_adherence::STRICT) = delete; template void from_dynamic( T& out, folly::StringPiece input, dynamic_format format, format_adherence adherence = format_adherence::STRICT) = delete; /** * Converts an object from its `folly::dynamic` representation using Thrift's * always-on reflection support. */ template T from_dynamic( const folly::dynamic& input, dynamic_format format, format_adherence adherence = format_adherence::STRICT) { T result; detail::dynamic_converter_impl::from(result, input, format, adherence); return result; } template T from_dynamic( const folly::dynamic& input, dynamic_format format, format_adherence adherence = format_adherence::STRICT) { using Tag = apache::thrift::type::infer_tag; return from_dynamic(input, format, adherence); } template T from_dynamic( folly::StringPiece input, dynamic_format format, format_adherence adherence = format_adherence::STRICT) = delete; template T from_dynamic( folly::StringPiece input, dynamic_format format, format_adherence adherence = format_adherence::STRICT) = delete; } // namespace thrift } // namespace facebook #include