0.0
No commit activity in last 3 years
No release in over 3 years
JsonFactory is a easy DSL to create JSON structures with a development focus on performance.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Build Status

JsonFactory

JsonFactory is a Easy DSL to create JSON with focus on performance and flexibility.

Installation

Add this line to your application's Gemfile:

gem 'json_factory'

And then execute:

$ bundle

Or install it yourself as:

$ gem install json_factory

Usage

DSL Method Description
value Generates a JSON value.
object Generates a JSON object.
member Adds a key-value pair to a JSON object.
array Generates a JSON array.
object_array Generates a JSON array.
element Adds a value to a JSON array.
partial Loads the given partial and evaluates it using the local variables.
cache Caches the given content under the specified key.
object_if Generates a JSON object if condition is true.
value method
factory = <<-RUBY
  value nil
RUBY

puts JSONFactory.build(factory) # => null
object method
factory = <<-RUBY
  object do
    member :data do
      object do
        member :id, object.id
      end
    end 
  end
RUBY

# test data 
test_object = OpenStruct.new(id: 1)

puts JSONFactory.build(factory, object: test_object) # => {"data":{"id":1}}
member method
factory = <<-RUBY
  object do 
    member :foo, 'bar' 
  end
RUBY

puts JSONFactory.build(factory) # => {"foo":"bar"}
array method
factory = <<-RUBY
  array do
    objects.each do |test_object|   
      element :id, test_object.id
    end
  end
RUBY

# test data 
test_object_1 = OpenStruct.new(id: 1)
test_object_2 = OpenStruct.new(id: 2)

puts JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # => [{"id": 1},{"id":2}]
object_array method
factory = <<-RUBY
  object_array objects do |test_object|
    member :id, test_object.id
  end
RUBY

# test data 
test_object_1 = OpenStruct.new(id: 1)
test_object_2 = OpenStruct.new(id: 2)

puts JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # => [{"id":1},{"id":2}]
element method
factory = <<-RUBY
  array do
    objects.each do |test_object|   
      element :id, test_object.id
    end
  end
RUBY

# test data 
test_object_1 = OpenStruct.new(id: 1)
test_object_2 = OpenStruct.new(id: 2)

puts JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # => [{"id": 1},{"id":2}]
partial method
# tmp/_test_partial.jfactory
member :id, test_object.id
member :name, test_object.name
# tmp/test.jfactory
object do
  partial 'tmp/test_partial', test_object: object
end
# test data
test_object = OpenStruct.new(id: '1', name: 'TestObject1')

puts JSONFactory.build('tmp/test.jfactory', object: test_object).build # => { "id": 1, name: "TestObject1" }
cache method
factory = <<-RUBY
  object do
    member :data do
      object do
        cache 'test-cache-key' do
          member :id, object.id
          member :name, object.name
        end
      end
    end
  end
RUBY

# test data 
test_object = OpenStruct.new(id: '1', name: 'TestObject1')

# set cache store
JSONFactory::Cache.instance.store = ActiveSupport::Cache::MemoryStore.new

puts JSONFactory.build(factory, object: test_object) # => { "data": { "id": "1", "name": "TestObject1" } }
object_if method
factory = <<-RUBY
  object do
    member :data do
      object_if true do
        member :foo, 'bar'
      end
    end 
  end
RUBY

puts JSONFactory.build(factory) # => { "data": { "foo": "bar" } }
factory = <<-RUBY
  object do
    member :data do
      object_if false do
        member :foo, 'bar'
      end
    end 
  end
RUBY

puts JSONFactory.build(factory) # => { "data": null }
Load jfactory files
# tmp/test.jfactory
member :id, test_object.id
member :name, test_object.name
# test data
test_object = OpenStruct.new(id: '1', name: 'TestObject1')

puts JSONFactory.build('tmp/test.jfactory', object: test_object).build # => { "id": 1, name: "TestObject1" }

Development

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/aklaiber/json_factory. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.