JSON::Schema::Fit - adjust data structure according to json-schema
version 0.07
my $data = get_dirty_result(); # raw data: got { num => "1.99999999997", flag => "1", junk => {...} } my $bad_json = encode_json $data; # {"num":"1.99999999997","flag":"1","junk":{...}} # JSON::Schema-compatible my $schema = { type => 'object', additionalProperties => 0, properties => { num => { type => 'integer' }, flag => { type => 'boolean' }, }}; my $prepared_data = JSON::Schema::Fit->new()->get_adjusted($data, $schema); my $cool_json = encode_json $prepared_data; # {"num":2,"flag":true}
The main goal of this package is preparing data to be encoded as json according to schema.
Actions implemented: adjusting value type (number/string/boolean), rounding numbers, filtering hash keys.
Explicitly set type for boolean values to JSON::true / JSON::false
Default: 1
Explicitly set type for numeric and integer values
Round numbers according to 'multipleOf' schema value
Crop numbers accordingly with 'maximum', 'minimum' attributes.
Values outside these limits will be set to the defined in the maximum/minimum attributes.
Default: 0
Explicitly set type for strings
Filter out not allowed hash keys (where additionalProperties is false).
Fill missing values that have 'default' attribute in schema.
Replace incompatible with schema values with defaults (if defined). For now, only presence in 'enum' is checked.
my $jsf = JSON::Schema::Fit->new(booleans => 0);
Create a new JSON::Schema::Fit instance. See bellow for valid options.
JSON::Schema::Fit
Returns "semi-copy" of data structure with adjusted values. Original data is not affected.
Related modules: JSON, JSON::Schema.
Json-schema home: http://json-schema.org/
liosha <liosha@yandex-team.ru>
This software is copyright (c) 2019 by liosha.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install JSON::Schema::Fit, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::Schema::Fit
CPAN shell
perl -MCPAN -e shell install JSON::Schema::Fit
For more information on module installation, please visit the detailed CPAN module installation guide.