Class: XapianDb::TypeCodec::JsonCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/type_codec.rb

Class Method Summary collapse

Class Method Details

.decode(json_string) ⇒ Hash

Decode an object from a json string

Parameters:

  • json_string (String)

    a json string representing the object

Returns:

  • (Hash)

    a ruby hash


44
45
46
47
48
49
50
51
# File 'lib/type_codec.rb', line 44

def self.decode(json_string)
  return nil if json_string.nil? || json_string == ""
  begin
    JSON.parse json_string
  rescue TypeError
    raise ArgumentError.new "'#{json_string}' cannot be parsed"
  end
end

.encode(object) ⇒ String

Encode an object to its json representation

Parameters:

  • object (Object)

    an object to encode

Returns:

  • (String)

    the json string


32
33
34
35
36
37
38
39
# File 'lib/type_codec.rb', line 32

def self.encode(object)
  return nil if object.nil?
  begin
    object.to_json
  rescue NoMethodError
    raise ArgumentError.new "#{object} does not support json serialization"
  end
end