Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor YAML/JSON heuristics into a new load_string method

  • Loading branch information...
commit 5a4c3ad835cea4400334e59d21813ea640074237 1 parent f8cf075
@dagolden dagolden authored
Showing with 23 additions and 5 deletions.
  1. +4 −0 Changes
  2. +19 −5 lib/Parse/CPAN/Meta.pm
View
4 Changes
@@ -2,6 +2,10 @@ Changes for Perl programming language extension Parse-CPAN-Meta
{{$NEXT}}
+ [ADDED]
+
+ - Added load_string, which uses heuristics to guess YAML/JSON content
+
[CHANGED]
- load_file will now attempt to detect YAML/JSON content if the suffix
View
24 lib/Parse/CPAN/Meta.pm
@@ -21,14 +21,21 @@ sub load_file {
elsif ($filename =~ /\.json$/) {
return $class->load_json_string($meta);
}
- elsif ( $meta =~ /^---/ ) { # looks like YAML
- return $class->load_yaml_string($meta);
+ else {
+ $class->load_string($meta); # try to detect yaml/json
}
- elsif ( $meta =~ /^\s*\{/ ) { # looks like JSON
- return $class->load_json_string($meta);
+}
+
+sub load_string {
+ my ($class, $string) = @_;
+ if ( $string =~ /^---/ ) { # looks like YAML
+ return $class->load_yaml_string($string);
+ }
+ elsif ( $string =~ /^\s*\{/ ) { # looks like JSON
+ return $class->load_json_string($string);
}
else { # maybe doc-marker-free YAML
- return $class->load_yaml_string($meta);
+ return $class->load_yaml_string($string);
}
}
@@ -194,6 +201,13 @@ This method deserializes the given string of JSON and the result.
If the source was UTF-8 encoded, the string must be decoded before calling
C<load_json_string>.
+=head2 load_string
+
+ my $metadata_structure = Parse::CPAN::Meta->load_string($some_string);
+
+If you don't know whether a string contains YAML or JSON data, this method
+will use some heuristics and guess. If it can't tell, it assumes YAML.
+
=head2 yaml_backend
my $backend = Parse::CPAN::Meta->yaml_backend;
Please sign in to comment.
Something went wrong with that request. Please try again.