Skip to content

Commit

Permalink
Added command line option '-t, --parse-strict' to utility ujson-print.
Browse files Browse the repository at this point in the history
  • Loading branch information
alfmep committed Aug 4, 2022
1 parent 7e6c32e commit fca2c5c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
62 changes: 39 additions & 23 deletions utils/ujson-print.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Dan Arrhenius <dan@ultramarin.se>
* Copyright (C) 2021,2022 Dan Arrhenius <dan@ultramarin.se>
*
* This file is part of ujson.
*
Expand Down Expand Up @@ -33,14 +33,16 @@ struct appargs_t {
bool pretty;
bool escape_slash;
bool sorted;
bool strict;
bool parse_strict;
bool print_strict;
string filename;

appargs_t () {
pretty = true;
escape_slash = false;
sorted = false;
strict = true;
parse_strict = false;
print_strict = true;
}
};

Expand All @@ -49,21 +51,26 @@ struct appargs_t {
//------------------------------------------------------------------------------
static void print_usage_and_exit (std::ostream& out, int exit_code)
{
out << endl
<< "Parse a json file (in relaxed mode) and print it." << endl
<< endl
<< "Usage: " << prog_name << " [OPTIONS] [json-file]" << endl
<< endl
<< "In no file name is given, a json definition is read from standard input." << endl
<< "Options:" <<endl
<< " -c, --compact Compact output, no newlines or intendation." << endl
<< " -e, --escape-slash Forward slash characters(\"/\") are escaped to \"\\/\"." << endl
<< " -s, --sort Object members are listed in sorted order, not in natural order." << endl
<< " -r, --relaxed Numbers that are infinite or NaN are representated as numbers (-)inf and nan," << endl
<< " and not as type null as in the JSON specification." << endl
<< " -h, --help Print this help message and exit." << endl
<< endl;
exit (exit_code);
out << endl;
out << "Parse a JSON document and print it to standard output." << endl;
out << endl;
out << "Usage: " << prog_name << " [OPTIONS] [JSON-file]" << endl;
out << endl;
out << "In no file name is given, a JSON document is read from standard input." << endl;
out << "By default, the JSON document is parsed in relaxed mode." << endl;
out << "Options:" <<endl;
out << " -c, --compact Compact output, no newlines or intendation." << endl;
out << " -e, --escape-slash Forward slash characters(\"/\") are escaped to \"\\/\"." << endl;
out << " -s, --sort Object members are listed in sorted order, not in natural order." << endl;
out << " -r, --relaxed Print the JSON document in relaxed form." << endl;
out << " Numbers that are infinite or NaN are printed as (-)inf and nan," << endl;
out << " and not as type null as in the JSON specification." << endl;
out << " Object member names are printed without enclosing double quotes" << endl;
out << " when the names are in the following format: [_a-zA-Z][_a-zA-Z0-9]*" << endl;
out << " -t, --parse-strict Parse the JSON document in strict mode." << endl;
out << " -h, --help Print this help message and exit." << endl;
out << endl;
exit (exit_code);
}


Expand All @@ -76,13 +83,14 @@ static void parse_args (int argc, char* argv[], appargs_t& args)
{ "escape-slash", no_argument, 0, 'e'},
{ "sort", no_argument, 0, 's'},
{ "relaxed", no_argument, 0, 'r'},
{ "parse-strict", no_argument, 0, 't'},
{ "help", no_argument, 0, 'h'},
{ 0, 0, 0, 0}
};
const char* arg_format = "cesrh";
const char* arg_format = "cesrth";

while (1) {
int c = getopt_long (argc, argv, arg_format, long_options, NULL);
int c = getopt_long (argc, argv, arg_format, long_options, nullptr);
if (c == -1)
break;
switch (c) {
Expand All @@ -96,7 +104,10 @@ static void parse_args (int argc, char* argv[], appargs_t& args)
args.sorted = true;
break;
case 'r':
args.strict = false;
args.print_strict = false;
break;
case 't':
args.parse_strict = true;
break;
case 'h':
print_usage_and_exit (std::cout, 0);
Expand Down Expand Up @@ -133,15 +144,20 @@ int main (int argc, char* argv[])
// Parse json file
//
ujson::Json j;
auto instance = j.parse_string (json_desc, false);
auto instance = j.parse_string (json_desc, opt.parse_strict);
if (!instance.valid()) {
cerr << "Parse error: " << j.error() << endl;
exit (1);
}

// Print the parsed json instance
//
cout << instance.describe(opt.pretty, opt.strict, opt.escape_slash, opt.sorted) << endl;
cout << instance.describe (opt.pretty,
opt.print_strict,
opt.escape_slash,
opt.sorted,
true)
<< endl;

return 0;
}
5 changes: 4 additions & 1 deletion utils/ujson-print.man
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ When printing JSON strings, forward slash characters("/") are escaped to "\\/".
Object members are printed in sorted order, not in natural order. Sorting is made on the member name.
.TP
.B -r, --relaxed
Numbers that are infinite or NaN are printed as (-)inf and nan, and not as null as in the JSON specification.
Print the JSON document in relaxed form. Numbers that are infinite or NaN are printed as (-)inf and nan, and not as type null as in the JSON specification. Object member names are printed without enclosing them in double quotes("") when the object member names are in the following format: [_a-zA-Z][_a-zA-Z0-9]*. The exceptions are the object member names "true", "false", "null", "nan", "inf", and "infinite". Those object member names are always enclosed by double quotes("").
.TP
.B -t, --parse-strict
Parse the JSON document in strict mode.
.TP
.B -h, --help
Print help and exit.
Expand Down

0 comments on commit fca2c5c

Please sign in to comment.