psych icon indicating copy to clipboard operation
psych copied to clipboard

Can we please get a "raw" loader?

Open otheus opened this issue 9 years ago • 3 comments

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"}}]}

otheus avatar Jul 22 '16 18:07 otheus

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.

tenderlove avatar Jul 22 '16 19:07 tenderlove

+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 ;)

sdhull avatar Dec 07 '16 23:12 sdhull

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.

akostadinov avatar Mar 20 '17 15:03 akostadinov