json_writer.h 2.8 KB

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