{"id":18362392,"url":"https://github.com/minuscorp/jsonconvertible","last_synced_at":"2026-05-18T03:03:31.351Z","repository":{"id":68063679,"uuid":"367323304","full_name":"minuscorp/JSONConvertible","owner":"minuscorp","description":"A lightweight Ruby mixin for encoding and decoding JSON data.","archived":false,"fork":false,"pushed_at":"2021-05-14T18:43:48.000Z","size":38,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-09T10:46:56.887Z","etag":null,"topics":["encoder-decoder","json","mixin","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/minuscorp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-14T10:07:30.000Z","updated_at":"2021-05-29T11:36:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"a09d0ab0-11f6-4b90-82fd-febc28546ee9","html_url":"https://github.com/minuscorp/JSONConvertible","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/minuscorp/JSONConvertible","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minuscorp%2FJSONConvertible","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minuscorp%2FJSONConvertible/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minuscorp%2FJSONConvertible/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minuscorp%2FJSONConvertible/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/minuscorp","download_url":"https://codeload.github.com/minuscorp/JSONConvertible/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minuscorp%2FJSONConvertible/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33163413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T22:39:12.733Z","status":"online","status_checked_at":"2026-05-18T02:00:06.436Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["encoder-decoder","json","mixin","ruby"],"created_at":"2024-11-05T22:40:39.370Z","updated_at":"2026-05-18T03:03:31.331Z","avatar_url":"https://github.com/minuscorp.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSONConvertible\n[![Build Status](https://travis-ci.org/minuscorp/JSONConvertible.svg?branch=main)](https://travis-ci.org/minuscorp/JSONConvertible) \n[![Gem Version](https://badge.fury.io/rb/json_convertible.svg)](https://badge.fury.io/rb/json_convertible)\n[![Coverage Status](https://coveralls.io/repos/github/minuscorp/JSONConvertible/badge.svg?branch=main)](https://coveralls.io/github/minuscorp/JSONConvertible?branch=main)\n\nA lightweight Ruby mixin for encoding and decoding JSON data classes.\n\n## Requirements\n\nRuby 2.4.0 or higher.\n\n## Install\n\n```no-highlight\ngem install json_convertible\n```\n\nAlternatively you can use bundler`:\n\n```ruby\ngem \"json_convertible\"\n```\n\n## Usage\n\nFrom within a class:\n\n```ruby\nrequire \"json_convertible\"\n\nclass Example\n  include JSONConvertible\nend\n``` \n\nThis produces the mixin to be included in the class, which allows the call of two main methods:\n\n```ruby\nobject = Example.new\n\n# Encode to an object dictionary\nobject.to_object_dictionary # =\u003e {}\n\n# Decode from an object dictionary\nExample.from_json({}) # =\u003e #\u003cExample\u003e\n```\n\n## Basic usage\n\nFor straightforward one-to-one attribute mapping the mixin works out-of-box:\n\n```ruby\nclass AttributedExample\n  include JSONConvertible\n  \n  attr_accessor :one\n  attr_accessor :two\n\n  def initialize(one: nil, two: nil)\n    self.one = one\n    self.two = two\n  end\nend\n\nobject = AttributedExample.new(one: \"Hello\", two: Time.at(0))\n\nobject.to_object_dictionary # =\u003e =\u003e {\"one\"=\u003e\"Hello\", \"two\"=\u003e1970-01-01 01:00:00 +0100}\n\ndeserialized = AttributedExample.from_json!(\n                { \"one\" =\u003e \"Hello\", \n                  \"two\" =\u003e Time.at(0).strftime(\"%G-W%V-%uT%R%:z\") \n                }\n               ) # =\u003e #\u003cAttributedExample @one=\"Hello\", @two=1970-01-01 01:00:00 +0100\u003e\n```\n\n## Advanced usage\n\n1. Custom type properties:\n\n```ruby\nclass CustomExample\n  include JSONConvertible\n\n  def self.attribute_to_type_map\n    {\n      :@example =\u003e Example\n    }\n  end \n\n  # @return [Example]\n  attr_accessor :example\n\n  def initialize(example: nil)\n    self.example = example\n  end\nend\n```\n\n2. Array type properties:\n\n```ruby\nclass ArrayExample\n  include JSONConvertible\n\n  def self.attribute_to_type_map\n    {\n      :@example =\u003e Example\n    }\n  end\n\n  # @return [Array(Example)]\n  attr_accessor :example_array\n\n  def initialize(example_array: nil)\n    self.example_array = example_array\n  end\nend\n```\n\n3. Multiple attribute array object:\n\n```ruby\nclass MultipleAttributeArrayExample\n  include JSONConvertible\n\n  attr_accessor :one_array_attribute\n\n  attr_accessor :other_array_attribute\n\n  def initialize(one_array_attribute: nil, other_array_attribute: nil)\n    self.one_array_attribute = one_array_attribute\n    self.other_array_attribute = other_array_attribute\n  end\n\n  def self.map_enumerable_type(enumerable_property_name: nil, current_json_object: nil)\n    if enumerable_property_name == :@one_array_attribute\n      object = OpenStruct.new(current_json_object)\n      object.is_from_one_array_attribute = true\n      return object\n    elsif enumerable_property_name == :@other_array_attribute\n      object = OpenStruct.new(current_json_object)\n      object.is_from_other_array_attribute = true\n      return object\n    end\n  end\nend\n```\n\n4. Required attribute objects:\n\n```ruby\nclass ExampleWithRequiredParams\n  include JSONConvertible\n\n  attr_reader :one_attribute\n\n  def initialize(one_attribute:)\n    @one_attribute = one_attribute\n  end\nend\n```\n\n5. Custom value mapping for attributes:\n\n```ruby\nclass CustomExample\n  include JSONConvertible\n\n  def self.json_to_attribute_name_proc_map\n    {\n      :@example =\u003e proc { |_| \"foo\" }\n    }\n  end \n\n  attr_accessor :example\n\n  def initialize(example: nil)\n    self.example = example\n  end\nend\n```\n\n* All of this cases can be seen in the specs\n\n## Acknowledgements\n\nThis project is inspired on the `JSONConvertible` util created by the [_fastlane_ CI team](https://github.com/fastlane/ci)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminuscorp%2Fjsonconvertible","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fminuscorp%2Fjsonconvertible","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminuscorp%2Fjsonconvertible/lists"}