| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# =========================================================================== |
| 4 |
# |
| 5 |
# PUBLIC DOMAIN NOTICE |
| 6 |
# National Center for Biotechnology Information (NCBI) |
| 7 |
# |
| 8 |
# This software/database is a "United States Government Work" under the |
| 9 |
# terms of the United States Copyright Act. It was written as part of |
| 10 |
# the author's official duties as a United States Government employee and |
| 11 |
# thus cannot be copyrighted. This software/database is freely available |
| 12 |
# to the public for use. The National Library of Medicine and the U.S. |
| 13 |
# Government do not place any restriction on its use or reproduction. |
| 14 |
# We would, however, appreciate having the NCBI and the author cited in |
| 15 |
# any work or product based on this material. |
| 16 |
# |
| 17 |
# Although all reasonable efforts have been taken to ensure the accuracy |
| 18 |
# and reliability of the software and data, the NLM and the U.S. |
| 19 |
# Government do not and cannot warrant the performance or results that |
| 20 |
# may be obtained by using this software or data. The NLM and the U.S. |
| 21 |
# Government disclaim all warranties, express or implied, including |
| 22 |
# warranties of performance, merchantability or fitness for any particular |
| 23 |
# purpose. |
| 24 |
# |
| 25 |
# =========================================================================== |
| 26 |
# |
| 27 |
# File Name: xml2json |
| 28 |
# |
| 29 |
# Author: Jonathan Kans |
| 30 |
# |
| 31 |
# Version Creation Date: 11/09/2020 |
| 32 |
# |
| 33 |
# ========================================================================== |
| 34 |
|
| 35 |
# Entrez Direct legacy support for xml to json, extracted from edirect.pl |
| 36 |
|
| 37 |
# use strict; |
| 38 |
use warnings; |
| 39 |
|
| 40 |
# usage - xml2json |
| 41 |
|
| 42 |
use JSON::PP; |
| 43 |
use XML::Simple; |
| 44 |
|
| 45 |
# definitions |
| 46 |
|
| 47 |
use constant false => 0; |
| 48 |
use constant true => 1; |
| 49 |
|
| 50 |
sub convert_bools { |
| 51 |
my %unrecognized; |
| 52 |
|
| 53 |
local *_convert_bools = sub { |
| 54 |
my $ref_type = ref($_[0]); |
| 55 |
if (!$ref_type) { |
| 56 |
# Nothing. |
| 57 |
} |
| 58 |
elsif ($ref_type eq 'HASH') { |
| 59 |
_convert_bools($_) for values(%{ $_[0] }); |
| 60 |
} |
| 61 |
elsif ($ref_type eq 'ARRAY') { |
| 62 |
_convert_bools($_) for @{ $_[0] }; |
| 63 |
} |
| 64 |
elsif ( |
| 65 |
$ref_type eq 'JSON::PP::Boolean' # JSON::PP |
| 66 |
|| $ref_type eq 'Types::Serialiser::Boolean' # JSON::XS |
| 67 |
) { |
| 68 |
$_[0] = $_[0] ? 1 : 0; |
| 69 |
} |
| 70 |
else { |
| 71 |
++$unrecognized{$ref_type}; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
&_convert_bools; |
| 76 |
} |
| 77 |
|
| 78 |
sub xml_to_json { |
| 79 |
|
| 80 |
my $data = shift (@_); |
| 81 |
|
| 82 |
my $xc = new XML::Simple(KeepRoot => 1); |
| 83 |
my $conv = $xc->XMLin($data); |
| 84 |
convert_bools($conv); |
| 85 |
my $jc = JSON::PP->new->ascii->pretty->allow_nonref; |
| 86 |
$data = $jc->encode($conv); |
| 87 |
|
| 88 |
return $data; |
| 89 |
} |
| 90 |
|
| 91 |
# main block |
| 92 |
|
| 93 |
my $str = do { local $/; <STDIN> }; |
| 94 |
|
| 95 |
$data = xml_to_json($str); |
| 96 |
|
| 97 |
print $data; |
| 98 |
|
| 99 |
# close input and output files |
| 100 |
|
| 101 |
close (STDIN); |
| 102 |
close (STDOUT); |
| 103 |
close (STDERR); |