Ruby module to easily provide json serialization
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.autotest
.gitignore
.rspec
.ruby-version
.travis.yml
Gemfile
LICENSE.txt
README.md
Rakefile
jsonizer.gemspec

README.md

Gem Version Build Status Code Climate Coverage Status Dependencies Documentation PullReview stats

Jsonizer

Module to easily provide json serialization

It was structurally inspired by http://github.com/dkubb/equalizer

Installation

Using Bundler (recommended)

  • Add gem 'jsonizer' to your Gemfile
  • Run bundle install

Using rubygems

  • Run gem install jsonizer

Installing from git

  • Clone the repository using git clone git://github.com/voidus/jsonizer
  • Enter the directory using cd jsonizer
  • Build and install the gem with rake install

Usage

class TransferObject
  include Jsonizer.new :operation_id, :parameter

  attr_reader :operation_id, :parameter
  attr_accessor :calculation_strategy

  def initialize operation_id, parameter, transient = "default transient attribute"
    @operation_id = operation_id
    @parameter = parameter
    @transient_attribute = transient
  end
end

TestTransferObject.new("add", [15, 20], 'transient').to_json nil
  # {"json_class":"TestTransferObject","operation_id":"add","parameter":[15,20]}

JSON.dump(TestTransferObject.new("add", [15, 20], 'transient))
  # {"json_class":"TestTransferObject","operation_id":"add","parameter":[15,20]}

JSON.load(JSON.dump(TestTransferObject.new("add", [15, 20], 'transient))).inspect
  # #<TestTransferObject:0x000000018ae188
  #   @operation_id="add",
  #   @parameter=[15, 20],
  #   @transient_attribute="default transient attribute">

JSON.dump(TestTransferObject.new("nested", TestTransferObject.new("op", "param")))
  # {"json_class":"TestTransferObject",
  #  "operation_id":"nested",
  #  "parameter":
  #     {"json_class":"TestTransferObject",
  #      "operation_id":"op",
  #      "parameter":"param"}}

JSON.load(JSON.dump(TestTransferObject.new("nested", TestTransferObject.new("op", "param"))))
  # <TestTransferObject:0x0000000268d308
  #   @operation_id="nested",
  #   @parameter=#<TestTransferObject:0x0000000268d5b0
  #                @operation_id="op",
  #                @parameter="param",
  #                @transient_attribute="default transient attribute">,
  #   @transient_attribute="default transient attribute">

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your tests and changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request