GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
JSON::Schema::Examples(3) User Contributed Perl Documentation JSON::Schema::Examples(3)

JSON::Schema::Examples - examples of JSON::Schema

The card schema at <http://json-schema.org/card> is tricky as it contains references to other schemas which need to be followed. But JSON::Schema handles it with ease...

 use JSON qw[to_json];
 use JSON::Schema;
 use LWP::Simple qw[get];
 
 # Here's some data...
 my $contact = to_json({
   fn        => 'Toby Inkster',
   nickname  => 'TOBYINK',
   email     => { value => 'tobyink@cpan.org' },
   adr       => { countryName => 'England', region => 'East Sussex' },
 });
 
 # Now we create our JSON Schema validator:
 my $card_schema = get('http://json-schema.org/card'); 
 my $validator = JSON::Schema->new($card_schema);
 
 # Validate:
 my $valid = $validator->validate($contact);
 if ($valid)
 {
   print "Yay!\n";
   exit;
 }
 
 # But it's not valid...
 foreach my $e ($valid->errors)
 {
   print "Naughty! $e\n";
 }

JSON::Schema has uses beyond JSON!

 use JSON:Schema;
 use DateTime;
 
 my $datetime_new_schema = {
   type => 'object', # i.e. a hashref
   properties => {
     year       => { type=>'number', minimum=>0, maximum=>9999 },
     month      => { type=>'number', minimum=>1, maximum=>12 },
     day        => { type=>'number', minimum=>1, maximum=>31 },
     hour       => { type=>'number', minimum=>0, maximum=>23 },
     minute     => { type=>'number', minimum=>0, maximum=>59 },
     second     => { type=>'number', minimum=>0, maximum=>61 },
     nanosecond => { type=>'number', minimum=>0, maximum=>999999999 },
     locale     => { type=>['DateTime::Locale','string'] },
     time_zone  => { type=>['DateTime::TimeZone', 'string'] },
     formatter  => { type=>'any' },
   }
 };
 my $validator = JSON::Schema->new($datetime_new_schema);
 
 my %params = get_user_input();
 my $valid  = $validator->validate(\%params);
 if ($valid)
 {
   my $dt = DateTime->new(%params);
   
   # do something interesting here with $dt...
 }
 else
 {
   die join("\n", "Invalid DateTime paramaters", $valid->errors);
 }

Toby Inkster <tobyink@cpan.org>.

Copyright 2010-2012 Toby Inkster.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.

<http://www.gnu.org/copyleft/fdl.html>.

This work is licenced under the Creative Commons Attribution-ShareAlike 2.0 UK: England & Wales License. To view a copy of this licence, visit <http://creativecommons.org/licenses/by-sa/2.0/uk/> or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
2014-09-11 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.