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