[331] | 1 | # mruby-iijson
|
---|
| 2 | [![Build Status](https://travis-ci.org/iij/mruby-iijson.svg?branch=master)](https://travis-ci.org/iij/mruby-iijson)
|
---|
| 3 |
|
---|
| 4 | "mruby-iijson" is JSON module for mruby.
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | ## Implemented Methods:
|
---|
| 8 | - [JSON.dump](http://docs.ruby-lang.org/en/2.2.0/JSON.html#method-i-dump)
|
---|
| 9 | - [JSON.generate](http://docs.ruby-lang.org/en/2.2.0/JSON.html#method-i-generate)
|
---|
| 10 | - [JSON.load](http://docs.ruby-lang.org/en/2.2.0/JSON.html#method-i-load)
|
---|
| 11 | - [JSON.parse](http://docs.ruby-lang.org/en/2.2.0/JSON.html#method-i-parse)
|
---|
| 12 | - `Array#to_json`, `Fixnum#to_json`, `Float#to_json`, `Hash#to_json`, `String#to_json`, ...
|
---|
| 13 |
|
---|
| 14 | ## Example:
|
---|
| 15 |
|
---|
| 16 | ```Ruby
|
---|
| 17 | str = '{
|
---|
| 18 | "Image": {
|
---|
| 19 | "Width": 800,
|
---|
| 20 | "Height": 600,
|
---|
| 21 | "Title": "View from 15th Floor",
|
---|
| 22 | "Thumbnail": {
|
---|
| 23 | "Url": "http://www.example.com/image/481989943",
|
---|
| 24 | "Height": 125,
|
---|
| 25 | "Width": "100"
|
---|
| 26 | },
|
---|
| 27 | "IDs": [116, 943, 234, 38793]
|
---|
| 28 | }
|
---|
| 29 | }'
|
---|
| 30 | p JSON.parse(str)
|
---|
| 31 |
|
---|
| 32 | h = {
|
---|
| 33 | :precision => "zip",
|
---|
| 34 | :Latitude => 37.7668,
|
---|
| 35 | :Longitude => -122.3959,
|
---|
| 36 | :Address => "",
|
---|
| 37 | :City => "SAN FRANCISCO",
|
---|
| 38 | :State => "CA",
|
---|
| 39 | :Zip => "94107",
|
---|
| 40 | :Country => "US"
|
---|
| 41 | }
|
---|
| 42 | p h.to_json
|
---|
| 43 | puts JSON.generate(h, {:pretty_print => true, :indent_with => 2})
|
---|
| 44 | ```
|
---|
| 45 |
|
---|
| 46 | ```
|
---|
| 47 | {"Image"=>{"Width"=>800, "Height"=>600, "Title"=>"View from 15th Floor", "Thumbnail"=>{"Url"=>"http://www.example.com/image/481989943", "Height"=>125, "Width"=>"100"}, "IDs"=>[116, 943, 234, 38793]}}
|
---|
| 48 | "{\"precision\":\"zip\",\"Latitude\":37.766800000000003,\"Longitude\":-122.3959,\"Address\":\"\",\"City\":\"SAN FRANCISCO\",\"State\":\"CA\",\"Zip\":\"94107\",\"Country\":\"US\"}"
|
---|
| 49 | {
|
---|
| 50 | "precision": "zip",
|
---|
| 51 | "Latitude": 37.766800000000003,
|
---|
| 52 | "Longitude": -122.3959,
|
---|
| 53 | "Address": "",
|
---|
| 54 | "City": "SAN FRANCISCO",
|
---|
| 55 | "State": "CA",
|
---|
| 56 | "Zip": "94107",
|
---|
| 57 | "Country": "US"
|
---|
| 58 | }
|
---|
| 59 | ```
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | ## Caveats
|
---|
| 63 |
|
---|
| 64 | - JSON.generate won't return if input Array/Hash has circular reference.
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | ## License
|
---|
| 68 |
|
---|
| 69 | Copyright (c) 2014 Internet Initiative Japan Inc.
|
---|
| 70 |
|
---|
| 71 | Permission is hereby granted, free of charge, to any person obtaining a
|
---|
| 72 | copy of this software and associated documentation files (the "Software"),
|
---|
| 73 | to deal in the Software without restriction, including without limitation
|
---|
| 74 | the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
| 75 | and/or sell copies of the Software, and to permit persons to whom the
|
---|
| 76 | Software is furnished to do so, subject to the following conditions:
|
---|
| 77 |
|
---|
| 78 | The above copyright notice and this permission notice shall be included in
|
---|
| 79 | all copies or substantial portions of the Software.
|
---|
| 80 |
|
---|
| 81 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
| 82 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
| 83 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
| 84 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
| 85 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 86 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
---|
| 87 | DEALINGS IN THE SOFTWARE.
|
---|