json.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2015 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <string>
  16. #include <bsoncxx/document/value.hpp>
  17. #include <bsoncxx/document/view.hpp>
  18. #include <bsoncxx/stdx/optional.hpp>
  19. #include <bsoncxx/config/prelude.hpp>
  20. ///
  21. /// Top level namespace for MongoDB C++ BSON functionality.
  22. ///
  23. namespace bsoncxx {
  24. BSONCXX_INLINE_NAMESPACE_BEGIN
  25. // Placing this variable between the `BSONCXX_INLINE_NAMESPACE_BEGIN` and the Doxygen documentation
  26. // for `to_json` suppressed the macro name from being shown as part of the return type in the
  27. // generated documentation pages.
  28. extern const bool k_silence_doxygen;
  29. ///
  30. /// An enumeration of the types of Extended JSON that the to_json function accepts
  31. enum class ExtendedJsonMode : std::uint8_t {
  32. k_legacy, ///< to produce Legacy Extended JSON
  33. k_canonical, ///< to produce Canonical Extended JSON
  34. k_relaxed, ///< to produce Relaxed Extended JSON
  35. };
  36. ///
  37. /// Converts a BSON document to a JSON string, in extended format.
  38. ///
  39. /// @param view
  40. /// A valid BSON document.
  41. /// @param mode
  42. /// An optional JSON representation mode.
  43. ///
  44. /// @throws bsoncxx::exception with error details if the conversion failed.
  45. ///
  46. /// @returns An extended JSON string.
  47. ///
  48. BSONCXX_API std::string BSONCXX_CALL to_json(document::view view,
  49. ExtendedJsonMode mode = ExtendedJsonMode::k_legacy);
  50. ///
  51. /// Constructs a new document::value from the provided JSON text
  52. ///
  53. /// @param 'json'
  54. /// A string_view into a JSON document
  55. ///
  56. /// @returns A document::value if conversion worked.
  57. ///
  58. /// @throws bsoncxx::exception with error details if the conversion failed.
  59. ///
  60. BSONCXX_API document::value BSONCXX_CALL from_json(stdx::string_view json);
  61. BSONCXX_INLINE_NAMESPACE_END
  62. } // namespace bsoncxx
  63. #include <bsoncxx/config/postlude.hpp>