Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added exclude_json_class option to json serialization.

  • Loading branch information...
commit aa8411a0af80721126621d22d14033682704f1f1 1 parent 266aa17
@treeder treeder authored
Showing with 47 additions and 38 deletions.
  1. +10 −1 README.markdown
  2. +37 −37 lib/simple_record/json.rb
View
11 README.markdown
@@ -2,7 +2,7 @@
An ActiveRecord interface for SimpleDB. Can be used as a drop in replacement for ActiveRecord in rails.
-Brought to you by: [![Appoxy](http://www.simpledeployr.com/images/global/appoxy-small.png)](http://www.appoxy.com)
+Brought to you by: [![Appoxy](https://lh5.googleusercontent.com/_-J9DSaseOX8/TX2Bq564w-I/AAAAAAAAxYU/xjeReyoxa8o/s800/appoxy-small%20%282%29.png)](http://www.appoxy.com)
## Discussion Group
@@ -261,6 +261,15 @@ all the clobs on the object.
Setting this will automatically use :s3_bucket=>:new as well.
+## JSON Support
+
+You can easily marshal and unmarshal SimpleRecord objects and results by calling `to_json` on them. The default
+serialization will include a `json_class` value which will be used when deserializing to find the class. If you're using
+the results in an API though, you may not want to include json_class because the receiving end may not have that class
+around, so you can pass in `:exclude_json_class` option to to_json, eg:
+
+ my_ob.to_json(:exclude_json_class=>true)
+
## Tips and Tricks and Things to Know
### Automagic Stuff
View
74 lib/simple_record/json.rb
@@ -1,30 +1,30 @@
module SimpleRecord
- module Json
+ module Json
- def self.included(base)
- base.extend ClassMethods
- end
+ def self.included(base)
+ base.extend ClassMethods
+ end
- module ClassMethods
+ module ClassMethods
- def json_create(object)
- obj = new
- for key, value in object
- next if key == 'json_class'
- if key == 'id'
- obj.id = value
- next
- end
- obj.set key, value
- end
- obj
- end
+ def json_create(object)
+ obj = new
+ for key, value in object
+ next if key == 'json_class'
+ if key == 'id'
+ obj.id = value
+ next
+ end
+ obj.set key, value
+ end
+ obj
+ end
- def from_json(json_string)
- return JSON.parse(json_string)
- end
+ def from_json(json_string)
+ return JSON.parse(json_string)
+ end
- end
+ end
#
# def to_json(*a)
# puts 'SimpleRecord to_json called'
@@ -46,26 +46,26 @@ def from_json(json_string)
# return ret
# end
- def as_json(options={})
-# puts 'SimpleRecord as_json called'
- result = {
- 'json_class' => self.class.name,
- 'id' => self.id
- }
- defined_attributes_local.each_pair do |name, val|
+ def as_json(options={})
+ puts 'SimpleRecord as_json called with options: ' + options.inspect
+ result = {
+ 'id' => self.id
+ }
+ result['json_class'] = self.class.name unless options[:exclude_json_class]
+ defined_attributes_local.each_pair do |name, val|
# puts name.to_s + "=" + val.inspect
- if val.type == :belongs_to
- result[name.to_s + "_id"] = get_attribute_sdb(name)
- else
- result[name] = get_attribute(name)
- end
+ if val.type == :belongs_to
+ result[name.to_s + "_id"] = get_attribute_sdb(name)
+ else
+ result[name] = get_attribute(name)
+ end
# puts 'result[name]=' + result[name].inspect
- end
+ end
# ret = result.as_json(options)
# puts 'ret=' + ret.inspect
# return ret
- result
- end
-
+ result
end
+
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.