Class: XapianDb::TypeCodec::JsonCodec
- Inherits:
-
Object
- Object
- XapianDb::TypeCodec::JsonCodec
- Defined in:
- lib/type_codec.rb
Class Method Summary collapse
-
.decode(json_string) ⇒ Hash
Decode an object from a json string.
-
.encode(object) ⇒ String
Encode an object to its json representation.
Class Method Details
.decode(json_string) ⇒ Hash
Decode an object from a json string
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
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 |