https://github.com/minuscorp/jsonconvertible
A lightweight Ruby mixin for encoding and decoding JSON data.
https://github.com/minuscorp/jsonconvertible
encoder-decoder json mixin ruby
Last synced: 24 days ago
JSON representation
A lightweight Ruby mixin for encoding and decoding JSON data.
- Host: GitHub
- URL: https://github.com/minuscorp/jsonconvertible
- Owner: minuscorp
- License: apache-2.0
- Created: 2021-05-14T10:07:30.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-14T18:43:48.000Z (about 5 years ago)
- Last Synced: 2025-10-09T10:46:56.887Z (8 months ago)
- Topics: encoder-decoder, json, mixin, ruby
- Language: Ruby
- Homepage:
- Size: 37.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSONConvertible
[](https://travis-ci.org/minuscorp/JSONConvertible)
[](https://badge.fury.io/rb/json_convertible)
[](https://coveralls.io/github/minuscorp/JSONConvertible?branch=main)
A lightweight Ruby mixin for encoding and decoding JSON data classes.
## Requirements
Ruby 2.4.0 or higher.
## Install
```no-highlight
gem install json_convertible
```
Alternatively you can use bundler`:
```ruby
gem "json_convertible"
```
## Usage
From within a class:
```ruby
require "json_convertible"
class Example
include JSONConvertible
end
```
This produces the mixin to be included in the class, which allows the call of two main methods:
```ruby
object = Example.new
# Encode to an object dictionary
object.to_object_dictionary # => {}
# Decode from an object dictionary
Example.from_json({}) # => #
```
## Basic usage
For straightforward one-to-one attribute mapping the mixin works out-of-box:
```ruby
class AttributedExample
include JSONConvertible
attr_accessor :one
attr_accessor :two
def initialize(one: nil, two: nil)
self.one = one
self.two = two
end
end
object = AttributedExample.new(one: "Hello", two: Time.at(0))
object.to_object_dictionary # => => {"one"=>"Hello", "two"=>1970-01-01 01:00:00 +0100}
deserialized = AttributedExample.from_json!(
{ "one" => "Hello",
"two" => Time.at(0).strftime("%G-W%V-%uT%R%:z")
}
) # => #
```
## Advanced usage
1. Custom type properties:
```ruby
class CustomExample
include JSONConvertible
def self.attribute_to_type_map
{
:@example => Example
}
end
# @return [Example]
attr_accessor :example
def initialize(example: nil)
self.example = example
end
end
```
2. Array type properties:
```ruby
class ArrayExample
include JSONConvertible
def self.attribute_to_type_map
{
:@example => Example
}
end
# @return [Array(Example)]
attr_accessor :example_array
def initialize(example_array: nil)
self.example_array = example_array
end
end
```
3. Multiple attribute array object:
```ruby
class MultipleAttributeArrayExample
include JSONConvertible
attr_accessor :one_array_attribute
attr_accessor :other_array_attribute
def initialize(one_array_attribute: nil, other_array_attribute: nil)
self.one_array_attribute = one_array_attribute
self.other_array_attribute = other_array_attribute
end
def self.map_enumerable_type(enumerable_property_name: nil, current_json_object: nil)
if enumerable_property_name == :@one_array_attribute
object = OpenStruct.new(current_json_object)
object.is_from_one_array_attribute = true
return object
elsif enumerable_property_name == :@other_array_attribute
object = OpenStruct.new(current_json_object)
object.is_from_other_array_attribute = true
return object
end
end
end
```
4. Required attribute objects:
```ruby
class ExampleWithRequiredParams
include JSONConvertible
attr_reader :one_attribute
def initialize(one_attribute:)
@one_attribute = one_attribute
end
end
```
5. Custom value mapping for attributes:
```ruby
class CustomExample
include JSONConvertible
def self.json_to_attribute_name_proc_map
{
:@example => proc { |_| "foo" }
}
end
attr_accessor :example
def initialize(example: nil)
self.example = example
end
end
```
* All of this cases can be seen in the specs
## Acknowledgements
This project is inspired on the `JSONConvertible` util created by the [_fastlane_ CI team](https://github.com/fastlane/ci)