{"id":16060534,"url":"https://github.com/aklaiber/json_factory","last_synced_at":"2025-03-18T05:30:26.651Z","repository":{"id":56879541,"uuid":"65148390","full_name":"aklaiber/json_factory","owner":"aklaiber","description":"JsonFactory is a Easy DSL to create JSON with focus on performance and flexibility.","archived":false,"fork":false,"pushed_at":"2019-08-05T14:42:41.000Z","size":65,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T21:05:22.062Z","etag":null,"topics":["dsl","jbuilder","json","json-structure","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aklaiber.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-07T18:38:33.000Z","updated_at":"2022-04-12T17:35:47.000Z","dependencies_parsed_at":"2022-08-20T11:40:40.642Z","dependency_job_id":null,"html_url":"https://github.com/aklaiber/json_factory","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aklaiber%2Fjson_factory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aklaiber%2Fjson_factory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aklaiber%2Fjson_factory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aklaiber%2Fjson_factory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aklaiber","download_url":"https://codeload.github.com/aklaiber/json_factory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243646650,"owners_count":20324585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dsl","jbuilder","json","json-structure","ruby"],"created_at":"2024-10-09T04:05:13.024Z","updated_at":"2025-03-18T05:30:25.982Z","avatar_url":"https://github.com/aklaiber.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/aklaiber/json_factory.svg?branch=master)](https://travis-ci.org/aklaiber/json_factory)\n\n# JsonFactory\n\nJsonFactory is a Easy DSL to create JSON with focus on performance and flexibility. \n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'json_factory'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install json_factory\n\n## Usage\n\n| DSL Method                           | Description                                                         | \n| ------------------------------------ |:------------------------------------------------------------------- |  \n| [value](#value-method)               | Generates a JSON value.                                             |   \n| [object](#object-method)             | Generates a JSON object.                                            |   \n| [member](#member-method)             | Adds a key-value pair to a JSON object.                             |\n| [array](#array-method)               | Generates a JSON array.                                             | \n| [object_array](#object_array-method) | Generates a JSON array.                                             |\n| [element](#element-method)           | Adds a value to a JSON array.                                       | \n| [partial](#partial-method)           | Loads the given partial and evaluates it using the local variables. |\n| [cache](#cache-method)               | Caches the given content under the specified key.                   |\n| [object_if](#object_if-method)       | Generates a JSON object if condition is true.                       |\n\n##### value method \n\n```ruby\nfactory = \u003c\u003c-RUBY\n  value nil\nRUBY\n\nputs JSONFactory.build(factory) # =\u003e null\n```\n\n##### object method \n\n```ruby\nfactory = \u003c\u003c-RUBY\n  object do\n    member :data do\n      object do\n        member :id, object.id\n      end\n    end \n  end\nRUBY\n\n# test data \ntest_object = OpenStruct.new(id: 1)\n\nputs JSONFactory.build(factory, object: test_object) # =\u003e {\"data\":{\"id\":1}}\n```\n\n##### member method \n\n```ruby\nfactory = \u003c\u003c-RUBY\n  object do \n    member :foo, 'bar' \n  end\nRUBY\n\nputs JSONFactory.build(factory) # =\u003e {\"foo\":\"bar\"}\n```\n\n##### array method\n\n```ruby\nfactory = \u003c\u003c-RUBY\n  array do\n    objects.each do |test_object|   \n      element :id, test_object.id\n    end\n  end\nRUBY\n\n# test data \ntest_object_1 = OpenStruct.new(id: 1)\ntest_object_2 = OpenStruct.new(id: 2)\n\nputs JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # =\u003e [{\"id\": 1},{\"id\":2}]\n```\n\n##### object_array method\n\n```ruby\nfactory = \u003c\u003c-RUBY\n  object_array objects do |test_object|\n    member :id, test_object.id\n  end\nRUBY\n\n# test data \ntest_object_1 = OpenStruct.new(id: 1)\ntest_object_2 = OpenStruct.new(id: 2)\n\nputs JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # =\u003e [{\"id\":1},{\"id\":2}]\n```\n\n##### element method\n\n```ruby\nfactory = \u003c\u003c-RUBY\n  array do\n    objects.each do |test_object|   \n      element :id, test_object.id\n    end\n  end\nRUBY\n\n# test data \ntest_object_1 = OpenStruct.new(id: 1)\ntest_object_2 = OpenStruct.new(id: 2)\n\nputs JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # =\u003e [{\"id\": 1},{\"id\":2}]\n```\n\n##### partial method\n\n```ruby\n# tmp/_test_partial.jfactory\nmember :id, test_object.id\nmember :name, test_object.name\n```  \n\n```ruby\n# tmp/test.jfactory\nobject do\n  partial 'tmp/test_partial', test_object: object\nend\n``` \n\n```ruby\n# test data\ntest_object = OpenStruct.new(id: '1', name: 'TestObject1')\n\nputs JSONFactory.build('tmp/test.jfactory', object: test_object).build # =\u003e { \"id\": 1, name: \"TestObject1\" }\n```  \n\n##### cache method\n\n```ruby\nfactory = \u003c\u003c-RUBY\n  object do\n    member :data do\n      object do\n        cache 'test-cache-key' do\n          member :id, object.id\n          member :name, object.name\n        end\n      end\n    end\n  end\nRUBY\n\n# test data \ntest_object = OpenStruct.new(id: '1', name: 'TestObject1')\n\n# set cache store\nJSONFactory::Cache.instance.store = ActiveSupport::Cache::MemoryStore.new\n\nputs JSONFactory.build(factory, object: test_object) # =\u003e { \"data\": { \"id\": \"1\", \"name\": \"TestObject1\" } }\n```\n\n##### object_if method\n\n```ruby\nfactory = \u003c\u003c-RUBY\n  object do\n    member :data do\n      object_if true do\n        member :foo, 'bar'\n      end\n    end \n  end\nRUBY\n\nputs JSONFactory.build(factory) # =\u003e { \"data\": { \"foo\": \"bar\" } }\n```\n```ruby\nfactory = \u003c\u003c-RUBY\n  object do\n    member :data do\n      object_if false do\n        member :foo, 'bar'\n      end\n    end \n  end\nRUBY\n\nputs JSONFactory.build(factory) # =\u003e { \"data\": null }\n```\n\n##### Load jfactory files\n\n```ruby\n# tmp/test.jfactory\nmember :id, test_object.id\nmember :name, test_object.name\n``` \n\n```ruby\n# test data\ntest_object = OpenStruct.new(id: '1', name: 'TestObject1')\n\nputs JSONFactory.build('tmp/test.jfactory', object: test_object).build # =\u003e { \"id\": 1, name: \"TestObject1\" }\n```   \n\n## Development\n\nTo install this gem onto your local machine, run `bundle exec rake install`. \n\n## Contributing\n\nBug 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](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faklaiber%2Fjson_factory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faklaiber%2Fjson_factory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faklaiber%2Fjson_factory/lists"}