DEV Community

Piotr Romańczuk
Piotr Romańczuk

Posted on • Edited on

5 2

Transparently serialize and deserialize ValueObjects to JSON

This is the way I choose when I want to serialize and deserialize ValueObjects transparently to JSON in Rails app

Put serialization methods into module

module ValueObjectJsonSerialization
  def to_json(*args)
    #override AtiveSupport to_json extensions if active
    args = JSON::SAFE_STATE_PROTOTYPE.dup if args == []
    {
      :json_class => self.class.to_s,
    }.merge(as_json).to_json(*args)
  end

  def as_json(_=nil)
    # unused argument to satisfy ActiveSupport as_json signature
    { :json_class => self.class.to_s }.merge(_as_json)
  end
end

Enter fullscreen mode Exit fullscreen mode

Use them in your ValueObject class and make it transparently serializable

class PublicationDate
  include ValueObjectJsonSerialization
  def initialize(y, m=nil, d=nil)
    @y, @m, @d = y, m, d 
  end

  def self.json_create(h)
    new(h['y'], h['m'], h['d'])
  end

  def _as_json
    { y: @y, m: @m, d: @d }.compact
  end
end
Enter fullscreen mode Exit fullscreen mode

DEMO

require 'json'
[9] pry(main)> PublicationDate.new(2021, 4)
=> #<PublicationDate:0x00005652cd6a5c40 @d=nil, @m=4, @y=2021>
[10] pry(main)> PublicationDate.new(2021, 4, 19).to_json
=> "{\"json_class\":\"PublicationDate\",\"y\":2021,\"m\":4,\"d\":19}
[11] pry(main)> JSON.load PublicationDate.new(2021, 4, 19).to_json
=> #<PublicationDate:0x00005652cd717d18 @d=19, @m=4, @y=2021>
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay