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

  • SEARCH
  • Home
  • 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 6766709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:51:50+00:00 2026-05-26T14:51:50+00:00

I asked a previous question about serialization and validation. Someone mentioned using the JSON

  • 0

I asked a previous question about serialization and validation. Someone mentioned using the JSON gem which allows me to tell my object how to serialize with the to_json method, however Ruby seems to do LOTS of complicated things really easily, however on the flip side some really simple things seem to be quite complicated, Serialization being one of those things.

I want to find out if there is a way to have a clean object:

class CleanClass
    attr_accessor :variable1
    attr_accessor :variable2
    attr_accessor :variable3
end

cleanObject = CleanClass.new

Ideally, I don’t want to dirty the model, I just want to pass it to something and tell it what the output type should be and let it work its magic.

An example would be something like:

jsonOutput = MagicSerializer::Json.Serialize(cleanObject)
xmlOutput = MagicSerializer::Xml.Serialize(cleanObject)
yamlOutput = MagicSerializer::Yaml.Serialize(cleanObject)

revertedJsonObject = MagicSerializer::Json.Unserialize(jsonOutput)
revertedXmlObject = MagicSerializer::Xml.Unserialize(xmlOutput)
revertedYamlObject = MagicSerializer::Yaml.Unserialize(yamlOutput)

I want to pass something an object, and get the strings outputted, then be able to convert it back.

I know ActiveModel has serialization functionality but I need to dirty my class to do this, and I don’t want to change the model if possible. ActiveSupport seems to satisfy the JSON criteria as I can just call that and it will take an object and spit out the JSON, but I would like to support other types.

Any further information would be great!

  • 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-26T14:51:50+00:00Added an answer on May 26, 2026 at 2:51 pm

    Ruby has built-in automagical serialization/deserialization to binary and yaml.

    Yaml:

    require 'yaml'
    serialized = CleanClass.new.to_yaml
    object = YAML.load(serialized)
    

    Marshal:

    serialized = Marshal.dump(CleanClass.new)
    object = Marshal.load(serialized)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is in continuation to my previous question, in which I asked about
I asked a previous question about mapping an enumerated value on a table using
Unit testing Abstract classes in Groovy I asked a question previous about unit testing
In a previous question I asked about how to group XML elements logically, and
So in a previous question I asked about implementing a generic interface with a
This stems from a previous question I asked - about a write conflict with
This is a follow up question to a previous question I asked about calculating
In a previous question, I asked about various ORM libraries. It turns out Kohana
In my previous question I asked about storing the result of a query in
I asked a previous question about keeping up a constant link with a database,

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.