json_writer.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2012 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. #ifndef BASE_JSON_JSON_WRITER_H_
  5. #define BASE_JSON_JSON_WRITER_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "base/json/json_common.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/strings/string_piece.h"
  12. #include "base/values.h"
  13. namespace base {
  14. class BASE_EXPORT JSONWriter {
  15. public:
  16. enum Options {
  17. // This option instructs the writer that if a Binary value is encountered,
  18. // the value (and key if within a dictionary) will be omitted from the
  19. // output, and success will be returned. Otherwise, if a binary value is
  20. // encountered, failure will be returned.
  21. OPTIONS_OMIT_BINARY_VALUES = 1 << 0,
  22. // This option instructs the writer to write doubles that have no fractional
  23. // part as a normal integer (i.e., without using exponential notation
  24. // or appending a '.0') as long as the value is within the range of a
  25. // 64-bit int.
  26. OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 1,
  27. // Return a slightly nicer formatted json string (pads with whitespace to
  28. // help with readability).
  29. OPTIONS_PRETTY_PRINT = 1 << 2,
  30. };
  31. JSONWriter(const JSONWriter&) = delete;
  32. JSONWriter& operator=(const JSONWriter&) = delete;
  33. // Given a root node, generates a JSON string and puts it into |json|.
  34. // The output string is overwritten and not appended.
  35. //
  36. // TODO(tc): Should we generate json if it would be invalid json (e.g.,
  37. // |node| is not a dictionary/list Value or if there are inf/-inf float
  38. // values)? Return true on success and false on failure.
  39. static bool Write(ValueView node,
  40. std::string* json,
  41. size_t max_depth = internal::kAbsoluteMaxDepth);
  42. // Same as above but with |options| which is a bunch of JSONWriter::Options
  43. // bitwise ORed together. Return true on success and false on failure.
  44. static bool WriteWithOptions(ValueView node,
  45. int options,
  46. std::string* json,
  47. size_t max_depth = internal::kAbsoluteMaxDepth);
  48. private:
  49. JSONWriter(int options,
  50. std::string* json,
  51. size_t max_depth = internal::kAbsoluteMaxDepth);
  52. // Called recursively to build the JSON string. When completed,
  53. // |json_string_| will contain the JSON.
  54. bool BuildJSONString(absl::monostate node, size_t depth);
  55. bool BuildJSONString(bool node, size_t depth);
  56. bool BuildJSONString(int node, size_t depth);
  57. bool BuildJSONString(double node, size_t depth);
  58. bool BuildJSONString(StringPiece node, size_t depth);
  59. bool BuildJSONString(const Value::BlobStorage& node, size_t depth);
  60. bool BuildJSONString(const Value::Dict& node, size_t depth);
  61. bool BuildJSONString(const Value::List& node, size_t depth);
  62. // Adds space to json_string_ for the indent level.
  63. void IndentLine(size_t depth);
  64. bool omit_binary_values_;
  65. bool omit_double_type_preservation_;
  66. bool pretty_print_;
  67. // Where we write JSON data as we generate it.
  68. raw_ptr<std::string> json_string_;
  69. // Maximum depth to write.
  70. const size_t max_depth_;
  71. // The number of times the writer has recursed (current stack depth).
  72. size_t stack_depth_;
  73. };
  74. } // namespace base
  75. #endif // BASE_JSON_JSON_WRITER_H_