Euphoria
json.h File Reference
#include <optional>
#include <set>
#include "jsonh/jsonh.h"
#include "log/log.h"
#include "assert/assert.h"
#include "base/result.h"

Go to the source code of this file.

Classes

struct  eu::io::Json
 
struct  eu::io::JsonError
 
struct  eu::io::ObjectQuery
 

Namespaces

 eu
 
 eu::io
 

Macros

#define JSON_PARSE_FUNC(TYPE)    bool parse(::eu::log::Logger* log, TYPE* owner, jsonh::Value root_val, const jsonh::Document* doc)
 
#define JSON_EXPECT(x, msg)
 
#define JSON_BEGIN_ENUM(TYPE)
 
#define JSON_ENUM_VAL(VAL)
 
#define JSON_END_ENUM()
 
#define JSON_BEGIN_OBJECT()
 
#define JSON_VAL_X(TYPE, PROP, REQUIRED)
 
#define JSON_VAL_OBJ_X(TYPE, PROP, REQUIRED, CON)
 
#define JSON_VAL_OBJ(TYPE, PROP)   JSON_VAL_OBJ_X(TYPE, PROP, true, )
 
#define JSON_VAL_OPT_OBJ(TYPE, PROP)   JSON_VAL_OBJ_X(TYPE, PROP, false, )
 
#define JSON_VAL_OPT_OBJ_CON(TYPE, PROP, CON)   JSON_VAL_OBJ_X(TYPE, PROP, false, CON)
 
#define JSON_VAL(TYPE, PROP)   JSON_VAL_X(TYPE, PROP, true)
 
#define JSON_OPT_VAL(TYPE, PROP)   JSON_VAL_X(TYPE, PROP, false)
 
#define JSON_ARRAY(TYPE, PROP)
 
#define JSON_ARRAY_PROP_CON(TYPE, PROP, CON)
 
#define JSON_ARRAY_PROP(TYPE, PROP)    JSON_ARRAY_PROP_CON(TYPE, PROP, )
 
#define JSON_END_OBJECT()
 

Typedefs

using eu::io::JsonResult = Result< Json, JsonError >
 

Enumerations

enum class  eu::io::JsonErrorType { eu::io::file_error , eu::io::parse_error }
 

Functions

std::string eu::io::get_string_from_path_for_debugging (FileSystem *fs, const FilePath &p)
 
JsonResult eu::io::read_json_file (FileSystem *fs, const FilePath &file_name)
 
std::string eu::io::could_be (const std::string &invalid_value, const std::vector< std::string > &possible_values)
 

Macro Definition Documentation

◆ JSON_ARRAY

#define JSON_ARRAY (   TYPE,
  PROP 
)
Value:
do{\
auto dnu_val = object.get(#PROP);\
JSON_EXPECT(dnu_val, "missing property " #PROP);\
auto* dnu_array = dnu_val->AsArray(doc);\
JSON_EXPECT(dnu_array, #PROP " is not a array");\
for (const auto dnu_json : dnu_array->array)\
{\
TYPE dnu_prop;\
if (false == parse(log, &dnu_prop, dnu_json, doc))\
{\
return false;\
}\
owner->PROP.emplace_back(std::move(dnu_prop));\
}\
} while(false)

Definition at line 132 of file json.h.

◆ JSON_ARRAY_PROP

#define JSON_ARRAY_PROP (   TYPE,
  PROP 
)     JSON_ARRAY_PROP_CON(TYPE, PROP, )

Definition at line 163 of file json.h.

◆ JSON_ARRAY_PROP_CON

#define JSON_ARRAY_PROP_CON (   TYPE,
  PROP,
  CON 
)
Value:
do{\
auto dnu_val = object.get(#PROP);\
JSON_EXPECT(dnu_val, "missing property " #PROP);\
auto* dnu_array = dnu_val->AsArray(doc);\
JSON_EXPECT(dnu_array, #PROP " is not a array");\
for (const auto dnu_json : dnu_array->array)\
{\
const auto* dnu_prop = dnu_json.As ## TYPE(doc);\
JSON_EXPECT(dnu_prop, #PROP " is not a " #TYPE);\
owner->PROP.emplace_back(CON(dnu_prop->value));\
}\
} while(false)

Definition at line 149 of file json.h.

◆ JSON_BEGIN_ENUM

#define JSON_BEGIN_ENUM (   TYPE)
Value:
const auto* dnu_val = root_val.AsString(doc);\
JSON_EXPECT(dnu_val, "Expected string value for " #TYPE);\
const std::string dnu_enum_name = #TYPE; \
using dnu_enum_type = TYPE; \
std::vector<std::string> dnu_values

Definition at line 65 of file json.h.

◆ JSON_BEGIN_OBJECT

#define JSON_BEGIN_OBJECT ( )
Value:
const auto* root = root_val.AsObject(doc);\
JSON_EXPECT(root, "root is not a object");\
auto object = ::eu::io::ObjectQuery{ root }

Definition at line 86 of file json.h.

◆ JSON_END_ENUM

#define JSON_END_ENUM ( )
Value:
log->error(fmt::format("{} was invalid for {}, could be {}",\
dnu_val->value, dnu_enum_name,\
eu::io::could_be(dnu_val->value, dnu_values)));\
return false
std::string could_be(const std::string &invalid_value, const std::vector< std::string > &possible_values)
Definition: json.cc:89

Definition at line 80 of file json.h.

◆ JSON_END_OBJECT

#define JSON_END_OBJECT ( )
Value:
const auto missing = object.get_missing_errors_message();\
if (missing)\
{\
log->error(*missing);\
return false;\
}\
return true

Definition at line 166 of file json.h.

◆ JSON_ENUM_VAL

#define JSON_ENUM_VAL (   VAL)
Value:
if(dnu_val->value == #VAL) {\
*owner = dnu_enum_type::VAL;\
return true; \
} \
else { \
dnu_values.emplace_back(#VAL);\
}

Definition at line 72 of file json.h.

◆ JSON_EXPECT

#define JSON_EXPECT (   x,
  msg 
)
Value:
do {\
if (!(x))\
{\
log->error(msg);\
return false;\
}\
} while(false)

Definition at line 56 of file json.h.

◆ JSON_OPT_VAL

#define JSON_OPT_VAL (   TYPE,
  PROP 
)    JSON_VAL_X(TYPE, PROP, false)

Definition at line 129 of file json.h.

◆ JSON_PARSE_FUNC

#define JSON_PARSE_FUNC (   TYPE)     bool parse(::eu::log::Logger* log, TYPE* owner, jsonh::Value root_val, const jsonh::Document* doc)

Definition at line 53 of file json.h.

◆ JSON_VAL

#define JSON_VAL (   TYPE,
  PROP 
)    JSON_VAL_X(TYPE, PROP, true)

Definition at line 128 of file json.h.

◆ JSON_VAL_OBJ

#define JSON_VAL_OBJ (   TYPE,
  PROP 
)    JSON_VAL_OBJ_X(TYPE, PROP, true, )

Definition at line 124 of file json.h.

◆ JSON_VAL_OBJ_X

#define JSON_VAL_OBJ_X (   TYPE,
  PROP,
  REQUIRED,
  CON 
)
Value:
do {\
auto dnu_json = object.get(#PROP);\
if (dnu_json)\
{\
TYPE dnu_prop = {};\
if (false == parse(log, &dnu_prop, *dnu_json, doc))\
{\
return false;\
}\
owner->PROP = CON(std::move(dnu_prop));\
}\
else if constexpr(REQUIRED)\
{\
object.required.emplace_back(#PROP);\
}\
} while(false)

Definition at line 106 of file json.h.

◆ JSON_VAL_OPT_OBJ

#define JSON_VAL_OPT_OBJ (   TYPE,
  PROP 
)    JSON_VAL_OBJ_X(TYPE, PROP, false, )

Definition at line 125 of file json.h.

◆ JSON_VAL_OPT_OBJ_CON

#define JSON_VAL_OPT_OBJ_CON (   TYPE,
  PROP,
  CON 
)    JSON_VAL_OBJ_X(TYPE, PROP, false, CON)

Definition at line 126 of file json.h.

◆ JSON_VAL_X

#define JSON_VAL_X (   TYPE,
  PROP,
  REQUIRED 
)
Value:
do {\
auto dnu_json = object.get(#PROP);\
if (dnu_json)\
{\
auto* dnu_prop = dnu_json->As ## TYPE(doc);\
JSON_EXPECT(dnu_prop, #PROP " is not a " #TYPE);\
owner->PROP = dnu_prop->value;\
}\
else if constexpr(REQUIRED)\
{\
object.required.emplace_back(#PROP);\
}\
} while(false)

Definition at line 91 of file json.h.