LLVM 22.0.0git
JSON.h File Reference

This file supports working with JSON data. More...

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <cmath>
#include <map>

Go to the source code of this file.

Classes

class  llvm::json::Object
 An Object is a JSON object, which maps strings to heterogenous JSON values. More...
class  llvm::json::Array
 An Array is a JSON array, which contains heterogeneous JSON values. More...
class  llvm::json::Value
 A Value is an JSON value of unknown type. More...
class  llvm::json::ObjectKey
 ObjectKey is a used to capture keys in Object. More...
struct  llvm::json::Object::KV
class  llvm::json::Path
 A "cursor" marking a position within a Value. More...
class  llvm::json::Path::Root
 The root is the trivial Path to the root value. More...
class  llvm::json::ObjectMapper
 Helper for mapping JSON objects onto protocol structs. More...
class  llvm::json::ParseError
class  llvm::json::OStream
 json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahead of time. More...
struct  llvm::format_provider< llvm::json::Value >
 Allow printing json::Value with formatv(). More...

Namespaces

namespace  llvm
 This is an optimization pass for GlobalISel generic memory operations.
namespace  llvm::json

Functions

LLVM_ABI bool llvm::json::isUTF8 (llvm::StringRef S, size_t *ErrOffset=nullptr)
 Returns true if S is valid UTF-8, which is required for use as JSON.
LLVM_ABI std::string llvm::json::fixUTF8 (llvm::StringRef S)
 Replaces invalid UTF-8 sequences in S with the replacement character (U+FFFD).
template<typename T>
Value llvm::json::toJSON (const std::optional< T > &Opt)
LLVM_ABI bool llvm::json::operator== (const Object &LHS, const Object &RHS)
bool llvm::json::operator!= (const Object &LHS, const Object &RHS)
bool llvm::json::operator!= (const Array &L, const Array &R)
LLVM_ABI bool llvm::json::operator== (const Value &L, const Value &R)
bool llvm::json::operator!= (const Value &L, const Value &R)
bool llvm::json::operator== (const Array &L, const Array &R)
bool llvm::json::operator== (const ObjectKey &L, const ObjectKey &R)
bool llvm::json::operator!= (const ObjectKey &L, const ObjectKey &R)
bool llvm::json::operator< (const ObjectKey &L, const ObjectKey &R)
LLVM_ABI std::vector< const Object::value_type * > llvm::json::sortedElements (const Object &O)
bool llvm::json::fromJSON (const Value &E, std::string &Out, Path P)
bool llvm::json::fromJSON (const Value &E, int &Out, Path P)
bool llvm::json::fromJSON (const Value &E, int64_t &Out, Path P)
bool llvm::json::fromJSON (const Value &E, double &Out, Path P)
bool llvm::json::fromJSON (const Value &E, bool &Out, Path P)
bool llvm::json::fromJSON (const Value &E, unsigned int &Out, Path P)
bool llvm::json::fromJSON (const Value &E, uint64_t &Out, Path P)
bool llvm::json::fromJSON (const Value &E, std::nullptr_t &Out, Path P)
template<typename T>
bool llvm::json::fromJSON (const Value &E, std::optional< T > &Out, Path P)
template<typename T>
bool llvm::json::fromJSON (const Value &E, std::vector< T > &Out, Path P)
template<typename T>
bool llvm::json::fromJSON (const Value &E, std::map< std::string, T > &Out, Path P)
LLVM_ABI llvm::Expected< Valuellvm::json::parse (llvm::StringRef JSON)
 Parses the provided JSON source, or returns a ParseError.
template<typename T>
Expected< Tllvm::json::parse (const llvm::StringRef &JSON, const char *RootName="")
 Version of parse() that converts the parsed value to the type T.
llvm::raw_ostreamllvm::json::operator<< (llvm::raw_ostream &OS, const Value &V)
 Serializes this Value to JSON, writing it to the provided stream.

Variables

template<typename T>
constexpr bool llvm::json::is_uint_64_bit_v

Detailed Description

This file supports working with JSON data.

It comprises:

  • classes which hold dynamically-typed parsed JSON structures These are value types that can be composed, inspected, and modified. See json::Value, and the related types json::Object and json::Array.
  • functions to parse JSON text into Values, and to serialize Values to text. See parse(), operator<<, and format_provider.
  • a convention and helpers for mapping between json::Value and user-defined types. See fromJSON(), ObjectMapper, and the class comment on Value.
  • an output API json::OStream which can emit JSON without materializing all structures as json::Value.

Typically, JSON data would be read from an external source, parsed into a Value, and then converted into some native data structure before doing real work on it. (And vice versa when writing).

Other serialization mechanisms you may consider:

  • YAML is also text-based, and more human-readable than JSON. It's a more complex format and data model, and YAML parsers aren't ubiquitous. YAMLParser.h is a streaming parser suitable for parsing large documents (including JSON, as YAML is a superset). It can be awkward to use directly. YAML I/O (YAMLTraits.h) provides data mapping that is more declarative than the toJSON/fromJSON conventions here.
  • LLVM bitstream is a space- and CPU- efficient binary format. Typically it encodes LLVM IR ("bitcode"), but it can be a container for other data. Low-level reader/writer libraries are in Bitstream/Bitstream*.h

Definition in file JSON.h.