Sha256: f4651416cbae94acd4d68e94d41faa24a68b3f2efd77c68a62960d56d9ebbdf4

Contents?: true

Size: 1.19 KB

Versions: 183

Compression:

Stored size: 1.19 KB

Contents

# This file contains implementations of rails custom objects for
# serialisation/deserialisation.

unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
  ::JSON::JSON_LOADED
  require 'json'
end

class Object
  def self.json_create(object)
    obj = new
    for key, value in object
      next if key == 'json_class'
      instance_variable_set "@#{key}", value
    end
    obj
  end

  def to_json(*a)
    result = {
      'json_class' => self.class.name
    }
    instance_variables.inject(result) do |r, name|
      r[name[1..-1]] = instance_variable_get name
      r
    end
    result.to_json(*a)
  end
end

class Symbol
  def to_json(*a)
    to_s.to_json(*a)
  end
end

module Enumerable
  def to_json(*a)
    to_a.to_json(*a)
  end
end

# class Regexp
#   def to_json(*)
#     inspect
#   end
# end
#
# The above rails definition has some problems:
#
# 1. { 'foo' => /bar/ }.to_json # => "{foo: /bar/}"
#    This isn't valid JSON, because the regular expression syntax is not
#    defined in RFC 4627. (And unquoted strings are disallowed there, too.)
#    Though it is valid Javascript.
#
# 2. { 'foo' => /bar/mix }.to_json # => "{foo: /bar/mix}"
#    This isn't even valid Javascript.

Version data entries

183 entries across 167 versions & 24 rubygems

Version Path
crazy_ivan-1.1.1 lib/crazy_ivan/vendor/json-1.1.7/lib/json/add/rails.rb
crazy_ivan-1.1.0 lib/crazy_ivan/vendor/json-1.1.7/lib/json/add/rails.rb
factorylabs-castronaut-0.7.5 vendor/json/lib/json/add/rails.rb
crazy_ivan-1.0.0 lib/crazy_ivan/vendor/json-1.1.7/lib/json/add/rails.rb
scout-5.0.3 vendor/json_pure/lib/json/add/rails.rb
middleman-0.12.2 lib/middleman/vendor/gems/gems/json-1.2.0/lib/json/add/rails.rb
middleman-0.12.2 lib/middleman/vendor/gems/gems/json_pure-1.2.0/lib/json/add/rails.rb
scout-5.0.2 vendor/json_pure/lib/json/add/rails.rb
poolparty-1.4.6 vendor/gems/json/lib/json/add/rails.rb
middleman-0.12.1 lib/middleman/vendor/gems/gems/json_pure-1.2.0/lib/json/add/rails.rb
middleman-0.12.1 lib/middleman/vendor/gems/gems/json-1.2.0/lib/json/add/rails.rb
middleman-0.12.0.pre3 lib/middleman/vendor/gems/gems/json_pure-1.2.0/lib/json/add/rails.rb
middleman-0.12.0.pre3 lib/middleman/vendor/gems/gems/json-1.2.0/lib/json/add/rails.rb
middleman-0.12.0.pre2 lib/middleman/vendor/gems/gems/json-1.2.0/lib/json/add/rails.rb
middleman-0.12.0.pre2 lib/middleman/vendor/gems/gems/json_pure-1.2.0/lib/json/add/rails.rb
poolparty-1.4.5 vendor/gems/json/lib/json/add/rails.rb
poolparty-1.4.4 vendor/gems/json/lib/json/add/rails.rb
poolparty-1.4.3 vendor/gems/json/lib/json/add/rails.rb
poolparty-1.4.2 vendor/gems/json/lib/json/add/rails.rb
poolparty-1.4.1 vendor/gems/json/lib/json/add/rails.rb