Skip to content

A ruby tool for generating POROs from JSON data.

License

Notifications You must be signed in to change notification settings

randyv12/json2ruby

 
 

Repository files navigation

git test

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

About

A ruby tool for generating POROs from JSON data.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%