Module: AttributeKit::JSONSerializableHash

Defined in:
lib/attribute-kit/json_serializable_hash.rb

Overview

JSONSerializableHash adds a set of methods to manage serialization and deserialization of hashes and Hash decendants in a consistent manner. By default, the methods symbolize the keys when they are deserialized, which greatly improves performance and also provides consistency in how keys appear both in JSON and in the hash.

Examples:

Serializing and deserializing a Hash with JSON


class JHash < Hash
  include AttributeKit::JSONSerializableHash
end

attr_hash = JHash.new                                    #=> {}
attr_hash[:foo] = 'bar'                                  #=> 'bar'
attr_hash[:bar] = 5                                      #=> 5
attr_hash                                                #=> {:foo=>"bar", :bar=>5}

j = attr_hash.to_json                                    #=> "{\"foo\":\"bar\",\"bar\":5}"
new_hash = JHash.new                                     #=> {}
new_hash.from_json(j)                                    #=> {:foo=>"bar", :bar=>5}
new_hash                                                 #=> {:foo=>"bar", :bar=>5}
new_hash.clear                                           #=> {}

f = attr_hash.get_json(:foo)                             #=> "{\"foo\":\"bar\"}"
new_hash[:bar] = 5                                       #=> 5
new_hash.store_json(f)                                   #=> {:bar=>5, :foo=>"bar"}
new_hash                                                 #=> {:bar=>5, :foo=>"bar"}

Serializing and deserializing an AttributeHash with JSON


class MyHash < AttributeKit::AttributeHash
  include AttributeKit::JSONSerializableHash
end

attr_hash = MyHash.new                                   #=> {}
attr_hash.empty?                                         #=> true
attr_hash.dirty?                                         #=> false
attr_hash[:foo] = 'bar'                                  #=> 'bar'
attr_hash.dirty?                                         #=> true
attr_hash.dirty_keys                                     #=> [:foo]
attr_hash[:bar] = 5                                      #=> 5
attr_hash                                                #=> {:foo=>"bar", :bar=>5}

j = attr_hash.to_json                                    #=> "{\"foo\":\"bar\",\"bar\":5}"
new_hash = MyHash.new                                    #=> {}
new_hash.from_json(j)                                    #=> {:foo=>"bar", :bar=>5}
new_hash                                                 #=> {:foo=>"bar", :bar=>5}
new_hash.clear                                           #=> {}

f = attr_hash.get_json(:foo)                             #=> "{\"foo\":\"bar\"}"
new_hash[:bar] = 5                                       #=> 5
new_hash.store_json(f)                                   #=> {:bar=>5, :foo=>"bar"}
new_hash                                                 #=> {:bar=>5, :foo=>"bar"}

Instance Method Summary collapse

Instance Method Details

#from_json(json, opts = {}) ⇒ Hash

Deserializes hash contents from a JSON string, replacing the contents of the hash if they already exist

Parameters:

  • json (String)

    A JSON formatted string to be parsed and assigned as the contents of the hash

  • opts (Hash) (defaults to: {})

    A hash of options to be passed to the JSON generator, :symbolize_keys => true is set by default

Returns:

  • (Hash)

    The new contents of the hash



79
80
81
82
83
84
85
# File 'lib/attribute-kit/json_serializable_hash.rb', line 79

def from_json(json, opts = {})
  defaults = {
      :symbolize_keys => true
  }
  opts.merge!(defaults){ |key, param, default| param }
  self.replace(JSON.parse(json, opts))
end

#get_json(key, opts = {}) ⇒ String

Serializes a single key-value pair from the hash contents into a JSON string.

Parameters:

  • key (Symbol)

    The key for the key-value pair to retrieve and encode

  • opts (Hash) (defaults to: {})

    A hash of options to be passed to the JSON generator, empty by default

Returns:

  • (String)

    A JSON encoded string describing the key-value pair



91
92
93
# File 'lib/attribute-kit/json_serializable_hash.rb', line 91

def get_json(key, opts = {})
  JSON.generate({key => self[key]}, opts)
end

#store_json(json, opts = {}) ⇒ Hash

Deserializes hash contents from a JSON string, merging the contents with any contents of the hash that already exist

Parameters:

  • json (String)

    A JSON formatted string to be parsed and update the contents of the hash with

  • opts (Hash) (defaults to: {})

    A hash of options to be passed to the JSON generator, :symbolize_keys => true is set by default

Returns:

  • (Hash)

    The new contents of the hash



99
100
101
102
103
104
105
# File 'lib/attribute-kit/json_serializable_hash.rb', line 99

def store_json(json, opts = {})
  defaults = {
      :symbolize_keys => true
  }
  opts.merge!(defaults){ |key, param, default| param }
  self.update(JSON.parse(json, opts))
end

#to_json(opts = {}) ⇒ String

Serializes the entire hash contents into a JSON string.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of options to be passed to the JSON generator, empty by default

Returns:

  • (String)

    A JSON encoded string describing the contents of the hash



71
72
73
# File 'lib/attribute-kit/json_serializable_hash.rb', line 71

def to_json(opts = {})
  JSON.generate(self, opts)
end