Project

kanade

0.0
No commit activity in last 3 years
No release in over 3 years
Strong-typed JSON to Ruby object serializer/deserializer
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
>= 0
>= 0
~> 3.5
>= 0

Runtime

 Project Readme

Kanade - Harmonize External API with Strong-typed Ruby Object Build Status

A JSON parser (intended for client). Like Gson / Jackson / JSON.NET, but for Ruby.

かなでる 【奏でる】 ichidan verb → conjugation / intransitive: to play an instrument (esp. string instruments)

Kanade harmonize your Ruby and any JSON-REST API into strong-typed Ruby object.

Installation

gem 'kanade'

Using Kanade

Create a DTO file, for example:

class ProductList < Kanade::Dto
  field :id, as: :fixnum
  field :products, as: :list, of: Product
end

class Product < Kanade::Dto
  field :id, as: :fixnum
  field :name, as: :string
  field :expire_at, as: :date_time
  field :price, as: :big_decimal
  field :available, as: :bool
end

Deserialize from JSON:

json = '
{
  "products": [
  {
    "price": "35.5"
  }]
}
'

engine = Kanade::Engine.new
engine.configure do |config|
  config.contract = :camel_case
  config.enum = :upper_snake_case
end

list = engine.deserialize(ProductList, json)
list.products.first.price # => A big decimal of 35.5

Syntax

To define a field:

field :field_name, # field name in Ruby
      as: :int, # Data type
      with: 'TheFieldName', # Nama field di JSON apabila tidak standar

Built in data type:

  • :big_decimal
  • :bool
  • :fixnum
  • :float
  • :string
  • :symbol
  • :time
  • Atau, objek apapun yang meng-extend Kanade::Dto

Naming strategy

Internally, as Ruby object, Kanade will respect Ruby convention by using snake_case naming. However, your JSON-REST API may have another convention. For example, in Java (GSON/Jackson), by default you will have camelCase naming. Kanade can understand this different by specifying a naming strategy.

engine = Kanade::Engine.new
engine.configure |c|
  c.naming_strategy :pascal_case
end

Kanade will automatically convert the naming from JSON to Ruby object and vice versa.