Skip to content

Instantly share code, notes, and snippets.

@syrnick
Created January 13, 2011 00:45
Show Gist options
  • Save syrnick/777189 to your computer and use it in GitHub Desktop.
Save syrnick/777189 to your computer and use it in GitHub Desktop.
To json issues
# Example of our problems with to_json( :to_json => false )
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-migrations/migration_runner'
require 'dm-timestamps'
require 'dm-serializer'
require 'dm-types'
require 'benchmark'
DataMapper.setup(:default, 'postgres://localhost/dm_test') # createdb test
class Course
include DataMapper::Resource
property :id, Serial
property :name, String
property :homepage, URI
end
DataMapper::MigrationRunner.migration( 1, :create_courses ) do
up do
create_table :courses do
column :id, Integer, :serial => true
column :name, String, :size => 50
column :homepage, String
end
create_index(:courses, :id)
end
down do
drop_table :courses
end
end
describe 'to_json' do
it 'should return a hash with simple types' do
DataMapper::MigrationRunner.migrate_up!
e = Course.create( :name => 'Magic in ruby', :homepage => URI.parse('http://ruby.org') )
json_representation = e.reload.to_json( :to_json => false )
reloaded_json = JSON.load(JSON.dump( json_representation ))
#This is fine, because we reloaded it:
p reloaded_json
reloaded_json["homepage"].class.should == String
#This fails, because the original representation has non-string values
p json_representation
json_representation["homepage"].class.should == String
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment