DAW JSON Link
Loading...
Searching...
No Matches
member_options

::# Member Options/Type Options

Many of the member types have options that can change how they are parsed. The options and their setters are located in the daw::json::options namespace and are placed into Options template parameter via a setter function that encodes the options into a json_options_t.


<tt>json_number</tt>

To set number options use the daw::json::options::number_opt( Flags... ) method.

<tt>LiteralAsStringOpt</tt>

Controls the ability to parse numbers that are encoded as strings. During serialization, Always emits the number in quotes. Never and Maybe emit ordinary finite numbers without quotes; Maybe only broadens the accepted input representation. Allowed NaN and infinity values are always emitted in quotes because they are not JSON number literals.

Values

  • Never - Never allow parsing this member as a string. It is a parser error if this member is encoded as a string
  • Maybe - Allow parsing this member as a string or number.
  • Always - Only allow parsing this member as a string. It is an error for this member to not be encoded as a string.

Default

  • Never

<tt>JsonRangeCheck</tt>

Values

  • Never - Do not attempt to check for narrowing when parsing integral types
  • CheckForNarrowing - Attempt to check for narrowing when parsing integral types

Default

  • Never

<tt>JsonNumberErrors</tt>

When outputting floating point numbers, control whether Inf/NaN values can be parsed/serialized. This requires that the LiteralAsString option be set to Maybe or Always

Values

  • None - Do not allow serialization/parsing of NaN/Inf
  • AllowNaN - Allow NaN to be expressed/parsed if number can be a string
  • AllowInf - Allow Inf/-Inf to be expressed/parsed if number can be a string
  • AllowNanInf - Allow NaN/Inf/-Inf to be expressed/parsed if number can be a string

Default

  • None

<tt>FPOutputFormat</tt>

Control the floating-point output format used by a floating-point json_number. This option only affects serialization.

Values

  • Auto - Round to Precision significant digits and use decimal notation for decimal-point positions from -4 through 6; use scientific notation outside that range.
  • Scientific - Use exponent notation <whole>[.fraction]e<exponent>. Its Precision is the number of digits after the decimal point.
  • Decimal - Use fixed-point notation <whole>[.fraction]. Its Precision is the number of digits after the decimal point.
  • Minimum - Round to Precision significant digits and use the shortest unpadded decimal representation.

For Auto and Minimum, Precision counts significant digits. For Decimal and Scientific, it counts digits after the decimal point. The default daw::max_value<unsigned> precision means no explicit digit limit; Auto and Minimum then preserve the shortest representation, while Decimal and Scientific still apply their selected notation. Rounding uses nearest-even rounding.

<tt>json_fp</tt> examples

json_fp is the floating-point equivalent of json_number with explicit format and precision template parameters. Its format is not supplied through number_opt:

using decimal_amount = daw::json::json_fp_no_name<
double, daw::json::options::FPOutputFormat::Decimal, 2>;
using scientific_amount = daw::json::json_fp_no_name<
double, daw::json::options::FPOutputFormat::Scientific, 3>;
using general_amount = daw::json::json_fp_no_name<
double, daw::json::options::FPOutputFormat::Auto, 4>;
daw::json::to_json<decimal_amount>( 12.345 ); // "12.35"
daw::json::to_json<scientific_amount>( 12.345 ); // "1.235e1"
daw::json::to_json<general_amount>( 12.345 ); // "12.35"

Named, nullable, and narrowing-checking variants use the same Format and Precision parameters:

using amount = daw::json::json_fp<
"amount", double, daw::json::options::FPOutputFormat::Decimal, 2>;
using optional_amount = daw::json::json_fp_null<
"amount", std::optional<double>,
daw::json::options::FPOutputFormat::Decimal, 2>;
using checked_amount = daw::json::json_checked_fp<
"amount", double, daw::json::options::FPOutputFormat::Decimal, 2>;

<tt>json_number</tt> versus <tt>json_fp</tt>

For floating-point values, json_number also reads JsonMember::fp_output_format, which is set through number_opt:

using number_decimal = daw::json::json_number_no_name<double,
daw::json::options::number_opt(
daw::json::options::FPOutputFormat::Decimal )>;

The distinction is that json_number selects the output format through its encoded options and uses the default precision. json_fp selects the format through its Format template parameter and additionally provides an explicit Precision template parameter. Internally, both mappings expose the selected format as JsonMember::fp_output_format; json_fp also exposes its precision to the serializer.

Default

  • Auto

<tt>json_fp</tt>

To set the encoded options for json_fp and json_checked_fp, use daw::json::options::fp_opt( Flags... ). These mappings accept:

  • LiteralAsStringOpt
  • JsonRangeCheck
  • JsonNumberErrors

Their meanings and defaults are the same as for json_number above. FPOutputFormat is instead supplied through the mapping's Format template parameter, followed by the Precision template parameter.

using quoted_amount = daw::json::json_fp_no_name<
double, daw::json::options::FPOutputFormat::Decimal, 2,
daw::json::options::fp_opt(
daw::json::options::LiteralAsStringOpt::Always )>;

JsonRangeCheck is accepted for consistency with the checked mapping aliases, but floating-point parsing currently does not consult it.


<tt>json_bool</tt>

To set bool options use the daw::json::options::bool_opt( Flags... ) method.

<tt>LiteralAsStringOpt</tt>

Controls the ability to parse booleans that are encoded as strings. During serialization, Always emits the boolean in quotes. Never and Maybe emit an unquoted JSON boolean; Maybe only broadens the accepted input representation.

Values

  • Never - Never allow parsing this member as a string. It is a parser error if this member is encoded as a string
  • Maybe - Allow parsing this member as a string or boolean literal.
  • Always - Only allow parsing this member as a string. It is an error for this member to not be encoded as a string.

Default

  • Never

<tt>json_string</tt>

To set string options use the daw::json::options::string_opt( Flags... ) method.

<tt>EightBitModes</tt>

Controls whether any string byte has the high bit set. If restricted, the serializer escapes bytes with the high bit set and the parser rejects them. This allows 7-bit JSON encoding.

Values

  • DisallowHigh - Escape any character with the high bit set and throw when encountered during parse
  • AllowFull - Allow the full 8 bits in output without escaping

Default

  • AllowFull

<tt>EscapeValidUTF8</tt>

Controls whether to_json validates and JSON-escapes the value of a json_string mapping. This option affects serialization only; it does not change how from_json parses the string.

Values

  • Validate - Validate the UTF-8 input and escape quotation marks, backslashes, control characters, and any characters required by the active output restrictions.
  • AssumeValid - Write the value directly between quotation marks. The caller guarantees that the value is valid UTF-8 and is already correctly escaped as JSON string content.

Default

  • Validate

AssumeValid avoids UTF-8 validation and escaping and can substantially improve serialization performance for trusted data. Supplying unescaped quotation marks, backslashes, control characters, or invalid UTF-8 can produce invalid JSON. Because the bytes are written directly, EightBitModes and global restricted-string output processing are not applied to that value.

using trusted_string = daw::json::json_string_no_name<
std::string_view,
daw::json::options::string_opt(
daw::json::options::EscapeValidUTF8::AssumeValid )>;

<tt>json_string_raw</tt>

To set raw string options use the daw::json::options::string_raw_opt( Flags... ) method.

<tt>EightBitModes</tt>

Controls whether any string byte has the high bit set during serialization. If restricted, serialization rejects bytes with the high bit set. Raw-string parsing preserves the input bytes and does not inspect this option.

Values

  • DisallowHigh - Reject any byte with the high bit set during serialization
  • AllowFull - Allow the full 8 bits in output without escaping

Default

  • AllowFull

<tt>AllowEscapeCharacter</tt>

In RAW String processing, if we know that there are no escaped double quotes \"</tt> we can stop at the first double quote. This allows faster string parsing @subsubsection autotoc_md137 Values * <tt>Allow</tt> - Full string processing to skip escaped characters * <tt>NoEscapedDblQuote</tt> - There will never be a \" sequence inside the string. This allows faster parsing @subsubsection autotoc_md138 Default * <tt>Allow</tt> <hr> @section autotoc_md139 <tt>json_custom</tt> To set json_custom options use the <tt>daw::json::options::json_custom_opt( Flags... )</tt> method. See @ref "/home/runner/work/daw_json_link/daw_json_link/docs/cookbook/custom_types.md" "Custom Types and Output" for complete converter and serialization examples.

<tt>JsonCustomTypes</tt>

Custom JSON types can be strings, unquoted literals, or any JSON value.

Values

  • String - The parser expects a JSON string and passes its unquoted contents to the converter. Serialization copies the converter output between double quotes without escaping or validating it.
  • Literal - The parser expects a JSON number, boolean, or null. Serialization copies the converter output verbatim without validation.
  • Any (Experimental) - The parser passes any JSON value, excluding leading whitespace, to the converter. JSON strings retain their quotes. Serialization copies the converter output verbatim without adding quotes, escaping, or validation. Parse-time validation follows the selected CheckedParseMode. Any is suitable for constructing a json_value to allow ad hoc parsing if json_raw is not suitable.

Default

  • String