Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we please get a "raw" loader? #286

Open
otheus opened this issue Jul 22, 2016 · 3 comments
Open

Can we please get a "raw" loader? #286

otheus opened this issue Jul 22, 2016 · 3 comments

Comments

@otheus
Copy link

otheus commented Jul 22, 2016

There are times when we need to load a YAML file but for various reasons, we do not want or care about the serialization/native dataclass conversions. We want to load the data in as a hash-tree object, as if the object classifier were not there.

Now it seems to me based on the long discussion related to the safe_load method, that safe_load does not, in fact, do what is requested here. It seems to me that several sane voices were a bit shuoted down in that discussion. But whatever.

What is needed is essentially to load YAML files as if they were dumb JSON files. As if the following code were implemented before the #load

yamltext = File.read("somefile","r")
yamltext.sub!(/^--- \!.*$/,'---')
purehash = YAML.load(yamltext)

Given the following YAML:

--- !ruby/object:Some::Custom::Type::User::Has::No::Gem::For
 node: "string"
 array:
  - "string"
  - unquoted text
  - "12.5"
  - 12.5
  - subarray:
      option: true
      nottrue: "false"

We get back from #load:

{"node"=>"string", "array"=>["string", "unquoted text", "12.5", 12.5, {"subarray"=>{"option"=>true, "nottrue"=>"false"}}]}

I propose an implementation that is cleaner, that uses YAML.load() but based on the parser-personality. The personalities might be a kind of a "raw" mode and "native-object" mode. Raw mode only sees Hash, Array, String. Native-object mode also sees numeric, boolean (and..?)

Psych.set_parse_mode(:native_object_conversion)
purehash=YAML.load(yamltext)

Produces the inspected output, above. Whereas:

Psych.set_parse_mode(:no_object_conversion)
purehash=YAML.load(yamltext)

produces (subtly different):

{"node"=>"string", "array"=>["string", "unquoted text", "12.5", "12.5", {"subarray"=>{"option"=>"true", "nottrue"=>"false"}}]}
@tenderlove
Copy link
Member

I don't mind adding ways to customize loading, but not via global settings. Something similar to safe_load, but takes YAML and a class loader object should be enough to accomplish what you're looking for.

@sdhull
Copy link

sdhull commented Dec 7, 2016

+1

Personally, I'd just love to be able to pass an option to safe_load like raise: false. Instead of raising Psych::DisallowedClass, it could just leave the value as a string. In fact, I'd prefer that to be default behavior but maybe that's just me ;)

@akostadinov
Copy link

Passing a class loader would be awesome. The reverse - object to perform serialization would also be awesome. These two will allow to handle communication with non-fully compliant services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants