Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

making timespec compatible

  • Loading branch information...
commit 100b871d87d2ed9bc5b293e9b62a24fb55f61022 1 parent 62872e1
Peter Ohler authored
View
2  README.md
@@ -28,6 +28,8 @@ A fast JSON parser and Object marshaller as a Ruby gem.
- Removed the encoding option and fixed a misunderstanding of the string encoding. Unicode code points are now used instead of byte codes. This is not compatible with previous releases but is compliant with RFC4627.
+ - Time encoding in :object mode is faster and higher nanosecond precision.
+
## <a name="description">Description</a>
Optimized JSON (Oj), as the name implies was written to provide speed
View
11 ext/oj/dump.c
@@ -39,6 +39,9 @@
#include "oj.h"
#include "cache8.h"
+#ifndef HAVE_RUBY_ENCODING_H
+#define rb_eEncodingError rb_eException
+#endif
typedef unsigned long ulong;
typedef struct _Out {
@@ -903,11 +906,17 @@ static void
dump_time(VALUE obj, Out out) {
char buf[64];
char *b = buf + sizeof(buf) - 1;
- char *dot = b - 10;
long size;
+ char *dot = b - 10;
+#ifdef HAVE_STRUCT_TIMESPEC
struct timespec ts = rb_time_timespec(obj);
time_t sec = ts.tv_sec;
long nsec = ts.tv_nsec;
+#else
+ struct timeval tv = rb_time_timeval(obj);
+ time_t sec = tv.tv_sec;
+ long nsec = tv.tv_usec * 1000;
+#endif
*b-- = '\0';
for (; dot < b; b--, nsec /= 10) {
View
2  ext/oj/fast.c
@@ -1151,7 +1151,7 @@ doc_open_file(VALUE clas, VALUE filename) {
fseek(f, 0, SEEK_SET);
if (len != fread(json, 1, len, f)) {
fclose(f);
- rb_raise(rb_eLoadError, "Failed to read %ld bytes from %s.\n", len, path);
+ rb_raise(rb_eLoadError, "Failed to read %lu bytes from %s.\n", len, path);
}
fclose(f);
json[len] = '\0';
View
2  ext/oj/oj.h
@@ -61,8 +61,10 @@ extern "C" {
#undef T_RATIONAL
#undef T_COMPLEX
#define NO_RSTRUCT 1
+#ifndef IVAR_HELPERS
#define IVAR_HELPERS 0
#endif
+#endif
#if IVAR_HELPERS
#include "ruby/st.h"
View
14 notes
@@ -4,20 +4,6 @@
^c^s show subtree
- next
- - unicode
- - remove option for encoding, always make UTF-8
- - later allow utf-16
- - keep ascii-only but make default - this is rcf compliant
- - maybe change the name to 'unicode' or reverse it to allow-high-bits
- -
- - change encoding to be unicode
- - use utf-16 surrogate pairs for extended
- - size logic
- - if character has hi bit set
- - size is number of bits set
- - skip that many characters unless one of them is 0 or exceed length
- -
-
- setup travis to run tests
- make sure time handles nsec instead of usec
View
23 test/tests.rb
@@ -67,8 +67,7 @@ class Juice < ::Test::Unit::TestCase
def test0_get_options
opts = Oj.default_options()
- assert_equal({ :encoding=>Encoding.find('UTF-8'),
- :indent=>0,
+ assert_equal({ :indent=>0,
:circular=>false,
:auto_define=>true,
:symbol_keys=>false,
@@ -78,7 +77,6 @@ def test0_get_options
def test0_set_options
orig = {
- :encoding=>nil,
:indent=>0,
:circular=>false,
:auto_define=>true,
@@ -86,7 +84,6 @@ def test0_set_options
:ascii_only=>false,
:mode=>:object}
o2 = {
- :encoding=>"UTF-8",
:indent=>4,
:circular=>true,
:auto_define=>false,
@@ -96,7 +93,6 @@ def test0_set_options
o3 = { :indent => 4 }
Oj.default_options = o2
opts = Oj.default_options()
- o2[:encoding] = Encoding.find('UTF-8')
assert_equal(opts, o2);
Oj.default_options = o3 # see if it throws an exception
Oj.default_options = orig # return to original
@@ -147,11 +143,22 @@ def test_string_object
end
def test_encode
- Oj.default_options = { :encoding => 'UTF-8' }
+ opts = Oj.default_options
+ Oj.default_options = { :ascii_only => false }
dump_and_load("ぴーたー", false)
Oj.default_options = { :ascii_only => true }
+ json = Oj.dump("ぴーたー")
+ assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
dump_and_load("ぴーたー", false)
- Oj.default_options = { :encoding => nil, :ascii_only => false }
+ Oj.default_options = opts
+ end
+
+ def test_unicode
+ # hits the 3 normal ranges and one extended surrogate pair
+ json = %{"\\u019f\\u05e9\\u3074\\ud834\\udd1e"}
+ obj = Oj.load(json)
+ json2 = Oj.dump(obj, :ascii_only => true)
+ assert_equal(json, json2)
end
def test_array
@@ -202,7 +209,7 @@ def test_time_null
def test_time_compat
t = Time.local(2012, 1, 5, 23, 58, 7)
json = Oj.dump(t, :mode => :compat)
- assert_equal(%{1325775487.000000}, json)
+ assert_equal(%{1325775487.000000000}, json)
end
def test_time_object
t = Time.now()
Please sign in to comment.
Something went wrong with that request. Please try again.