Sha256: 4f38871e154a7463f58734a3268622aae244abb4200d01be21d59607c8ce6e9e

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 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

6 entries across 6 versions & 1 rubygems

Version Path
shoesgem-0.1480.0 shoes/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb
shoesgem-0.1469.0 shoes/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb
shoesgem-0.1430.0 shoes/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb
shoesgem-0.1429.0 shoes/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb
shoesgem-0.1428.0 shoes/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb
shoesgem-0.1426.0 shoes/ruby/gems/1.9.1/gems/json-shoes-1.1.3/lib/json/add/rails.rb