App Serialization
-
bahuvrihi
Notes wrt hacking YAML.
require 'yaml' require 'test/unit' class A attr_reader :key, :hash, :array def initialize(value='value') @key = value @hash = {:key => value} @array = [:key, value] end end class B attr_reader :a def initialize(a=A.new) @a = a end end class CustomEmitter < YAML::Syck::Emitter module OutExtension def map( taguri, to_yaml_style) super do |out| out.extend MapExtension yield(out) end end def seq( taguri, to_yaml_style) super do |out| out.extend SeqExtension yield(out) end end end module MapExtension def add( k, v) puts [k,v].inspect super end end module SeqExtension def add(v) v.inspect super end end def initialize super @out.extend OutExtension end end class TestYAML < Test::Unit::TestCase def test_yaml_emitter_can_be_extended a = A.new b = B.new(a) emitter = CustomEmitter.new.set_resolver( YAML.resolver ) dump = b.to_yaml(emitter) d = YAML.load(dump) assert_equal 'value', d.a.key assert_equal({:key => 'value'}, d.a.hash) assert_equal [:key, 'value'], d.a.array end end -
bahuvrihi
Made a nasty hack using the above system before discovering the formatting error that syck produces.
-
bahuvrihi
- State changed from new to resolved
- Assigned user set to bahuvrihi
The current solution is acceptable. Pure-YAML serialization that removes any Proc or Threads. This allows the YAML to be re-loaded and inspected, even reconstructed for re-use.
It will have issues because the dump/reload is not totally faithful, but as an 80% I think it goes.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
