The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Sah::Schemas::JSON - Various schemas related to JSON

VERSION

This document describes version 0.007 of Sah::Schemas::JSON (from Perl distribution Sah-Schemas-JSON), released on 2022-11-15.

SAH SCHEMAS

The following schemas are included in this distribution:

  • any_from_json

    A data structure, coerced from JSON string.

    You can use this schema if you want to accept any data (a data structure or simple scalar), but if user supplies a defined string e.g. in a command-line script as command-line argument or option, the string will be assumed to be a JSON-encoded value and decoded. Data will not be valid if the string does not contain valid JSON.

    Thus, if you want to supply a string, you have to JSON-encode it.

  • array_from_json

    Array, coercible from JSON string.

    You can use this schema if you want to accept an array, but if user supplies a string e.g. in a command-line script as command-line argument or option, the string will be coerced into array if the string contains a JSON-encoded array. Data will not be valid if the string does not contain valid JSON.

    Note that array data is accepted, unlike the json_str::array schema which only accepts JSON-encoded array in string form.

  • hash_from_json

    Hash, coerced from JSON string.

    You can use this schema if you want to accept a hash, but if user supplies a string e.g. in a command-line script as command-line argument or option, the string will be coerced into hash if the string contains a JSON-encoded object (hash). Data will not be valid if the string does not contain valid JSON.

    Note that hash data is accepted, unlike the json_str::hash schema which only accepts hash in JSON-encoded string form.

  • json_or_str

    A JSON-encoded data or string.

    You can use this schema if you want to accept any data (a data structure or simple scalar), and if user supplies a defined string e.g. in a command-line script as command-line argument or option, it will be tried to be JSON-decoded first. If the string does not contain valid JSON, it will be left as-is as string.

    This schema is convenient on the command-line where you want to accept data structure via command-line argument or option. But you have to be careful when you want to pass a string like null, true, false; you have to quote it to "null", "true", "false" to prevent it being decoded into undef or boolean values.

    See also related schema: json_str, str::encoded_json, str::escaped_json.

  • json_str

    A string that contains valid JSON.

    This schema can be used if you want to accept a string that contains valid JSON. The JSON string will not be decoded (e.g. a JSON-encoded array will not beome an array) but you know that the string contains a valid JSON. Data will not be valid if the string does not contain valid JSON.

    See also related schema: json_or_str, str::encoded_json, str::escaped_json.

  • json_str::array

    A string that contains valid JSON and the JSON encodes an array.

    This schema is like the json_str schema: it accepts a string that contains valid JSON. The JSON string will not be decoded but you know that the string contains a valid JSON. In addition to that, the JSON must encode an array. Data will not be valid if it is not a valid JSON or if the JSON is not an array.

    Note that unlike the array_from_json schema, an array data is not accepted by this schema. Data must be a string.

  • json_str::hash

    A string that contains valid JSON and the JSON encodes a hash (JavaScript object).

    This schema is like the json_str schema: it accepts a string that contains valid JSON. The JSON string will not be decoded but you know that the string contains a valid JSON. In addition to that, the JSON must encode a hash (JavaScript object). Data will not be valid if it is not a valid JSON or if the JSON is not a hash.

    Note that unlike the hash_from_json schema, a hash data is not accepted by this schema. Data must be a string.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Sah-Schemas-JSON.

SOURCE

Source repository is at https://github.com/perlancar/perl-Sah-Schemas-JSON.

SEE ALSO

Sah::Schemas::Str

Sah - schema specification

Data::Sah - Perl implementation of Sah

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTING

To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 2022, 2021 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-JSON

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.