Project

safe_parse

0.0
No commit activity in last 3 years
No release in over 3 years
Parse json safely with a default return value.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0
~> 3.0
 Project Readme

SafeParse

Parse json safely

Installation

Add this line to your application's Gemfile:

gem 'safe_parse'

And then execute:

$ bundle

Or install it yourself as:

$ gem install safe_parse

Usage

Parsing a json string safely.

irb> json_string = JSON.generate({abc: 123})
=> "{\"abc\":123}"
irb> SafeParse.call(json_string)
=> {"abc"=>123}

Parsing an invalid json string will default to {}.

irb> invalid_json_string = "abc123"
=> "abc123"
irb> SafeParse.call(invalid_json_string)
=> {}

SafeParse can take a default to return if the json parsing fails.

irb> SafeParse.call(invalid_json_string, {abc: 123})
=> {:abc=>123}

Safe parse can also take a ruby hash and it correctly.

irb> SafeParse.call({ruby: "hash"})
=> {:ruby=>"hash"}

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/gogogarrett/safe_parse. 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.