Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:29:56+00:00 2026-06-17T06:29:56+00:00

This issue will surface for many who depend on Ruby’s JSON serialization outside of

  • 0

This issue will surface for many who depend on Ruby’s JSON serialization outside of a Rails projects. When they try to use their code in a Rails project, it will not work as expected.

The following code run from Ruby (no Rails), prints A.
When run from rails console, it prints Hash.

That means my json serialization works in my command line lib/app, but not when it’s imported into a Rails project.

What is the reason/workaround for this?

require 'json'

class A
  def to_json(*a)
    {:json_class => self.class.name}.to_json(*a)
  end
  def self.json_create(o)
    A.new
  end
end

class B
  attr_accessor :value
  def initialize(value)
    @value = value
  end

  def to_json(*a)
    {:json_class => self.class.name, :value => value}.to_json(*a)
  end
  def self.json_create(o)
    B.new(o['value'])
  end
end
b = JSON.parse(B.new(A.new).to_json)
puts b.value.class

Ruby is 1.9.3, Rails is 3.2.10

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:29:57+00:00Added an answer on June 17, 2026 at 6:29 am

    According to others, the answer is yes.
    http://www.rubyhood.com/2011/06/rails-spoiled-standard-json-library.html

    In short, make as_json do what to_json does. That got me what I wanted/expected (and what I’ve been getting from pure Ruby – Rails).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It looks like this issue will be solved in Rails 4: http://blog.plataformatec.com.br/2012/08/eager-loading-for-greater-good/ but until
i keep running in this issue where it will jump out of the for
Update: This issue was not properly explored. The real issue lies within render :json
I am stuck with this issue and will appreciate any help... (Oleg, are you
Going nuts with this issue. I have a solution with 2 projects, one of
This issue has been on/off bugging me and I've written 4 wrappers now. I'm
This issue is better demonstrated than explained, so I've set up a git repo
This issue has me baffled, it's affecting a single user (to my knowledge) and
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
Note: This issue appears to be limited to SQL Server 2005 SP2 I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.