简体   繁体   中英

js-yaml -> Parse ruby types

I am making a Ruby console in JavaScript and thus need to accurately send Ruby object information to the renderer process. I decided to use YAML given that it is super easy to use YAML on the Ruby side and contains all information required. But I keep getting errors.

Take the following ruby:

[Test,Test.new].to_yaml

This converts to the following YAML:

---
- !ruby/class 'Test'
- !ruby/object:Test {}

When I try to parse this with JS-YAML we get the following error:

unknown tag !<!ruby/class> at line 2, column 21:
    - !ruby/class 'Test'
                        ^

So I expect this is happenning because YAML in JavaScript doesn't have Ruby types! I've seen that one solution is to create new YAML types to handle this data:

var RubyClassType = new jsyaml.Type('!ruby/class', {
  kind: 'class'
});

However, in an ideal world I wouldn't have to define each individual type. In an ideal world either, all unknown types will be treated the same (eg as yaml sequences) or ruby wouldn't generate the odd ruby types in the first place. Can I get around this issue without having to define every Ruby type in JavaScript?

Looks like this can be handled in JS-YAML as follows: handle_unknown_types.js

Won't accept this as an answer though as a ruby-first solution would be better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM