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 440651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:53:39+00:00 2026-05-12T20:53:39+00:00

I have a structure of simple container classes like so (in pseudo ruby): class

  • 0

I have a structure of simple container classes like so (in pseudo ruby):

class A
  attr_reader :string_field1, :string_field2
  ...
end

class B
  attr_reader: int_field3, :string_field4
  ...
end

# C includes an instance of A and B
class C
  attr_reader: :a_instance, :b_instance
  ...
end

Is there are simple way to de/serialize this to JSON in Ruby? Or should I make nested serialization methods per class as in this example?

EDIT:

In my specific scenario, I want to POST some JSON data to a server running Ruby, which will extract the data and act accordingly.

The sender of the JSON is not necessarily a Ruby process, but may be the back end of some other system. (Although it is Ruby in my test harness).

So, I don’t need the JSON to be in some “Ruby specific” format, I just assumed it would be easier if that was actually built-in to Ruby.

  • 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-05-12T20:53:39+00:00Added an answer on May 12, 2026 at 8:53 pm

    Use Marshal, PStore, or another Ruby solution for non-JSON objects

    One might reasonably wonder why a completely reflective language like Ruby doesn’t automate JSON generation and parsing of arbitrary classes.

    However, unless you stick to the JSON types, there is no place to send or receive the JSON objects except to another running Ruby. And in that case, I suspect that the conventional wisdom is “forget JSON, use a native Ruby interface like the core class Marshal.

    So, if you are really sending those objects to PHP or something non-Ruby, then you should create directly JSON-supported Ruby data structures using Array and the like, and then you will have something that JSON.generate will directly deal with.

    If you just need serialization it’s possible you should use Marshal or PStore.

    Update: Aha, ok, try this:

    module AutoJ
      def auto_j
        h = {}
        instance_variables.each do |e|
          o = instance_variable_get e.to_sym
          h[e[1..-1]] = (o.respond_to? :auto_j) ? o.auto_j : o;
        end
        h
      end
      def to_json *a
        auto_j.to_json *a
      end
    end
    

    If you then include AutoJ in each of your classes, it should DTRT. In your example this results in

    {
      "a": {
        "string_field1": "aa",
        "string_field2": "bb"
      },
      "b": {
        "int_field3": 123,
        "string_field4": "dd"
      }
    }
    

    You might want to change the auto_j method to return h.values instead of just h, in which case you get:

    [
      ["aa", "bb"],
      [123, "dd"]
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OK, I got it: eclipse.ini should contain following line: -Duser.language=en_US May 12, 2026 at 10:53 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure exactly why your table is updated by… May 12, 2026 at 10:53 pm
  • Editorial Team
    Editorial Team added an answer The executables aren't themselves portable. PE format is "portable" in… May 12, 2026 at 10:53 pm

Related Questions

Suppose I have an interface for a service: public interface IFooService { void DoSomething();
I am not an idiot, but header files make me feel like one sometimes.
I'm looking for a better way to this. I have a chunk of code
I'm having trouble finding/getting any logging output from a web service. I have a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.