header_utils
Loading...
Searching...
No Matches
Wilson

Wilson is a superset of JSON with a strongly permissive syntax. More...

Functions

auto ghassanpl::formats::wilson::parse (std::string_view wilson_str) -> expected< wilson, wilson_parsing_error >
 Parses any Wilson value.
auto ghassanpl::formats::wilson::parse_object (std::string_view wilson_str, char closing_char='}') -> expected< wilson, wilson_parsing_error >
 Parses a set of key-value pairs, until end of string or encountering the closing_char.
auto ghassanpl::formats::wilson::parse_array (std::string_view wilson_str, char closing_char=']') -> expected< wilson, wilson_parsing_error >
 Parses an array of Wilson values, until end of string or encountering the closing_char.
auto ghassanpl::formats::wilson::parse_word_or_string (std::string_view wilson_str) -> expected< wilson, wilson_parsing_error >
 Parses: a word as a string value or bool/null value; a "string literal" as a string value.
auto ghassanpl::formats::wilson::parse_string_value (std::string_view wilson_str) -> expected< std::string, wilson_parsing_error >
 Parses a word or a string literal a string value.
auto ghassanpl::formats::wilson::parse_string_literal (std::string_view wilson_str) -> expected< std::string, wilson_parsing_error >
 Parses a string literal as a string value.
auto ghassanpl::formats::wilson::consume_string_literal (std::string_view &_str)
 Consumes a string literal returning a string value.
auto ghassanpl::formats::wilson::consume_string_value (std::string_view &str)
 Consumes a word or a string literal returning a string value.
auto ghassanpl::formats::wilson::consume_word_or_string (std::string_view &str)
 Consumes: a word, returning a string/bool/null value; or a "string literal", returning a string value.
auto ghassanpl::formats::wilson::consume_object (std::string_view &wilson_str, char closing_char='}') -> expected< wilson, wilson_parsing_error >
auto ghassanpl::formats::wilson::consume_array (std::string_view &wilson_str, char closing_char=']') -> expected< wilson, wilson_parsing_error >
auto ghassanpl::formats::wilson::consume_value (std::string_view &wilson_str) -> expected< wilson, wilson_parsing_error >
template<typename OUTFUNC>
void ghassanpl::formats::wilson::output (OUTFUNC &&out, wilson const &value, output_parameters const &parameters={}, size_t indent=0)
 Outputs value formatted as a string using the out function which should take a string_view to append to the output.
void ghassanpl::formats::wilson::output_to_stream (std::ostream &strm, wilson const &value, output_parameters const &parameters={})
 Outputs value formatted as a string to the strm output stream.
std::string ghassanpl::formats::wilson::to_string (wilson const &value, output_parameters const &parameters={})
 Returns value formatted as a string.
expected< wilson, wilson_parsing_error > ghassanpl::formats::wilson::load_file (std::filesystem::path const &from)
 Loads a Wilson value from a file.
wilson ghassanpl::formats::wilson::try_load_file (std::filesystem::path const &from, wilson or_json=json::empty_json)
 Loads a Wilson value from a file, or returns or_json on failure.
void ghassanpl::formats::wilson::save_file (std::filesystem::path const &to, wilson const &j, output_parameters const &parameters={})
 Saves a Wilson value to a file formatted as a string.

Detailed Description

Wilson is a superset of JSON with a strongly permissive syntax.

Uses the nlohmann::json library to store parsed values.

Differences from JSON:

  • supports line comments - # starts a comment that runs until \n is encountered; comments are treated as whitespace
  • strings can be delimited by both ' and " characters (they must be paired tho)
  • single-word strings do not need to start with ' or " (unless you want a "null", "true" or "false" string) - a sequence of characters matching /[A-Za-z_][A-Za-z0-9]*‍/ will be considered a string
  • both null and nil will be treated as the null value
  • numbers are parsed using the C++ std::from_chars function, as doubles, with all that entails
  • arrays can be delimited by both (/) and [/] bracket pairs (they must be paired tho)
  • array elements and object pairs can be separated by the , or ; characters
  • array element and object pair separated are optional
  • keys and values can be separated by the : or = characters
  • key and value separators are also optional
  • in an object pair, if a value is not given, and the object pair separator like , or ; or the end of the object is encountered, the value will be considered to be true
  • trailing separators are permitted
  • an ending array/object delimiter (}, ), ] etc) is not required if end-of-string is encountered
  • additional escape sequences are recognized
    • "\'" for '-delimited strings
    • "\0" for embedded zeroes
    • "\onnn" for C-style octal escapes (but requires exactly 3 digits)
    • "\xnn" for C-style hex escapes (but requires exactly 2 digits)
    • "\unnnn" for C++-style encoding of Unicode codepoints from U+0000 to U+FFFF as UTF-8
    • "\Unnnnnnnn" for C++-style encoding of all Unicode codepoints as UTF-8 Therefore this is a valid Wilson document:
{
# comment
array = ( 10 20 30 ) # another comment
"another array": [10; 20; 30]
key value # yes, this is a "key":"value" pair
true_key; # this will be a "true_key":true pair
also_true_key
}

equivalent to:

{
"array": [10, 20, 30],
"another array": [10, 20, 30],
"key": "value",
"true_key": true,
"also_true_key": true
}

Function Documentation

◆ consume_array()

expected< wilson, wilson_parsing_error > ghassanpl::formats::wilson::consume_array ( std::string_view & wilson_str,
char closing_char = ']' ) -> expected< wilson, wilson_parsing_error >
inline

Definition at line 145 of file wilson.h.

◆ consume_object()

expected< wilson, wilson_parsing_error > ghassanpl::formats::wilson::consume_object ( std::string_view & wilson_str,
char closing_char = '}' ) -> expected< wilson, wilson_parsing_error >
inline

Definition at line 213 of file wilson.h.

◆ consume_string_literal()

expected< std::string, wilson_parsing_error > ghassanpl::formats::wilson::consume_string_literal ( std::string_view & _str) -> expected< std::string, wilson_parsing_error >
inline

Consumes a string literal returning a string value.

Definition at line 169 of file wilson.h.

◆ consume_string_value()

expected< std::string, wilson_parsing_error > ghassanpl::formats::wilson::consume_string_value ( std::string_view & str) -> expected< std::string, wilson_parsing_error >
inline

Consumes a word or a string literal returning a string value.

Definition at line 180 of file wilson.h.

◆ consume_value()

expected< wilson, wilson_parsing_error > ghassanpl::formats::wilson::consume_value ( std::string_view & wilson_str) -> expected< wilson, wilson_parsing_error >
inline

Definition at line 262 of file wilson.h.

◆ consume_word_or_string()

expected< wilson, wilson_parsing_error > ghassanpl::formats::wilson::consume_word_or_string ( std::string_view & str) -> expected< wilson, wilson_parsing_error >
inline

Consumes: a word, returning a string/bool/null value; or a "string literal", returning a string value.

Definition at line 197 of file wilson.h.

◆ load_file()

expected< wilson, wilson_parsing_error > ghassanpl::formats::wilson::load_file ( std::filesystem::path const & from)
inline

Loads a Wilson value from a file.

Definition at line 461 of file wilson.h.

◆ output()

template<typename OUTFUNC>
void ghassanpl::formats::wilson::output ( OUTFUNC && out,
wilson const & value,
output_parameters const & parameters = {},
size_t indent = 0 )

Outputs value formatted as a string using the out function which should take a string_view to append to the output.

Definition at line 391 of file wilson.h.

◆ output_to_stream()

void ghassanpl::formats::wilson::output_to_stream ( std::ostream & strm,
wilson const & value,
output_parameters const & parameters = {} )
inline

Outputs value formatted as a string to the strm output stream.

Definition at line 105 of file wilson.h.

◆ parse()

auto ghassanpl::formats::wilson::parse ( std::string_view wilson_str) -> expected< wilson, wilson_parsing_error >
inline

Parses any Wilson value.

Definition at line 291 of file wilson.h.

◆ parse_array()

auto ghassanpl::formats::wilson::parse_array ( std::string_view wilson_str,
char closing_char = ']' ) -> expected< wilson, wilson_parsing_error >
inline

Parses an array of Wilson values, until end of string or encountering the closing_char.

Definition at line 302 of file wilson.h.

◆ parse_object()

auto ghassanpl::formats::wilson::parse_object ( std::string_view wilson_str,
char closing_char = '}' ) -> expected< wilson, wilson_parsing_error >
inline

Parses a set of key-value pairs, until end of string or encountering the closing_char.

Definition at line 297 of file wilson.h.

◆ parse_string_literal()

auto ghassanpl::formats::wilson::parse_string_literal ( std::string_view wilson_str) -> expected< std::string, wilson_parsing_error >
inline

Parses a string literal as a string value.

Definition at line 317 of file wilson.h.

◆ parse_string_value()

auto ghassanpl::formats::wilson::parse_string_value ( std::string_view wilson_str) -> expected< std::string, wilson_parsing_error >
inline

Parses a word or a string literal a string value.

Definition at line 312 of file wilson.h.

◆ parse_word_or_string()

auto ghassanpl::formats::wilson::parse_word_or_string ( std::string_view wilson_str) -> expected< wilson, wilson_parsing_error >
inline

Parses: a word as a string value or bool/null value; a "string literal" as a string value.

Definition at line 307 of file wilson.h.

◆ save_file()

void ghassanpl::formats::wilson::save_file ( std::filesystem::path const & to,
wilson const & j,
output_parameters const & parameters = {} )
inline

Saves a Wilson value to a file formatted as a string.

Definition at line 120 of file wilson.h.

◆ to_string()

std::string ghassanpl::formats::wilson::to_string ( wilson const & value,
output_parameters const & parameters = {} )
inline

Returns value formatted as a string.

Definition at line 454 of file wilson.h.

◆ try_load_file()

wilson ghassanpl::formats::wilson::try_load_file ( std::filesystem::path const & from,
wilson or_json = json::empty_json )
inline

Loads a Wilson value from a file, or returns or_json on failure.

Definition at line 470 of file wilson.h.