Project

json2ruby

0.0
No commit activity in last 3 years
No release in over 3 years
json2ruby is intended to generate ruby model classes/modules from existing JSON data, e.g. responses from an API.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 10.0
~> 4.1
~> 3.1
~> 0.4
~> 0.10.0
 Project Readme

json2ruby

Build Status Coverage Status Gem Version Dependency Status Gem Downloads

A ruby rool for generating POROs from JSON data. It is intended to generate ruby model classes/modules from existing JSON data, e.g. responses from an API.

The tool will 'fold down' objects with identical fields - i.e. if an object has exactly the same field names and types as another, it will assume they are the same type.

'Root' entities are named after the files that they are parsed from. Entities with no obvious name (items in an array, for instance) are named Unknown<x> where x increments from 1.

Installation

gem install json2ruby

Usage

json2ruby.rb [options] <file.json> [<file.json>....]
Option Flags Default Description
-o, --outputdir ./classes The output directory for Ruby files
-m, --modulename The Ruby module for files
-s, --superclass The superclass for classes
-r, --require Add ruby require to files
-i, --include Add ruby include to files
-e, --extend Add ruby extend to files
-M, --modules Generate Ruby modules, not classes
-a, --attributemethod attr_accessor Use a custom attribute definition method
-c, --collectionmethod attr_accessor Use a custom collection definition method
-t, --types Include type name in attribute definition call
-b, --baseless Don't generate for the root object in each file
-f, --forceoverwrite Overwrite Existing files
-N, --forcenumeric Use Numeric instead of Integer/Float
-v, --verbose Be verbose, List every operation/file

Example

Generate a simple set of POROs from an API response JSON file, be verbose:

./json2ruby.rb -v data.json

Generate a (very) basic set of apotonick/representable compatible representer modules:

./json2ruby.rb -r representable/json-i Representable::JSON -M -a property -c collection data.json 

Notes

The option -m, --modulename can take path module names in FirstModule::Submodule::SubSubModule format, which will produce classes like so:

module FirstModule
  module Submodule
    module SubSubModule
      class JSONObject
      ...
      end
    end
  end
end

The option -N, --forcenumeric can be useful to fold down identical types where an attribute which is a float happens to have an integer value, to avoid generating two identical types.

Documentation

Regenerate the documentation with 'rdoc':

rdoc

Contributing

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