Class: Chainer::Serializers::MarshalDeserializer

Inherits:
Deserializer show all
Defined in:
lib/chainer/serializers/marshal.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Deserializer

#load

Constructor Details

#initialize(marshalData, path: '', strict: true) ⇒ MarshalDeserializer

Returns a new instance of MarshalDeserializer.



56
57
58
59
60
# File 'lib/chainer/serializers/marshal.rb', line 56

def initialize(marshalData, path: '', strict: true)
  @marshal_data = marshalData
  @path = path
  @strict = strict
end

Class Method Details

.load_file(filename, obj, path: '', strict: true) ⇒ Object

Loads an object from the file in Marshal format. This is a short-cut function to load from an Marshal file that contains only one object.

Parameters:

  • filename (string)

    Name of the file to be loaded.

  • obj (object)

    Object to be deserialized. It must support serialization protocol.



49
50
51
52
53
54
# File 'lib/chainer/serializers/marshal.rb', line 49

def self.load_file(filename, obj, path: '', strict: true)
  File.open(filename) do |f|
    d = self.new(Marshal.load(f), path: path, strict: strict)
    d.load(obj)
  end
end

Instance Method Details

#[](key) ⇒ Object



62
63
64
# File 'lib/chainer/serializers/marshal.rb', line 62

def [](key)
  self.class.new(@marshal_data, path: File.join(@path, key, '/'), strict: @strict)
end

#call(key, value) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chainer/serializers/marshal.rb', line 66

def call(key, value)
  key = File.join(@path, key)
  if !@strict && !@marshal_data.keys.include?(key)
    return value
  end

  dataset = @marshal_data[key]
  if value.nil?
    return dataset
  elsif value.instance_of?(String)
    return dataset
  elsif Chainer.array?(value)
    value.store(dataset)
    return value
  elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
    return dataset[0] == 1
  else
    return dataset[0]
  end
end