JsonCpp project page JsonCpp home page

Json::StreamWriterBuilder Class Reference

Build a StreamWriter implementation. More...

#include <json/writer.h>

Inherits Json::StreamWriter::Factory.

Collaboration diagram for Json::StreamWriterBuilder:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 StreamWriterBuilder ()
virtual ~StreamWriterBuilder ()
virtual StreamWriternewStreamWriter () const
bool validate (Json::Value *invalid) const
Valueoperator[] (std::string key)
 A simple way to update a specific setting.

Static Public Member Functions

static void setDefaults (Json::Value *settings)
 Called by ctor, but you can use this to reset settings_.

Public Attributes

Json::Value settings_
 Configuration of this builder.

Detailed Description

Build a StreamWriter implementation.

Usage:

  using namespace Json;
  Value value = ...;
  StreamWriterBuilder builder;
  builder["commentStyle"] = "None";
  builder["indentation"] = "   ";  // or whatever you like
  std::unique_ptr<Json::StreamWriter> writer(
      builder.newStreamWriter());
  writer->write(value, &std::cout);
  std::cout << std::endl;  // add lf and flush

Definition at line 87 of file writer.h.


Constructor & Destructor Documentation

Json::StreamWriterBuilder::StreamWriterBuilder (  ) 

Definition at line 1079 of file json_writer.cpp.

References setDefaults(), and settings_.

Json::StreamWriterBuilder::~StreamWriterBuilder (  )  [virtual]

Definition at line 1083 of file json_writer.cpp.


Member Function Documentation

StreamWriter * Json::StreamWriterBuilder::newStreamWriter (  )  const [virtual]
Exceptions:
std::exception if something goes wrong (e.g. invalid settings)

Implements Json::StreamWriter::Factory.

Definition at line 1085 of file json_writer.cpp.

References Json::Value::asBool(), Json::Value::asString(), settings_, and Json::throwRuntimeError().

Referenced by Json::operator<<().

Here is the caller graph for this function:

Value & Json::StreamWriterBuilder::operator[] ( std::string  key  ) 

A simple way to update a specific setting.

Definition at line 1139 of file json_writer.cpp.

References settings_.

void Json::StreamWriterBuilder::setDefaults ( Json::Value settings  )  [static]

Called by ctor, but you can use this to reset settings_.

Precondition:
'settings' != NULL (but Json::null is fine)
Remarks:
Defaults: src/lib_json/json_writer.cpp StreamWriterBuilderDefaults

[StreamWriterBuilderDefaults]

[StreamWriterBuilderDefaults]

Definition at line 1144 of file json_writer.cpp.

Referenced by StreamWriterBuilder().

Here is the caller graph for this function:

bool Json::StreamWriterBuilder::validate ( Json::Value invalid  )  const
Returns:
true if 'settings' are legal and consistent; otherwise, indicate bad settings via 'invalid'.

Definition at line 1122 of file json_writer.cpp.

References Json::Value::getMemberNames(), Json::getValidWriterKeys(), settings_, and Json::Value::size().


Member Data Documentation

Configuration of this builder.

Available settings (case-sensitive):

  • "commentStyle": "None" or "All"
  • "indentation": "<anything>"
  • "enableYAMLCompatibility": false or true
    • slightly change the whitespace around colons
  • "dropNullPlaceholders": false or true
    • Drop the "null" string from the writer's output for nullValues. Strictly speaking, this is not valid JSON. But when the output is being fed to a browser's Javascript, it makes for smaller output and the browser can handle the output just fine.

You can examine 'settings_` yourself to see the defaults. You can also write and read them just like any JSON Value.

See also:
setDefaults()

Definition at line 108 of file writer.h.

Referenced by newStreamWriter(), operator[](), StreamWriterBuilder(), and validate().


The documentation for this class was generated from the following files: