15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_JSON_JSON_WRITER_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_JSON_JSON_WRITER_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Value; 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT JSONWriter { 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum Options { 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // This option instructs the writer that if a Binary value is encountered, 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the value (and key if within a dictionary) will be omitted from the 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // output, and success will be returned. Otherwise, if a binary value is 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // encountered, failure will be returned. 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) OPTIONS_OMIT_BINARY_VALUES = 1 << 0, 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This option instructs the writer to write doubles that have no fractional 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // part as a normal integer (i.e., without using exponential notation 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // or appending a '.0') as long as the value is within the range of a 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 64-bit int. 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 1, 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Return a slightly nicer formatted json string (pads with whitespace to 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // help with readability). 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) OPTIONS_PRETTY_PRINT = 1 << 2, 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Given a root node, generates a JSON string and puts it into |json|. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(tc): Should we generate json if it would be invalid json (e.g., 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // values)? Return true on success and false on failure. 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static bool Write(const Value* const node, std::string* json); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Same as above but with |options| which is a bunch of JSONWriter::Options 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // bitwise ORed together. Return true on success and false on failure. 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static bool WriteWithOptions(const Value* const node, int options, 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* json); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) JSONWriter(int options, std::string* json); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Called recursively to build the JSON string. When completed, 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // |json_string_| will contain the JSON. 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool BuildJSONString(const Value* const node, size_t depth); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Adds space to json_string_ for the indent level. 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void IndentLine(size_t depth); 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool omit_binary_values_; 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool omit_double_type_preservation_; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool pretty_print_; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Where we write JSON data as we generate it. 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* json_string_; 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(JSONWriter); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace base 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // BASE_JSON_JSON_WRITER_H_ 71