diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/json.rb | 52 | ||||
-rw-r--r-- | lib/json/add/core.rb | 144 | ||||
-rw-r--r-- | lib/json/common.rb | 8 | ||||
-rw-r--r-- | lib/json/editor.rb | 32 | ||||
-rw-r--r-- | lib/json/pure/generator.rb | 5 | ||||
-rw-r--r-- | lib/json/pure/parser.rb | 6 | ||||
-rw-r--r-- | lib/json/version.rb | 2 |
7 files changed, 201 insertions, 48 deletions
diff --git a/lib/json.rb b/lib/json.rb index 789b0de..d7bc1a2 100644 --- a/lib/json.rb +++ b/lib/json.rb @@ -1,3 +1,55 @@ +## +# = JavaScript Object Notation (JSON) +# +# JSON is a lightweight data-interchange format. It is easy for us +# humans to read and write. Plus, equally simple for machines to generate or parse. +# JSON is completely language agnostic, making it the ideal interchange format. +# +# Built on two universally available structures: +# 1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array. +# 2. An orderd list of values. More commonly named as an _array_, vector, sequence, or list. +# +# To read more about JSON visit: http://json.org +# +# == Parsing JSON +# +# To parse a JSON string received by another application, or generated within +# your existing application: +# +# require 'json' +# +# my_hash = JSON.parse('{"hello": "goodbye"}') +# puts my_hash["hello"] => "goodbye" +# +# Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects +# the argument to be a string and can't convert objects like a hash or array. +# +# Ruby converts your string into a hash +# +# == Generating JSON +# +# Creating a JSON string for communication or serialization is +# just as simple. +# +# require 'json' +# +# my_hash = {:hello => "goodbye"} +# puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}" +# +# Or an alternative way: +# +# require 'json' +# puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}" +# +# <tt>JSON.generate</tt> only allows objects or arrays to be converted +# to JSON syntax. While <tt>to_json</tt> accepts many Ruby classes +# even though it only acts a method for serialization: +# +# require 'json' +# +# 1.to_json => "1" +# + require 'json/common' module JSON require 'json/version' diff --git a/lib/json/add/core.rb b/lib/json/add/core.rb index 7a901d0..e9850af 100644 --- a/lib/json/add/core.rb +++ b/lib/json/add/core.rb @@ -6,20 +6,32 @@ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED end require 'date' +# Symbol serialization/deserialization class Symbol - def to_json(*a) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, - 's' => to_s, - }.to_json(*a) + 's' => to_s, + } end + # Stores class name (Symbol) with String representation of Symbol as a JSON string. + def to_json(*a) + as_json.to_json(*a) + end + + # Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol def self.json_create(o) o['s'].to_sym end end +# Time serialization/deserialization class Time + + # Deserializes JSON string by converting time since epoch to Time def self.json_create(object) if usec = object.delete('u') # used to be tv_usec -> tv_nsec object['n'] = usec * 1000 @@ -31,34 +43,59 @@ class Time end end - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, - 's' => tv_sec, - 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000 - }.to_json(*args) + 's' => tv_sec, + 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000 + } + end + + # Stores class name (Time) with number of seconds since epoch and number of + # microseconds for Time as JSON string + def to_json(*args) + as_json.to_json(*args) end end +# Date serialization/deserialization class Date + + # Deserializes JSON string by converting Julian year <tt>y</tt>, month + # <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date. def self.json_create(object) civil(*object.values_at('y', 'm', 'd', 'sg')) end alias start sg unless method_defined?(:start) - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, 'y' => year, 'm' => month, 'd' => day, 'sg' => start, - }.to_json(*args) + } + end + + # Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day + # <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string + def to_json(*args) + as_json.to_json(*args) end end +# DateTime serialization/deserialization class DateTime + + # Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>, + # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>, + # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime. def self.json_create(object) args = object.values_at('y', 'm', 'd', 'H', 'M', 'S') of_a, of_b = object['of'].split('/') @@ -73,7 +110,9 @@ class DateTime alias start sg unless method_defined?(:start) - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, 'y' => year, @@ -84,64 +123,121 @@ class DateTime 'S' => sec, 'of' => offset.to_s, 'sg' => start, - }.to_json(*args) + } + end + + # Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>, + # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>, + # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string + def to_json(*args) + as_json.to_json(*args) end end +# Range serialization/deserialization class Range + + # Deserializes JSON string by constructing new Range object with arguments + # <tt>a</tt> serialized by <tt>to_json</tt>. def self.json_create(object) new(*object['a']) end - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { - JSON.create_id => self.class.name, - 'a' => [ first, last, exclude_end? ] - }.to_json(*args) + JSON.create_id => self.class.name, + 'a' => [ first, last, exclude_end? ] + } + end + + # Stores class name (Range) with JSON array of arguments <tt>a</tt> which + # include <tt>first</tt> (integer), <tt>last</tt> (integer), and + # <tt>exclude_end?</tt> (boolean) as JSON string. + def to_json(*args) + as_json.to_json(*args) end end +# Struct serialization/deserialization class Struct + + # Deserializes JSON string by constructing new Struct object with values + # <tt>v</tt> serialized by <tt>to_json</tt>. def self.json_create(object) new(*object['v']) end - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) klass = self.class.name klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!" { JSON.create_id => klass, - 'v' => values, - }.to_json(*args) + 'v' => values, + } + end + + # Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string. + # Only named structs are supported. + def to_json(*args) + as_json.to_json(*args) end end +# Exception serialization/deserialization class Exception + + # Deserializes JSON string by constructing new Exception object with message + # <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt> def self.json_create(object) result = new(object['m']) result.set_backtrace object['b'] result end - def to_json(*args) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, - 'm' => message, - 'b' => backtrace, - }.to_json(*args) + 'm' => message, + 'b' => backtrace, + } + end + + # Stores class name (Exception) with message <tt>m</tt> and backtrace array + # <tt>b</tt> as JSON string + def to_json(*args) + as_json.to_json(*args) end end +# Regexp serialization/deserialization class Regexp + + # Deserializes JSON string by constructing new Regexp object with source + # <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by + # <tt>to_json</tt> def self.json_create(object) new(object['s'], object['o']) end - def to_json(*) + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) { JSON.create_id => self.class.name, 'o' => options, 's' => source, - }.to_json + } + end + + # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt> + # (Regexp or String) as JSON string + def to_json(*) + as_json.to_json end end diff --git a/lib/json/common.rb b/lib/json/common.rb index f8ce2da..1a5f048 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -185,7 +185,7 @@ module JSON # * *indent*: a string used to indent levels (default: ''), # * *space*: a string that is put after, a : or , delimiter (default: ''), # * *space_before*: a string that is put before a : pair delimiter (default: ''), - # * *object_nl*: a string that is put at the end of a JSON object (default: ''), + # * *object_nl*: a string that is put at the end of a JSON object (default: ''), # * *array_nl*: a string that is put at the end of a JSON array (default: ''), # * *allow_nan*: true if NaN, Infinity, and -Infinity should be # generated, otherwise an exception is thrown, if these values are @@ -292,6 +292,7 @@ module JSON result end + # Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_ def recurse_proc(result, &proc) case result when Array @@ -351,13 +352,15 @@ module JSON # Shortuct for iconv. if ::String.method_defined?(:encode) + # Encodes string using Ruby's _String.encode_ def self.iconv(to, from, string) string.encode(to, from) end else require 'iconv' + # Encodes string using _iconv_ library def self.iconv(to, from, string) - Iconv.iconv(to, from, string).first + Iconv.conv(to, from, string) end end @@ -408,6 +411,7 @@ module ::Kernel end end +# Extends any Class to include _json_creatable?_ method. class ::Class # Returns true, if this class can be used to create an instance # from a serialised JSON string. The class has to implement a class diff --git a/lib/json/editor.rb b/lib/json/editor.rb index 3450455..985a554 100644 --- a/lib/json/editor.rb +++ b/lib/json/editor.rb @@ -47,14 +47,14 @@ module JSON # Opens an error dialog on top of _window_ showing the error message # _text_. def Editor.error_dialog(window, text) - dialog = MessageDialog.new(window, Dialog::MODAL, - MessageDialog::ERROR, + dialog = MessageDialog.new(window, Dialog::MODAL, + MessageDialog::ERROR, MessageDialog::BUTTONS_CLOSE, text) dialog.show_all dialog.run rescue TypeError - dialog = MessageDialog.new(Editor.window, Dialog::MODAL, - MessageDialog::ERROR, + dialog = MessageDialog.new(Editor.window, Dialog::MODAL, + MessageDialog::ERROR, MessageDialog::BUTTONS_CLOSE, text) dialog.show_all dialog.run @@ -66,8 +66,8 @@ module JSON # message _text_. If yes was answered _true_ is returned, otherwise # _false_. def Editor.question_dialog(window, text) - dialog = MessageDialog.new(window, Dialog::MODAL, - MessageDialog::QUESTION, + dialog = MessageDialog.new(window, Dialog::MODAL, + MessageDialog::QUESTION, MessageDialog::BUTTONS_YES_NO, text) dialog.show_all dialog.run do |response| @@ -464,7 +464,7 @@ module JSON add_separator add_item("Append new node", ?a, &method(:append_new_node)) add_item("Insert new node before", ?i, &method(:insert_new_node)) - add_separator + add_separator add_item("Collapse/Expand node (recursively)", ?e, &method(:collapse_expand)) @@ -503,7 +503,7 @@ module JSON # Revert the current JSON document in the editor to the saved version. def revert(item) window.instance_eval do - @filename and file_open(@filename) + @filename and file_open(@filename) end end @@ -665,7 +665,7 @@ module JSON collapse_all else self.expanded = true - expand_all + expand_all end end @@ -884,7 +884,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all self.focus = dialog - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT @key = key_input.text type = ALL_TYPES[@type = type_input.active] @@ -936,7 +936,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all self.focus = dialog - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT type = types[type_input.active] @content = case type @@ -981,7 +981,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all self.focus = dialog - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT return @order = order_input.text, reverse_checkbox.active? end @@ -1016,7 +1016,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all self.focus = dialog - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT begin return Regexp.new(regex_input.text, icase_checkbox.active? ? Regexp::IGNORECASE : 0) @@ -1215,7 +1215,7 @@ module JSON end end - # Save the current file as the filename + # Save the current file as the filename def file_save_as filename = select_file('Save as a JSON file') store_file(filename) @@ -1241,7 +1241,7 @@ module JSON rescue SystemCallError => e Editor.error_dialog(self, "Failed to store JSON file: #{e}!") end - + # Load the file named _filename_ into the editor as a JSON document. def load_file(filename) if filename @@ -1333,7 +1333,7 @@ module JSON dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER) dialog.show_all - dialog.run do |response| + dialog.run do |response| if response == Dialog::RESPONSE_ACCEPT return @location = location_input.text end diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb index 44cca60..3687e30 100644 --- a/lib/json/pure/generator.rb +++ b/lib/json/pure/generator.rb @@ -99,7 +99,7 @@ module JSON module Pure module Generator # This class is used to create State instances, that are use to hold data - # while generating a JSON text from a a Ruby data structure. + # while generating a JSON text from a Ruby data structure. class State # Creates a State object from _opts_, which ought to be Hash to create # a new State instance configured by _opts_, something else to create @@ -125,7 +125,7 @@ module JSON # * *indent*: a string used to indent levels (default: ''), # * *space*: a string that is put after, a : or , delimiter (default: ''), # * *space_before*: a string that is put before a : pair delimiter (default: ''), - # * *object_nl*: a string that is put at the end of a JSON object (default: ''), + # * *object_nl*: a string that is put at the end of a JSON object (default: ''), # * *array_nl*: a string that is put at the end of a JSON array (default: ''), # * *check_circular*: is deprecated now, use the :max_nesting option instead, # * *max_nesting*: sets the maximum level of data structure nesting in @@ -212,6 +212,7 @@ module JSON end self end + alias merge configure # Returns the configuration instance variables as a hash, that can be # passed to the configure method. diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb index 8043e67..d612018 100644 --- a/lib/json/pure/parser.rb +++ b/lib/json/pure/parser.rb @@ -41,7 +41,7 @@ module JSON [^*/]| # normal chars /[^*]| # slashes that do not start a nested comment \*[^/]| # asterisks that do not end this comment - /(?=\*/) # single slash before this comment's end + /(?=\*/) # single slash before this comment's end )* \*/ # the End of this comment |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr @@ -162,12 +162,12 @@ module JSON ?n => "\n", ?r => "\r", ?t => "\t", - ?u => nil, + ?u => nil, }) EMPTY_8BIT_STRING = '' if ::String.method_defined?(:encode) - EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT + EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT end def parse_string diff --git a/lib/json/version.rb b/lib/json/version.rb index 86a741a..2175ac0 100644 --- a/lib/json/version.rb +++ b/lib/json/version.rb @@ -1,6 +1,6 @@ module JSON # JSON version - VERSION = '1.5.1' + VERSION = '1.5.4' VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc: VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc: VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: |