From 2e4f0af00f85ca228bcf5fa919882359411c652a Mon Sep 17 00:00:00 2001 From: hsbt Date: Sun, 12 Apr 2015 08:36:37 +0000 Subject: * ext/json/*, test/json/*: Reverted r50231. Because it's not works with cross-compile environment. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50267 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/json/lib/json/add/bigdecimal.rb | 28 +++++++++++++++++++++ ext/json/lib/json/add/complex.rb | 28 +++++++++++++++++++++ ext/json/lib/json/add/core.rb | 11 ++++++++ ext/json/lib/json/add/date.rb | 34 +++++++++++++++++++++++++ ext/json/lib/json/add/date_time.rb | 50 +++++++++++++++++++++++++++++++++++++ ext/json/lib/json/add/exception.rb | 31 +++++++++++++++++++++++ ext/json/lib/json/add/ostruct.rb | 31 +++++++++++++++++++++++ ext/json/lib/json/add/range.rb | 29 +++++++++++++++++++++ ext/json/lib/json/add/rational.rb | 27 ++++++++++++++++++++ ext/json/lib/json/add/regexp.rb | 30 ++++++++++++++++++++++ ext/json/lib/json/add/struct.rb | 30 ++++++++++++++++++++++ ext/json/lib/json/add/symbol.rb | 25 +++++++++++++++++++ ext/json/lib/json/add/time.rb | 38 ++++++++++++++++++++++++++++ 13 files changed, 392 insertions(+) create mode 100644 ext/json/lib/json/add/bigdecimal.rb create mode 100644 ext/json/lib/json/add/complex.rb create mode 100644 ext/json/lib/json/add/core.rb create mode 100644 ext/json/lib/json/add/date.rb create mode 100644 ext/json/lib/json/add/date_time.rb create mode 100644 ext/json/lib/json/add/exception.rb create mode 100644 ext/json/lib/json/add/ostruct.rb create mode 100644 ext/json/lib/json/add/range.rb create mode 100644 ext/json/lib/json/add/rational.rb create mode 100644 ext/json/lib/json/add/regexp.rb create mode 100644 ext/json/lib/json/add/struct.rb create mode 100644 ext/json/lib/json/add/symbol.rb create mode 100644 ext/json/lib/json/add/time.rb (limited to 'ext/json/lib/json/add') diff --git a/ext/json/lib/json/add/bigdecimal.rb b/ext/json/lib/json/add/bigdecimal.rb new file mode 100644 index 0000000000..0ef69f12e0 --- /dev/null +++ b/ext/json/lib/json/add/bigdecimal.rb @@ -0,0 +1,28 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +defined?(::BigDecimal) or require 'bigdecimal' + +class BigDecimal + # Import a JSON Marshalled object. + # + # method used for JSON marshalling support. + def self.json_create(object) + BigDecimal._load object['b'] + end + + # Marshal the object to JSON. + # + # method used for JSON marshalling support. + def as_json(*) + { + JSON.create_id => self.class.name, + 'b' => _dump, + } + end + + # return the JSON value + def to_json(*) + as_json.to_json + end +end diff --git a/ext/json/lib/json/add/complex.rb b/ext/json/lib/json/add/complex.rb new file mode 100644 index 0000000000..3d653bb50d --- /dev/null +++ b/ext/json/lib/json/add/complex.rb @@ -0,0 +1,28 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +defined?(::Complex) or require 'complex' + +class Complex + + # Deserializes JSON string by converting Real value r, imaginary + # value i, to a Complex object. + def self.json_create(object) + Complex(object['r'], object['i']) + end + + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) + { + JSON.create_id => self.class.name, + 'r' => real, + 'i' => imag, + } + end + + # Stores class name (Complex) along with real value r and imaginary value i as JSON string + def to_json(*) + as_json.to_json + end +end diff --git a/ext/json/lib/json/add/core.rb b/ext/json/lib/json/add/core.rb new file mode 100644 index 0000000000..77d9dc0b20 --- /dev/null +++ b/ext/json/lib/json/add/core.rb @@ -0,0 +1,11 @@ +# This file requires the implementations of ruby core's custom objects for +# serialisation/deserialisation. + +require 'json/add/date' +require 'json/add/date_time' +require 'json/add/exception' +require 'json/add/range' +require 'json/add/regexp' +require 'json/add/struct' +require 'json/add/symbol' +require 'json/add/time' diff --git a/ext/json/lib/json/add/date.rb b/ext/json/lib/json/add/date.rb new file mode 100644 index 0000000000..4288237db1 --- /dev/null +++ b/ext/json/lib/json/add/date.rb @@ -0,0 +1,34 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +require 'date' + +# Date serialization/deserialization +class Date + + # Deserializes JSON string by converting Julian year y, month + # m, day d and Day of Calendar Reform sg to Date. + def self.json_create(object) + civil(*object.values_at('y', 'm', 'd', 'sg')) + end + + alias start sg unless method_defined?(:start) + + # 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, + } + end + + # Stores class name (Date) with Julian year y, month m, day + # d and Day of Calendar Reform sg as JSON string + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/date_time.rb b/ext/json/lib/json/add/date_time.rb new file mode 100644 index 0000000000..5ea42ea656 --- /dev/null +++ b/ext/json/lib/json/add/date_time.rb @@ -0,0 +1,50 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +require 'date' + +# DateTime serialization/deserialization +class DateTime + + # Deserializes JSON string by converting year y, month m, + # day d, hour H, minute M, second S, + # offset of and Day of Calendar Reform sg to DateTime. + def self.json_create(object) + args = object.values_at('y', 'm', 'd', 'H', 'M', 'S') + of_a, of_b = object['of'].split('/') + if of_b and of_b != '0' + args << Rational(of_a.to_i, of_b.to_i) + else + args << of_a + end + args << object['sg'] + civil(*args) + end + + alias start sg unless method_defined?(:start) + + # 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, + 'H' => hour, + 'M' => min, + 'S' => sec, + 'of' => offset.to_s, + 'sg' => start, + } + end + + # Stores class name (DateTime) with Julian year y, month m, + # day d, hour H, minute M, second S, + # offset of and Day of Calendar Reform sg as JSON string + def to_json(*args) + as_json.to_json(*args) + end +end + + diff --git a/ext/json/lib/json/add/exception.rb b/ext/json/lib/json/add/exception.rb new file mode 100644 index 0000000000..e6ad257abf --- /dev/null +++ b/ext/json/lib/json/add/exception.rb @@ -0,0 +1,31 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +# Exception serialization/deserialization +class Exception + + # Deserializes JSON string by constructing new Exception object with message + # m and backtrace b serialized with to_json + def self.json_create(object) + result = new(object['m']) + result.set_backtrace object['b'] + result + end + + # 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, + } + end + + # Stores class name (Exception) with message m and backtrace array + # b as JSON string + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/ostruct.rb b/ext/json/lib/json/add/ostruct.rb new file mode 100644 index 0000000000..da81e107a7 --- /dev/null +++ b/ext/json/lib/json/add/ostruct.rb @@ -0,0 +1,31 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +require 'ostruct' + +# OpenStruct serialization/deserialization +class OpenStruct + + # Deserializes JSON string by constructing new Struct object with values + # v serialized by to_json. + def self.json_create(object) + new(object['t'] || object[:t]) + end + + # 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, + 't' => table, + } + end + + # Stores class name (OpenStruct) with this struct's values v as a + # JSON string. + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/range.rb b/ext/json/lib/json/add/range.rb new file mode 100644 index 0000000000..e61e553cdb --- /dev/null +++ b/ext/json/lib/json/add/range.rb @@ -0,0 +1,29 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +# Range serialization/deserialization +class Range + + # Deserializes JSON string by constructing new Range object with arguments + # a serialized by to_json. + def self.json_create(object) + new(*object['a']) + end + + # 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? ] + } + end + + # Stores class name (Range) with JSON array of arguments a which + # include first (integer), last (integer), and + # exclude_end? (boolean) as JSON string. + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/rational.rb b/ext/json/lib/json/add/rational.rb new file mode 100644 index 0000000000..ee39c20e8d --- /dev/null +++ b/ext/json/lib/json/add/rational.rb @@ -0,0 +1,27 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end +defined?(::Rational) or require 'rational' + +class Rational + # Deserializes JSON string by converting numerator value n, + # denominator value d, to a Rational object. + def self.json_create(object) + Rational(object['n'], object['d']) + end + + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) + { + JSON.create_id => self.class.name, + 'n' => numerator, + 'd' => denominator, + } + end + + # Stores class name (Rational) along with numerator value n and denominator value d as JSON string + def to_json(*) + as_json.to_json + end +end diff --git a/ext/json/lib/json/add/regexp.rb b/ext/json/lib/json/add/regexp.rb new file mode 100644 index 0000000000..2fcbb6fb14 --- /dev/null +++ b/ext/json/lib/json/add/regexp.rb @@ -0,0 +1,30 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +# Regexp serialization/deserialization +class Regexp + + # Deserializes JSON string by constructing new Regexp object with source + # s (Regexp or String) and options o serialized by + # to_json + def self.json_create(object) + new(object['s'], object['o']) + end + + # 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, + } + end + + # Stores class name (Regexp) with options o and source s + # (Regexp or String) as JSON string + def to_json(*) + as_json.to_json + end +end diff --git a/ext/json/lib/json/add/struct.rb b/ext/json/lib/json/add/struct.rb new file mode 100644 index 0000000000..6847cde99b --- /dev/null +++ b/ext/json/lib/json/add/struct.rb @@ -0,0 +1,30 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +# Struct serialization/deserialization +class Struct + + # Deserializes JSON string by constructing new Struct object with values + # v serialized by to_json. + def self.json_create(object) + new(*object['v']) + end + + # 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, + } + end + + # Stores class name (Struct) with Struct values v as a JSON string. + # Only named structs are supported. + def to_json(*args) + as_json.to_json(*args) + end +end diff --git a/ext/json/lib/json/add/symbol.rb b/ext/json/lib/json/add/symbol.rb new file mode 100644 index 0000000000..03dc9a56a5 --- /dev/null +++ b/ext/json/lib/json/add/symbol.rb @@ -0,0 +1,25 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +end + +# Symbol serialization/deserialization +class Symbol + # 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, + } + 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 string value stored in the object to a Symbol + def self.json_create(o) + o['s'].to_sym + end +end diff --git a/ext/json/lib/json/add/time.rb b/ext/json/lib/json/add/time.rb new file mode 100644 index 0000000000..d9834677ac --- /dev/null +++ b/ext/json/lib/json/add/time.rb @@ -0,0 +1,38 @@ +unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED + require 'json' +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 + end + if method_defined?(:tv_nsec) + at(object['s'], Rational(object['n'], 1000)) + else + at(object['s'], object['n'] / 1000) + end + end + + # Returns a hash, that will be turned into a JSON object and represent this + # object. + def as_json(*) + nanoseconds = [ tv_usec * 1000 ] + respond_to?(:tv_nsec) and nanoseconds << tv_nsec + nanoseconds = nanoseconds.max + { + JSON.create_id => self.class.name, + 's' => tv_sec, + 'n' => nanoseconds, + } + 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 -- cgit v1.2.3