Rails 2.02 dropped JSON attribute tag ?

Hey there

I was scratching my head over why my application breaks when I run on a local server but works on my remote server. It turns out that my local server is Rails 2.0.2 and my remote server is 1.2.3 and I expect the tag "attributes" to be passed with a json stream, before parsing. Unfortunately, this was dropped in 2.0.2. Is there any documentation on this? Any way to get it back?

Examples:

Rails 1.2.3:

Site.find(:first).to_json

=> "{attributes: {title: \"some bogus title\", location: \"some bogus location\"}}"

Rails 2.0.2:

Site.find(:first).to_json

=> "{title: \"some bogus title\", location: \"some bogus location\"}"

Thanks! -Dave

Can anyone verify this? It seems like a bug and I'm wondering how to report it?

Thanks, Dave

I guess this is because prior to Rails 2.0 json encoding was done in a generic way for all objects, taking their instance variables and constructing a json string from them: http://dev.rubyonrails.org/browser/tags/rel_1-2-3/activesupport/lib/active_support/json?rev=7905

Rails 2.0 introduced a Json serializer into ActiveRecord that behaves differently: http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/ (Check out serialization.rb and serializers/json_serializer.rb) The AR Serializer extracts serializable methods and attributes and creates a serializable record out of that which is THEN processed by ActiveSupports JsonConverter.

To get the old behaviour, use ActiveSupport::JSON.encode(@record.instance_values)

This looks like it might be related to an issue that I'm having getting some code from the book "Google Maps Applications with Rails and Ajax". I am a complete Ruby and Rails beginner, and a fairly rusty coder to boot, and so have no idea how the suggestion of helps:

ActiveSupport::JSON.encode(@record.instance_values)

But appreciate being pointed in the right direction non the less and would very much appreciate any further help anyone can give.

Everything is working fine on my dev environment which is running 1.2.6, but not on my Heroku.com account which is running 2.0.2. The code in question I believe is as follows:

   def list      render :text=>(Marker.find :all).to_json    end

and is later called by a Javascript function where the relevant code is this:

//tell the request what to do when the state changes.     request.onreadystatechange = function() {         //alert('Step 1');         if (request.readyState == 4) {             //parse the result to JSON, by eval-ing it.             //The response is an array of markers             //alert('Step 2');             markers=eval("(" + request.responseText + ")" );             for (var i = 0; i < markers.length ; i++) {

                var marker=markers[i].attributes                 //alert(Step 3);                 var lat=marker.lat;                 var lng=marker.lng;

I get a JavaScript error telling me that 'marker has no properties' which I think kicks in somewhere around the commented out Step 3 alert.

Failing any solution to the above does anyone know how/if it is possible to role back to 1.2.6 when already running 2.0.2? This might be best whislt I'm using two books which are both written for versions of Rails earlier than 2.0.

Any light that anyone can shed on this would be very very much appreciated.

For anyone reading my book (Google Maps Applications with Rails and Ajax), I posted an explanation along with relevant changes to examples in the book here: http://googlemapsbook.com/2008/02/13/to_json-and-rails-2/

It's not a bug, it's a decision so reporting it will fall on deaf ears. If you really need to get it back you can probably do something like this:

{:attributes=>Stie.find(params[:id])}.to_json

It'd probably also be worth upgrading your dev environment...