| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_JSON_JSON_WRITER_H_ | 5 #ifndef BASE_JSON_JSON_WRITER_H_ |
| 6 #define BASE_JSON_JSON_WRITER_H_ | 6 #define BASE_JSON_JSON_WRITER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 // Return a slightly nicer formatted json string (pads with whitespace to | 32 // Return a slightly nicer formatted json string (pads with whitespace to |
| 33 // help with readability). | 33 // help with readability). |
| 34 OPTIONS_PRETTY_PRINT = 1 << 2, | 34 OPTIONS_PRETTY_PRINT = 1 << 2, |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 // Given a root node, generates a JSON string and puts it into |json|. | 37 // Given a root node, generates a JSON string and puts it into |json|. |
| 38 // TODO(tc): Should we generate json if it would be invalid json (e.g., | 38 // TODO(tc): Should we generate json if it would be invalid json (e.g., |
| 39 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float | 39 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float |
| 40 // values)? Return true on success and false on failure. | 40 // values)? Return true on success and false on failure. |
| 41 static bool Write(const Value* const node, std::string* json); | 41 static bool Write(const Value& node, std::string* json); |
| 42 | 42 |
| 43 // Same as above but with |options| which is a bunch of JSONWriter::Options | 43 // Same as above but with |options| which is a bunch of JSONWriter::Options |
| 44 // bitwise ORed together. Return true on success and false on failure. | 44 // bitwise ORed together. Return true on success and false on failure. |
| 45 static bool WriteWithOptions(const Value* const node, int options, | 45 static bool WriteWithOptions(const Value& node, |
| 46 int options, |
| 46 std::string* json); | 47 std::string* json); |
| 47 | 48 |
| 48 private: | 49 private: |
| 49 JSONWriter(int options, std::string* json); | 50 JSONWriter(int options, std::string* json); |
| 50 | 51 |
| 51 // Called recursively to build the JSON string. When completed, | 52 // Called recursively to build the JSON string. When completed, |
| 52 // |json_string_| will contain the JSON. | 53 // |json_string_| will contain the JSON. |
| 53 bool BuildJSONString(const Value* const node, size_t depth); | 54 bool BuildJSONString(const Value& node, size_t depth); |
| 54 | 55 |
| 55 // Adds space to json_string_ for the indent level. | 56 // Adds space to json_string_ for the indent level. |
| 56 void IndentLine(size_t depth); | 57 void IndentLine(size_t depth); |
| 57 | 58 |
| 58 bool omit_binary_values_; | 59 bool omit_binary_values_; |
| 59 bool omit_double_type_preservation_; | 60 bool omit_double_type_preservation_; |
| 60 bool pretty_print_; | 61 bool pretty_print_; |
| 61 | 62 |
| 62 // Where we write JSON data as we generate it. | 63 // Where we write JSON data as we generate it. |
| 63 std::string* json_string_; | 64 std::string* json_string_; |
| 64 | 65 |
| 65 DISALLOW_COPY_AND_ASSIGN(JSONWriter); | 66 DISALLOW_COPY_AND_ASSIGN(JSONWriter); |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 } // namespace base | 69 } // namespace base |
| 69 | 70 |
| 70 #endif // BASE_JSON_JSON_WRITER_H_ | 71 #endif // BASE_JSON_JSON_WRITER_H_ |
| OLD | NEW |