|
LLVM
10.0.0
|
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahead of time. More...
#include "llvm/Support/JSON.h"
Public Types | |
| using | Block = llvm::function_ref< void()> |
Public Member Functions | |
| OStream (llvm::raw_ostream &OS, unsigned IndentSize=0) | |
| ~OStream () | |
| void | flush () |
| Flushes the underlying ostream. OStream does not buffer internally. More... | |
| void | value (const Value &V) |
| Emit a self-contained value (number, string, vector<string> etc). More... | |
| void | array (Block Contents) |
| Emit an array whose elements are emitted in the provided Block. More... | |
| void | object (Block Contents) |
| Emit an object whose elements are emitted in the provided Block. More... | |
| void | attribute (llvm::StringRef Key, const Value &Contents) |
| Emit an attribute whose value is self-contained (number, vector<int> etc). More... | |
| void | attributeArray (llvm::StringRef Key, Block Contents) |
| Emit an attribute whose value is an array with elements from the Block. More... | |
| void | attributeObject (llvm::StringRef Key, Block Contents) |
| Emit an attribute whose value is an object with attributes from the Block. More... | |
| void | arrayBegin () |
| void | arrayEnd () |
| void | objectBegin () |
| void | objectEnd () |
| void | attributeBegin (llvm::StringRef Key) |
| void | attributeEnd () |
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahead of time.
It's faster, lower-level, and less safe than OS << json::Value.
Only one "top-level" object can be written to a stream. Simplest usage involves passing lambdas (Blocks) to fill in containers:
json::OStream J(OS); J.array([&]{ for (const Event &E : Events) J.object([&] { J.attribute("timestamp", int64_t(E.Time)); J.attributeArray("participants", [&] { for (const Participant &P : E.Participants) J.value(P.toString()); }); }); });
This would produce JSON like:
[ { "timestamp": 19287398741, "participants": [ "King Kong", "Miley Cyrus", "Cleopatra" ] }, ... ]
The lower level begin/end methods (arrayBegin()) are more flexible but care must be taken to pair them correctly:
json::OStream J(OS); for (const Event &E : Events) { J.objectBegin(); J.attribute("timestamp", int64_t(E.Time)); J.attributeBegin("participants"); for (const Participant &P : E.Participants) J.value(P.toString()); J.attributeEnd(); J.objectEnd(); } J.arrayEnd();
If the call sequence isn't valid JSON, asserts will fire in debug mode. This can be mismatched begin()/end() pairs, trying to emit attributes inside an array, and so on. With asserts disabled, this is undefined behavior.
| using llvm::json::OStream::Block = llvm::function_ref<void()> |
|
inlineexplicit |
|
inline |
|
inline |
| void llvm::json::OStream::arrayBegin | ( | ) |
Definition at line 646 of file JSON.cpp.
References llvm::json::Value::Array.
| void llvm::json::OStream::arrayEnd | ( | ) |
Definition at line 654 of file JSON.cpp.
References llvm::json::Value::Array, and assert().
|
inline |
|
inline |
| void llvm::json::OStream::attributeBegin | ( | llvm::StringRef | Key | ) |
Definition at line 682 of file JSON.cpp.
References assert(), llvm::json::fixUTF8(), llvm::json::isUTF8(), LLVM_LIKELY, llvm::json::Value::Object, and llvm::json::quote().
| void llvm::json::OStream::attributeEnd | ( | ) |
Definition at line 701 of file JSON.cpp.
References assert(), and llvm::json::Value::Object.
|
inline |
Emit an attribute whose value is an object with attributes from the Block.
Definition at line 804 of file JSON.h.
References llvm::HasValue(), and llvm::json::Object::Object().
|
inline |
|
inline |
| void llvm::json::OStream::objectBegin | ( | ) |
Definition at line 664 of file JSON.cpp.
References llvm::json::Value::Object.
| void llvm::json::OStream::objectEnd | ( | ) |
Definition at line 672 of file JSON.cpp.
References assert(), and llvm::json::Value::Object.
Emit a self-contained value (number, string, vector<string> etc).
Definition at line 593 of file JSON.cpp.
References llvm::json::Value::Array, assert(), llvm::json::Value::Boolean, E, llvm::format(), llvm::json::Value::getAsArray(), llvm::json::Value::getAsBoolean(), llvm::json::Value::getAsInteger(), llvm::json::Value::getAsNumber(), llvm::json::Value::getAsObject(), llvm::json::Value::getAsString(), llvm::json::Value::kind(), llvm::json::Value::Null, llvm::json::Value::Number, llvm::json::Value::Object, llvm::json::quote(), llvm::json::sortedElements(), and llvm::json::Value::String.
Referenced by llvm::format_provider< llvm::json::Value >::format(), and llvm::json::operator<<().
1.8.13