fjson 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +3 -0
- data/README +5 -0
- data/Rakefile +3 -2
- data/ext/extensions/false_class_ext/false_class_ext.h +1 -1
- data/ext/extensions/float_ext/float_ext.h +1 -1
- data/ext/extensions/integer_ext/integer_ext.h +1 -1
- data/ext/extensions/nil_class_ext/nil_class_ext.h +1 -1
- data/ext/json_ext/json_ext.c +40 -30
- data/ext/json_ext/json_ext.h +1 -1
- data/lib/parser.rb +2 -1
- data/spec/string_with_latin1_character_set_json_spec.rb +13 -0
- data/spec/string_with_utf8_values_when_supporting_unicode_json_spec.rb +47 -30
- metadata +2 -2
data/CHANGES
CHANGED
data/README
CHANGED
data/Rakefile
CHANGED
@@ -54,7 +54,8 @@ end
|
|
54
54
|
task :install do
|
55
55
|
dir = File.dirname(__FILE__)
|
56
56
|
fjson_extensions.each do |extension_path|
|
57
|
-
|
57
|
+
binary_path = extension_binary_path(extension_path)
|
58
|
+
cp binary_path, "#{dir}/lib/#{extension_path}.so"
|
58
59
|
end
|
59
60
|
end
|
60
61
|
|
@@ -77,7 +78,7 @@ def extension_directory_path(relative_extension_path)
|
|
77
78
|
end
|
78
79
|
|
79
80
|
PKG_NAME = "fjson"
|
80
|
-
PKG_VERSION = "0.0.
|
81
|
+
PKG_VERSION = "0.0.3"
|
81
82
|
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
82
83
|
PKG_FILES = FileList[
|
83
84
|
'[A-Z]*',
|
data/ext/json_ext/json_ext.c
CHANGED
@@ -61,8 +61,17 @@ static VALUE utf8_to_json(self, input)
|
|
61
61
|
|
62
62
|
int c_increment_length;
|
63
63
|
size_t i = 0;
|
64
|
+
|
65
|
+
VALUE supports_utf8 = Qfalse;
|
66
|
+
if(
|
67
|
+
rb_funcall(mJSON, rb_intern("support_unicode?"), 0) == Qtrue &&
|
68
|
+
rb_equal(rb_gv_get("KCODE"), rb_str_new2("UTF8"))
|
69
|
+
) {
|
70
|
+
supports_utf8 = Qtrue;
|
71
|
+
}
|
72
|
+
|
64
73
|
while(i < c_length) {
|
65
|
-
c_increment_length = _convert_utf8_char(input, i, result);
|
74
|
+
c_increment_length = _convert_utf8_char(input, i, result, supports_utf8);
|
66
75
|
i += c_increment_length;
|
67
76
|
}
|
68
77
|
|
@@ -92,10 +101,11 @@ static void _begin_setup_iconv() {
|
|
92
101
|
rb_funcall(mUTF16toUTF8, rb_intern("iconv"), 1, rb_str_new2("no bom"));
|
93
102
|
}
|
94
103
|
|
95
|
-
static long _convert_utf8_char(input, i, result)
|
104
|
+
static long _convert_utf8_char(input, i, result, supports_utf8)
|
96
105
|
VALUE input;
|
97
106
|
long i;
|
98
107
|
VALUE result;
|
108
|
+
VALUE supports_utf8;
|
99
109
|
{
|
100
110
|
long c_increment_length = 1;
|
101
111
|
if (i < 0 || RSTRING(input)->len <= i) {
|
@@ -124,6 +134,9 @@ static long _convert_utf8_char(input, i, result)
|
|
124
134
|
else if(c_current == '\\') {
|
125
135
|
rb_str_cat2(result, "\\\\");
|
126
136
|
}
|
137
|
+
else if(c_current == '\/') {
|
138
|
+
rb_str_cat2(result, "\\\/");
|
139
|
+
}
|
127
140
|
else if(0x00 <= c_current && c_current <= 0x1f) {
|
128
141
|
VALUE format_string = rb_str_new2("\\u%04x");
|
129
142
|
rb_str_append(result, rb_funcall(format_string, rb_intern("%"), 1, LONG2NUM(c_current)));
|
@@ -131,38 +144,35 @@ static long _convert_utf8_char(input, i, result)
|
|
131
144
|
else if(0x20 <= c_current && c_current <= 0x7f) {
|
132
145
|
rb_str_concat(result, LONG2NUM(c_current));
|
133
146
|
}
|
134
|
-
else if(
|
135
|
-
!(
|
136
|
-
rb_funcall(mJSON, rb_intern("support_unicode?"), 0) == Qtrue &&
|
137
|
-
rb_equal(rb_gv_get("KCODE"), rb_str_new2("UTF8"))
|
138
|
-
)) {
|
147
|
+
else if(supports_utf8 == Qfalse) {
|
139
148
|
VALUE current = rb_funcall(input, rb_intern("[]"), 1, LONG2NUM(i));
|
140
149
|
rb_str_concat(result, current);
|
141
150
|
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
// }
|
151
|
+
else if((c_current & 0xe0) == 0xc0) {
|
152
|
+
c_increment_length = 2;
|
153
|
+
_convert_utf_8_string_part(result, input, i, c_increment_length);
|
154
|
+
}
|
155
|
+
else if((c_current & 0xf0) == 0xe0) {
|
156
|
+
c_increment_length = 3;
|
157
|
+
_convert_utf_8_string_part(result, input, i, c_increment_length);
|
158
|
+
}
|
159
|
+
else if((c_current & 0xf8) == 0xf0) {
|
160
|
+
c_increment_length = 4;
|
161
|
+
_convert_utf_8_string_part(result, input, i, c_increment_length);
|
162
|
+
}
|
163
|
+
else if((c_current & 0xfc) == 0xf8) {
|
164
|
+
c_increment_length = 5;
|
165
|
+
_convert_utf_8_string_part(result, input, i, c_increment_length);
|
166
|
+
}
|
167
|
+
else if((c_current & 0xfe) == 0xfc) {
|
168
|
+
c_increment_length = 6;
|
169
|
+
_convert_utf_8_string_part(result, input, i, c_increment_length);
|
170
|
+
}
|
163
171
|
else {
|
164
|
-
|
165
|
-
|
172
|
+
char* error_message = "Encountered unknown UTF-8 byte: ";
|
173
|
+
error_message += c_current;
|
174
|
+
strcat(error_message, "!");
|
175
|
+
rb_raise(rb_eRuntimeError, error_message);
|
166
176
|
}
|
167
177
|
|
168
178
|
return c_increment_length;
|
data/ext/json_ext/json_ext.h
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
static VALUE utf8_to_utf16(VALUE, VALUE);
|
5
5
|
static VALUE _convert_utf_8_string_part(VALUE, VALUE, long, long);
|
6
|
-
static long _convert_utf8_char(VALUE, long, VALUE);
|
6
|
+
static long _convert_utf8_char(VALUE, long, VALUE, VALUE);
|
7
7
|
static VALUE utf8_to_json(VALUE, VALUE);
|
8
8
|
static void _begin_setup_iconv();
|
9
9
|
void Init_json_ext();
|
data/lib/parser.rb
CHANGED
@@ -56,8 +56,9 @@ module JSON
|
|
56
56
|
def parse_string
|
57
57
|
if scan(STRING)
|
58
58
|
return '' if self[1].empty?
|
59
|
-
self[1].gsub(
|
59
|
+
self[1].gsub(%r(\\(?:[\\bfnrt"/]|u([A-Fa-f\d]{4})))) do
|
60
60
|
case $~[0]
|
61
|
+
when '\/' then '/'
|
61
62
|
when '\\\\' then '\\'
|
62
63
|
when '\\b' then "\b"
|
63
64
|
when '\\f' then "\f"
|
@@ -51,4 +51,17 @@ context "String with latin1 character set Json" do
|
|
51
51
|
JSON.utf8_to_json("test").should_equal "test"
|
52
52
|
JSON.utf8_to_json('1').should_equal '1'
|
53
53
|
end
|
54
|
+
|
55
|
+
specify "should support backslashes" do
|
56
|
+
json = '"\\\\.(?i:gif|jpe?g|png)$"'
|
57
|
+
data = JSON.parse(json)
|
58
|
+
JSON.unparse(data).should_equal json
|
59
|
+
json = '"\\""'
|
60
|
+
data = JSON.parse(json)
|
61
|
+
JSON.unparse(data).should_equal json
|
62
|
+
json = '"\/"'
|
63
|
+
data = JSON.parse(json)
|
64
|
+
data.should_equal '/'
|
65
|
+
JSON.unparse(data).should_equal json
|
66
|
+
end
|
54
67
|
end
|
@@ -4,33 +4,50 @@ dir = File.dirname(__FILE__)
|
|
4
4
|
require File.expand_path("#{dir}/spec_helper")
|
5
5
|
require File.expand_path("#{dir}/../lib/fjson")
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
7
|
+
context "String with UTF8 values when supporting unicode Json" do
|
8
|
+
setup do
|
9
|
+
JSON.support_unicode = true
|
10
|
+
$KCODE = "UTF8"
|
11
|
+
end
|
12
|
+
|
13
|
+
specify "should format values between 0x00000080 and 0x000007ff" do
|
14
|
+
val = [0x0080].pack("U")
|
15
|
+
JSON.utf8_to_json(val).should_equal "\\u0080"
|
16
|
+
|
17
|
+
val = [0x07ff].pack("U")
|
18
|
+
JSON.utf8_to_json(val).should_equal "\\u07ff"
|
19
|
+
end
|
20
|
+
|
21
|
+
specify "should format values between 0x00001000 and 0x0000ffff" do
|
22
|
+
val = [0x1000].pack("U")
|
23
|
+
JSON.utf8_to_json(val).should_equal "\\u1000"
|
24
|
+
|
25
|
+
val = [0xffff].pack("U")
|
26
|
+
JSON.utf8_to_json(val).should_equal "\\uffff"
|
27
|
+
end
|
28
|
+
|
29
|
+
specify "should format values between 0x00010000 and 0x0010ffff" do
|
30
|
+
val = [0x010000].pack("U")
|
31
|
+
JSON.utf8_to_json(val).should_equal "\\ud800dc00"
|
32
|
+
|
33
|
+
val = [0x10ffff].pack("U")
|
34
|
+
JSON.utf8_to_json(val).should_equal "\\udbffdfff"
|
35
|
+
end
|
36
|
+
|
37
|
+
specify "should parse unicode values" do
|
38
|
+
utf8 = '© ≠ €!'
|
39
|
+
json = '"\u00a9 \u2260 \u20ac!"'
|
40
|
+
utf8.to_json.should_equal json
|
41
|
+
JSON.parse(json).should_equal utf8
|
42
|
+
|
43
|
+
utf8 = "\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"
|
44
|
+
json = '"\u3042\u3044\u3046\u3048\u304a"'
|
45
|
+
utf8.to_json.should_equal json
|
46
|
+
JSON.parse(json).should_equal utf8
|
47
|
+
|
48
|
+
utf8 = 'საქართველო'
|
49
|
+
json = '"\u10e1\u10d0\u10e5\u10d0\u10e0\u10d7\u10d5\u10d4\u10da\u10dd"'
|
50
|
+
utf8.to_json.should_equal json
|
51
|
+
JSON.parse(json).should_equal utf8
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: fjson
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2006-10-
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2006-10-17 00:00:00 -07:00
|
8
8
|
summary: This library is for parsing JSON strings and unparsing ruby data structures. This library is a fork of Florian Frank's JSON library with key parts implemented in C for performance improvements.
|
9
9
|
require_paths:
|
10
10
|
- lib
|