Add missing files of r34971,

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34973 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
naruse 2012-03-11 17:42:18 +00:00
parent b7df3e9f4e
commit 199d1fa924
11 changed files with 472 additions and 0 deletions

156
ext/json/fbuffer/fbuffer.h Normal file
View File

@ -0,0 +1,156 @@
#ifndef _FBUFFER_H_
#define _FBUFFER_H_
#include <assert.h>
#include "ruby.h"
#ifdef HAVE_RUBY_ENCODING_H
#include "ruby/encoding.h"
#define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
#else
#define FORCE_UTF8(obj)
#endif
/* We don't need to guard objects for rbx, so let's do nothing at all. */
#ifndef RB_GC_GUARD
#define RB_GC_GUARD(object)
#endif
typedef struct FBufferStruct {
unsigned long initial_length;
char *ptr;
unsigned long len;
unsigned long capa;
} FBuffer;
#define FBUFFER_INITIAL_LENGTH_DEFAULT 1024
#define FBUFFER_PTR(fb) (fb->ptr)
#define FBUFFER_LEN(fb) (fb->len)
#define FBUFFER_CAPA(fb) (fb->capa)
#define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
static FBuffer *fbuffer_alloc(unsigned long initial_length);
static void fbuffer_free(FBuffer *fb);
static void fbuffer_clear(FBuffer *fb);
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
static void fbuffer_append_long(FBuffer *fb, long number);
static void fbuffer_append_char(FBuffer *fb, char newchr);
static FBuffer *fbuffer_dup(FBuffer *fb);
static VALUE fbuffer_to_s(FBuffer *fb);
static FBuffer *fbuffer_alloc(unsigned long initial_length)
{
FBuffer *fb;
if (initial_length <= 0) initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
fb = ALLOC(FBuffer);
memset((void *) fb, 0, sizeof(FBuffer));
fb->initial_length = initial_length;
return fb;
}
static void fbuffer_free(FBuffer *fb)
{
if (fb->ptr) ruby_xfree(fb->ptr);
ruby_xfree(fb);
}
static void fbuffer_clear(FBuffer *fb)
{
fb->len = 0;
}
static void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
{
unsigned long required;
if (!fb->ptr) {
fb->ptr = ALLOC_N(char, fb->initial_length);
fb->capa = fb->initial_length;
}
for (required = fb->capa; requested > required - fb->len; required <<= 1);
if (required > fb->capa) {
REALLOC_N(fb->ptr, char, required);
fb->capa = required;
}
}
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
{
if (len > 0) {
fbuffer_inc_capa(fb, len);
MEMCPY(fb->ptr + fb->len, newstr, char, len);
fb->len += len;
}
}
static void fbuffer_append_str(FBuffer *fb, VALUE str)
{
const char *newstr = StringValuePtr(str);
unsigned long len = RSTRING_LEN(str);
RB_GC_GUARD(str);
fbuffer_append(fb, newstr, len);
}
static void fbuffer_append_char(FBuffer *fb, char newchr)
{
fbuffer_inc_capa(fb, 1);
*(fb->ptr + fb->len) = newchr;
fb->len++;
}
static void freverse(char *start, char *end)
{
char c;
while (end > start) {
c = *end, *end-- = *start, *start++ = c;
}
}
static long fltoa(long number, char *buf)
{
static char digits[] = "0123456789";
long sign = number;
char* tmp = buf;
if (sign < 0) number = -number;
do *tmp++ = digits[number % 10]; while (number /= 10);
if (sign < 0) *tmp++ = '-';
freverse(buf, tmp - 1);
return tmp - buf;
}
static void fbuffer_append_long(FBuffer *fb, long number)
{
char buf[20];
unsigned long len = fltoa(number, buf);
fbuffer_append(fb, buf, len);
}
static FBuffer *fbuffer_dup(FBuffer *fb)
{
unsigned long len = fb->len;
FBuffer *result;
assert(len > 0);
if (len > 0) {
result = fbuffer_alloc(len);
fbuffer_append(result, FBUFFER_PAIR(fb));
}
return result;
}
static VALUE fbuffer_to_s(FBuffer *fb)
{
VALUE result = rb_str_new(FBUFFER_PAIR(fb));
fbuffer_free(fb);
FORCE_UTF8(result);
return result;
}
#endif

View File

@ -0,0 +1,21 @@
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json'
end
defined?(::BigDecimal) or require 'bigdecimal'
class BigDecimal
def self.json_create(object)
BigDecimal._load object['b']
end
def as_json(*)
{
JSON.create_id => self.class.name,
'b' => _dump,
}
end
def to_json(*)
as_json.to_json
end
end

View File

@ -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 <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)
# 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 <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

View File

@ -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 <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('/')
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 <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

View File

@ -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
# <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
# 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 <tt>m</tt> and backtrace array
# <tt>b</tt> as JSON string
def to_json(*args)
as_json.to_json(*args)
end
end

View File

@ -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
# <tt>v</tt> serialized by <tt>to_json</tt>.
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 <tt>v</tt> as a
# JSON string.
def to_json(*args)
as_json.to_json(*args)
end
end

View File

@ -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
# <tt>a</tt> serialized by <tt>to_json</tt>.
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 <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

View File

@ -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
# <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
# 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 <tt>o</tt> and source <tt>s</tt>
# (Regexp or String) as JSON string
def to_json(*)
as_json.to_json
end
end

View File

@ -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
# <tt>v</tt> serialized by <tt>to_json</tt>.
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 <tt>v</tt> as a JSON string.
# Only named structs are supported.
def to_json(*args)
as_json.to_json(*args)
end
end

View File

@ -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 <tt>string</tt> value stored in the object to a Symbol
def self.json_create(o)
o['s'].to_sym
end
end

View File

@ -0,0 +1,35 @@
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 instance_methods.include?(: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(*)
{
JSON.create_id => self.class.name,
'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