package common;
  use JSON::PP;
  use URI::Escape;
  $tabstops=int $ENV{'JSON_INDENT'} || 0;
  $json = JSON::PP->new->pretty;
  $json->space_before(0);
  $json->indent($tabstops > 0);
  $json->indent_length($tabstops);
  $json->allow_nonref(1);

  sub stdin {
    my $data;
    undef $/;
    while (<>) {
      $data = $_;
    }
    return $data;
  }

  sub parse {
    my $data = common->stdin();
    return common->convert($data);
    #undef $/;
    #while (<>) {
      #$doc = $json->decode($_);
    #}
    #return $doc;
  }

  sub convert {
    my ($class, $data) = (@_);
    return $json->decode($data);
  }

  sub stringify {
    my ($class, $doc, $stops) = (@_);
    if(defined($stops)) {
      if($stops == 0) {
        $json->indent(0);
      }else{
        $json->indent(1);
      }
      $json->indent_length($stops);
    }
    return $json->encode($doc);
  }

  sub compact {
    my ($class, $doc) = (@_);
    return common->stringify($doc,0);
  }
1;
