Project

json_world

0.04
Low commit activity in last 3 years
No release in over a year
Provides DSL to define JSON Schema representation of your class.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

JsonWorld

Provides DSL to define JSON Schema representation of your class.

Usage

.property and .link

class User
  include JsonWorld::DSL

  property(
    :id,
    example: 1,
    type: Integer,
  )

  property(
    :name,
    example: "alice",
    pattern: /^\w{5}$/,
    type: String,
  )

  link(
    :get_user,
    path: "/users/:user_id",
  )

  attr_reader :id, :name

  # @param [Integer] id
  # @param [String] name
  def initialize(id: nil, name: nil)
    @id = id
    @name = name
  end
end

#to_json and .to_json_schema

User.new(id: 1, name: "alice").to_json
# '{"id":1,"name":"alice"}'

User.to_json_schema
# '{
#   "links": [
#     {
#       "href": "/users/:user_id",
#       "title": "get_user"
#     }
#   ],
#   "properties": {
#     "id": {
#       "example": 1,
#       "type": "integer"
#     },
#     "name": {
#       "example": "alice",
#       "pattern": "^\\w{5}$",
#       "type": "string"
#     }
#   },
#   "required": [
#     "id",
#     "name"
#   ]
# }'

See our tests for more examples.

Installation

Add this line to your application's Gemfile:

gem "json_world"