Method: Xunch::JsonCodec#encode

Defined in:
lib/xunch/codec/json_codec.rb

#encode(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xunch/codec/json_codec.rb', line 10

def encode(value)
  unless value.class.name.start_with? @klass.name
    raise XunchCodecError.new("Codec error. Codec class is #{@klass}," <<
      "object_id is #{@klass.object_id}, but value class is #{value.class}," <<
      "object_id is #{value.class.object_id}.
      value class = #{value.class.inspect} vs config class = #{@klass.inspect} ")
  end
  hash = Hash.new
  @get_methods.each do | k , v |
    if value.has_attribute?(v)
      var_value = value.send(v)
      if var_value != nil
        hash[k] = var_value
      end
    end
  end
  Yajl::Encoder.encode(hash)
end