Phylo/Parsers/Json.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 42 CC 2 SCAR 11.5
CC
2
Cov
0%
_parse
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.1
1 package Bio::Phylo::Parsers::Json;
2 use strict;
3 use base 'Bio::Phylo::Parsers::Abstract';
4 use Bio::Phylo::IO 'parse';
5 use Bio::Phylo::Util::Exceptions 'throw';
6 use Bio::Phylo::Util::Dependency 'Bio::Phylo::NeXML::XML2JSON';
7  
8 =head1 NAME
9  
10 Bio::Phylo::Parsers::Json - Parser used by Bio::Phylo::IO, no serviceable parts inside
11  
12 =head1 DESCRIPTION
13  
14 This module is used to import NeXML data that was re-formatted as JSON, using
15 the mapping implemented by L<XML::XML2JSON>. This module is experimental in that
16 complex NeXML-to-JSON mapped strings may fail to yield valid NeXML (and,
17 consequently, valid Bio::Phylo objects) in the round trip. The reason for this
18 is that the XML2JSON omits xmlns declarations in its JSON output. We try to work
19 around this here by re-introducing the default namespaces, but if any additional
20 ones were present in the original NeXML (e.g. to specify namspaces for metadata
21 predicates) these can't be reconstructed. In addition, the JSON that XML2JSON
22 produces doesn't preserve element order. We try to be lenient about this when
23 parsing the intermediate NeXML, though it is actually invalid.
24  
25 =cut
26  
27 sub _parse {
28 my $self = shift;
29 my $fh = $self->_handle;
30 my $json = do { local $/; <$fh> };
31
32 # perhaps not happy about prolog?
33 $json =~ s/"\@encoding":"\S+?","\@version":"1.0",//;
34 my $conf = Bio::Phylo::NeXML::XML2JSON->new;
35 my $xml = $conf->json2xml($json);
36 my $fac = $self->_factory;
37 my $proj = $fac->create_project;
38 my $ns = $proj->get_namespaces;
39 for my $pre ( keys %{ $ns } ) {
40 my $uri = $ns->{$pre};
41 if ( $xml !~ /xmlns:$pre/ ) {
42 $xml =~ s/<nex:nexml /<nex:nexml xmlns:$pre="$uri" /;
43 }
44 }
45 return @{ parse( '-format' => 'nexml', '-string' => $xml ) };
46 }
47  
48 # podinherit_insert_token
49  
50 =head1 SEE ALSO
51  
52 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
53 for any user or developer questions and discussions.
54  
55 =over
56  
57 =item L<Bio::Phylo::IO>
58  
59 The json parser is called by the L<Bio::Phylo::IO|Bio::Phylo::IO> object.
60 Look there to learn how to parse data using Bio::Phylo.
61  
62 =item L<Bio::Phylo::Manual>
63  
64 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>
65  
66 =back
67  
68 =head1 CITATION
69  
70 If you use Bio::Phylo in published research, please cite it:
71  
72 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
73 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
74 I<BMC Bioinformatics> B<12>:63.
75 L<http://dx.doi.org/10.1186/1471-2105-12-63>
76  
77 =cut
78  
79 1;