Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json_pretty_generate

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json_pretty_generate

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

JSON Pretty Generate

Small utility method for pretty generating JSON

Installation

Add this line to your application's Gemfile:

gem 'json_pretty_generate'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install json_pretty_generate

Usage

There is a single ::call method.

Argument is a Hash

When the argument is a Hash JSON will be returned with line breaks

obj = { a: 1 }
JSONPrettyGenerate.(obj)
# => "{\n  \"a\": 1\n}"

Argument is a String

When the argument is JSON it returns JSON with line breaks

obj = "{\"a\":1}"
JSONPrettyGenerate.(obj)
# => "{\n  \"a\": 1\n}"

When the argument is a string that is not JSON it returns the argument

obj = "a=1"
JSONPrettyGenerate.(obj)
# => "a=1"

Argument is not a String or a Hash

When the argument type is not a String or Hash it will return nil

obj = 1
JSONPrettyGenerate.(obj)
# => nil

Filtering with a block

You can use a block to filter the obj to a subset of the data. Strings or symbols can be passed to the block argument.

obj = { data: { body: { a: 1 } } }
JSONPrettyGenerate.(obj) { |o| o[:data][:body] }
# => "{\n  \"a\": 1\n}"

Development

To run the specs

  $  bundle exec rake

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nikkypx/json_pretty_generate.

License

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

FAQs

Package last updated on 15 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc