<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package SQL::Translator::Parser::JSON;

use strict;
use warnings;
our $VERSION = '1.00';

use SQL::Translator::Schema;
use SQL::Translator::Utils qw(header_comment);
use Data::Dumper;
use JSON;

sub parse {
    my ($translator, $data) = @_;
    $data = from_json($data);
    $data = $data-&gt;{'schema'};

    warn "JSON data:", Dumper($data) if $translator-&gt;debug;

    my $schema = $translator-&gt;schema;

    #
    # Tables
    #
    my @tables =
        map   { $data-&gt;{'tables'}{ $_-&gt;[1] } }
        sort  { $a-&gt;[0] &lt;=&gt; $b-&gt;[0] }
        map   { [ $data-&gt;{'tables'}{ $_ }{'order'} || 0, $_ ] }
        keys %{ $data-&gt;{'tables'} }
    ;

    for my $tdata ( @tables ) {

        my $table = $schema-&gt;add_table(
            map {
              $tdata-&gt;{$_} ? ($_ =&gt; $tdata-&gt;{$_}) : ()
            } (qw/name extra options/)
        ) or die $schema-&gt;error;

        my @fields =
            map   { $tdata-&gt;{'fields'}{ $_-&gt;[1] } }
            sort  { $a-&gt;[0] &lt;=&gt; $b-&gt;[0] }
            map   { [ $tdata-&gt;{'fields'}{ $_ }{'order'}, $_ ] }
            keys %{ $tdata-&gt;{'fields'} }
        ;

        for my $fdata ( @fields ) {
            $table-&gt;add_field( %$fdata ) or die $table-&gt;error;
            $table-&gt;primary_key( $fdata-&gt;{'name'} )
                if $fdata-&gt;{'is_primary_key'};
        }

        for my $idata ( @{ $tdata-&gt;{'indices'} || [] } ) {
            $table-&gt;add_index( %$idata ) or die $table-&gt;error;
        }

        for my $cdata ( @{ $tdata-&gt;{'constraints'} || [] } ) {
            $table-&gt;add_constraint( %$cdata ) or die $table-&gt;error;
        }
    }

    #
    # Views
    #
    my @views =
        map   { $data-&gt;{'views'}{ $_-&gt;[1] } }
        sort  { $a-&gt;[0] &lt;=&gt; $b-&gt;[0] }
        map   { [ $data-&gt;{'views'}{ $_ }{'order'}, $_ ] }
        keys %{ $data-&gt;{'views'} }
    ;

    for my $vdata ( @views ) {
        $schema-&gt;add_view( %$vdata ) or die $schema-&gt;error;
    }

    #
    # Triggers
    #
    my @triggers =
        map   { $data-&gt;{'triggers'}{ $_-&gt;[1] } }
        sort  { $a-&gt;[0] &lt;=&gt; $b-&gt;[0] }
        map   { [ $data-&gt;{'triggers'}{ $_ }{'order'}, $_ ] }
        keys %{ $data-&gt;{'triggers'} }
    ;

    for my $tdata ( @triggers ) {
        $schema-&gt;add_trigger( %$tdata ) or die $schema-&gt;error;
    }

    #
    # Procedures
    #
    my @procedures =
        map   { $data-&gt;{'procedures'}{ $_-&gt;[1] } }
        sort  { $a-&gt;[0] &lt;=&gt; $b-&gt;[0] }
        map   { [ $data-&gt;{'procedures'}{ $_ }{'order'}, $_ ] }
        keys %{ $data-&gt;{'procedures'} }
    ;

    for my $tdata ( @procedures ) {
        $schema-&gt;add_procedure( %$tdata ) or die $schema-&gt;error;
    }

    if ( my $tr_data = $data-&gt;{'translator'} ) {
        $translator-&gt;add_drop_table( $tr_data-&gt;{'add_drop_table'} );
        $translator-&gt;filename( $tr_data-&gt;{'filename'} );
        $translator-&gt;no_comments( $tr_data-&gt;{'no_comments'} );
        $translator-&gt;parser_args( $tr_data-&gt;{'parser_args'} );
        $translator-&gt;producer_args( $tr_data-&gt;{'producer_args'} );
        $translator-&gt;parser_type( $tr_data-&gt;{'parser_type'} );
        $translator-&gt;producer_type( $tr_data-&gt;{'producer_type'} );
        $translator-&gt;show_warnings( $tr_data-&gt;{'show_warnings'} );
        $translator-&gt;trace( $tr_data-&gt;{'trace'} );
    }

    return 1;
}

1;

__END__

=head1 NAME

SQL::Translator::Parser::JSON - Parse a JSON representation of a schema

=head1 SYNOPSIS

    use SQL::Translator;

    my $translator = SQL::Translator-&gt;new(parser =&gt; "JSON");

=head1 DESCRIPTION

C&lt;SQL::Translator::Parser::JSON&gt; parses a schema serialized with JSON.

=head1 AUTHORS

Darren Chamberlain E&lt;lt&gt;darren@cpan.orgE&lt;gt&gt;,
Ken Y. Clark E&lt;lt&gt;kclark@cpan.orgE&lt;gt&gt;.
Jon Jensen E&lt;lt&gt;jonj@cpan.orgE&lt;gt&gt;.
</pre></body></html>