37#include <drain/Sprinter.h>
40#include "TextReader.h"
41#include "TreeUnordered.h"
73 void readTree(T & tree, std::istream & istr);
110 void handleValue(std::istream & istr, T & tree,
const std::string & key){
124 const bool DATA = !tree.data.empty();
126 const bool CHILDREN = tree.hasChildren();
136 if (! (DATA||CHILDREN)){
140 ostr << layout.mapChars.prefix <<
" " << layout.mapChars.suffix;
143 ostr << layout.mapChars.prefix <<
' ' << layout.mapChars.suffix;
149 const std::string pad(2*indent,
' ');
162 ostr << layout.mapChars.prefix;
168 for (
const auto & entry: tree){
174 sep = layout.mapChars.separator;
179 ostr << layout.pairChars.prefix;
180 ostr <<
'"' << entry.first <<
'"' << layout.pairChars.separator <<
' ';
183 ostr << layout.pairChars.suffix;
189 ostr << pad << layout.mapChars.suffix;
201 log.
error(
"File read error");
210 log.
error(
"Syntax error: read '", c,
"' when expecting '{'");
218 bool completed =
true;
245 log.
error() <<
"Syntax error: read \"" << key <<
"\" followed by '" << c <<
"' when expecting object {...}, string \"...\", array [...], or number" << log.endl;
251 log.
warn(
"empty section after key=", key);
261 log.
error(
"Syntax error: char '", c,
"' (", (
short)c,
"), expected '\"', '}' or ','");
271typedef drain::UnorderedMultiTree<drain::Variable,true> JSONtree;
283 JSONtree & child = dst[key];
287 char c = istr.peek();
336 int visitPrefix(
const JSONtree & tree,
const JSONtree::path_t & path){
337 const JSONtree & t = tree(path);
338 if (t.hasChildren() && !t.data.empty()){
339 std::cout <<
"JSON violation at '" << path <<
"': has child nodes and also non-empty data: " << t.data <<
'\n';
Utility for extracting JSON-compatible strings, numbers or arrays of numbers in text data.
Definition JSON.h:60
static void handleValue(std::istream &istr, T &tree, const std::string &key)
In reading trees.
Definition JSON.h:110
static void readValue(std::istream &istr, Castable &v, bool keepType=false)
Read a value (JSON syntax). Read stream until a value has been extracted, with type recognition.
Definition JSON.cpp:52
static std::ostream & treeToStream(std::ostream &ostr, const T &tree, const drain::SprinterLayout &layout=drain::Sprinter::jsonLayout, short indent=0)
Write a tree into an output stream using JSON layout by default.
Definition JSON.h:122
static void readTree(T &tree, std::istream &istr)
Reads and parses a JSON file.
Definition JSON.h:195
static void readArray(const std::string &s, Castable &v)
Given comma-separated string of values, assign them to variable of minimum compatible type.
Definition JSON.cpp:116
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:440
Logger & error(const TT &... args)
Echoes.
Definition Log.h:426
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:521
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:423
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition Sprinter.h:224
static char skipWhiteSpace(std::istream &istr)
Definition TextReader.h:90
static std::string scanSegment(std::istream &istr, const std::string &endChars)
Read input stream until any char in endChars is encountered. The end char will not be included,...
Definition TextReader.h:59
Default implementation of a tree visitor (concept) compatible TreeUtils::traverser()
Definition TreeUtils.h:266
Definition Sprinter.h:136