ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
erf_description::Writer Class Reference

#include <ERF_DescriptionWriter.H>

Collaboration diagram for erf_description::Writer:

Public Member Functions

 Writer (std::ostream &output, Format format)
 
void begin_object (const char *name)
 
void end_object ()
 
void finish (bool newline=true)
 
void value (const char *name, std::nullptr_t)
 
void value (const char *name, bool v)
 
void value (const char *name, int v)
 
void value (const char *name, const char *v)
 
void optional_string (const char *name, const char *v)
 
void strings (const char *name, const std::vector< std::string > &values)
 
template<class T , class Emit >
void objects (const char *name, const std::vector< T > &values, Emit emit)
 

Private Member Functions

void key (const char *name, bool object=false)
 
void end_value ()
 
void quoted (const char *value)
 

Private Attributes

std::ostream & os
 
bool yaml
 
std::vector< bool > first {true}
 

Detailed Description

Fixed-structure JSON/YAML writer with no parser dependency. YAML output uses block mappings, JSON flow arrays and double-quoted keys and strings, so YAML 1.1 parsers never read them as booleans or numbers.

Constructor & Destructor Documentation

◆ Writer()

erf_description::Writer::Writer ( std::ostream &  output,
Format  format 
)
inline
22  : os(output), yaml(format == Format::YAML)
23  {
24  if (!yaml) os << '{';
25  }
erf_description::Format format
Definition: ERF_InputPreflight.cpp:30
bool yaml
Definition: ERF_DescriptionWriter.H:105
std::ostream & os
Definition: ERF_DescriptionWriter.H:104

Member Function Documentation

◆ begin_object()

void erf_description::Writer::begin_object ( const char *  name)
inline
28  {
29  key(name, true);
30  os << (yaml ? '\n' : '{');
31  first.push_back(true);
32  }
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
std::vector< bool > first
Definition: ERF_DescriptionWriter.H:106
void key(const char *name, bool object=false)
Definition: ERF_DescriptionWriter.H:108
Here is the call graph for this function:

◆ end_object()

void erf_description::Writer::end_object ( )
inline
35  {
36  AMREX_ASSERT(first.size() > 1);
37  first.pop_back();
38  if (!yaml) os << '}';
39  }

◆ end_value()

void erf_description::Writer::end_value ( )
inlineprivate
123 { if (yaml) os << '\n'; }

Referenced by objects(), strings(), and value().

Here is the caller graph for this function:

◆ finish()

void erf_description::Writer::finish ( bool  newline = true)
inline
42  {
43  AMREX_ASSERT(first.size() == 1);
44  if (!yaml) { os << '}'; if (newline) os << '\n'; }
45  }

Referenced by objects().

Here is the caller graph for this function:

◆ key()

void erf_description::Writer::key ( const char *  name,
bool  object = false 
)
inlineprivate
109  {
110  if (yaml) {
111  os << std::string((first.size() - 1) * 2, ' ');
112  quoted(name);
113  os << ':';
114  if (!object) os << ' ';
115  } else {
116  if (!first.back()) os << ',';
117  quoted(name);
118  os << ':';
119  }
120  first.back() = false;
121  }
void quoted(const char *value)
Definition: ERF_DescriptionWriter.H:125

Referenced by begin_object(), objects(), strings(), and value().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ objects()

template<class T , class Emit >
void erf_description::Writer::objects ( const char *  name,
const std::vector< T > &  values,
Emit  emit 
)
inline
90  {
91  key(name);
92  os << '[';
93  for (std::size_t i = 0; i < values.size(); ++i) {
94  if (i) os << ", ";
95  Writer item(os, Format::JSON);
96  emit(item, values[i]);
97  item.finish(false);
98  }
99  os << ']';
100  end_value();
101  }
Writer(std::ostream &output, Format format)
Definition: ERF_DescriptionWriter.H:22
void end_value()
Definition: ERF_DescriptionWriter.H:123
Here is the call graph for this function:

◆ optional_string()

void erf_description::Writer::optional_string ( const char *  name,
const char *  v 
)
inline
72  {
73  value(name, v != nullptr && *v != '\0' ? v : nullptr);
74  }
void value(const char *name, std::nullptr_t)
Definition: ERF_DescriptionWriter.H:47
Here is the call graph for this function:

◆ quoted()

void erf_description::Writer::quoted ( const char *  value)
inlineprivate
126  {
127  constexpr char hex[] = "0123456789abcdef";
128  os << '"';
129  for (const auto* c = reinterpret_cast<const unsigned char*>(value); *c; ++c) {
130  if (*c == '"' || *c == '\\') {
131  os << '\\' << static_cast<char>(*c);
132  } else if (*c < 0x20 || *c == 0x7f) {
133  os << "\\u00" << hex[*c >> 4] << hex[*c & 15];
134  } else if (*c == 0xc2 && c[1] >= 0x80 && c[1] <= 0x9f) {
135  // C1 controls (incl. NEL, a YAML line break)
136  ++c;
137  os << "\\u00" << hex[*c >> 4] << hex[*c & 15];
138  } else if (*c == 0xe2 && c[1] == 0x80 && (c[2] == 0xa8 || c[2] == 0xa9)) {
139  // U+2028/9, line breaks to YAML 1.1 parsers
140  os << "\\u202" << (c[2] == 0xa8 ? '8' : '9');
141  c += 2;
142  } else {
143  os << static_cast<char>(*c);
144  }
145  }
146  os << '"';
147  }

Referenced by key(), strings(), and value().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ strings()

void erf_description::Writer::strings ( const char *  name,
const std::vector< std::string > &  values 
)
inline
77  {
78  key(name);
79  os << '[';
80  for (std::size_t i = 0; i < values.size(); ++i) {
81  if (i) os << ", ";
82  quoted(values[i].c_str());
83  }
84  os << ']';
85  end_value();
86  }
Here is the call graph for this function:

◆ value() [1/4]

void erf_description::Writer::value ( const char *  name,
bool  v 
)
inline
53  {
54  key(name); os << (v ? "true" : "false"); end_value();
55  }
Here is the call graph for this function:

◆ value() [2/4]

void erf_description::Writer::value ( const char *  name,
const char *  v 
)
inline
63  {
64  key(name);
65  if (v == nullptr) os << "null";
66  else quoted(v);
67  end_value();
68  }
Here is the call graph for this function:

◆ value() [3/4]

void erf_description::Writer::value ( const char *  name,
int  v 
)
inline
58  {
59  key(name); os << std::to_string(v); end_value();
60  }
Here is the call graph for this function:

◆ value() [4/4]

void erf_description::Writer::value ( const char *  name,
std::nullptr_t   
)
inline
48  {
49  key(name); os << "null"; end_value();
50  }

Referenced by optional_string(), and quoted().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ first

std::vector<bool> erf_description::Writer::first {true}
private

Referenced by begin_object(), end_object(), finish(), and key().

◆ os

std::ostream& erf_description::Writer::os
private

◆ yaml

bool erf_description::Writer::yaml
private

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