jrjackson 0.2.7 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/changelog.md +15 -1
- data/dependency-reduced-pom.xml +4 -4
- data/lib/jrjackson/build_info.rb +1 -1
- data/lib/jrjackson/jars/jrjackson-1.2.11.jar +0 -0
- data/lib/jrjackson/jrjackson.rb +5 -5
- data/pom.xml +8 -8
- data/run_all_individual_bench.sh +15 -15
- data/src/main/java/com/jrjackson/JrJacksonRaw.java +41 -16
- data/src/main/java/com/jrjackson/RubyAnySerializer.java +6 -3
- data/src/main/java/com/jrjackson/RubyJacksonModule.java +0 -6
- data/src/main/java/com/jrjackson/RubyObjectDeserializer.java +11 -11
- data/src/main/java/com/jrjackson/RubyStringConverter.java +5 -1
- data/src/main/java/com/jrjackson/RubyUtils.java +6 -1
- data/test/jrjackson_test.rb +71 -4
- metadata +4 -3
- data/lib/jrjackson/jars/jrjackson-1.2.9.jar +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40a6bbd8555b9dca8cf37b6e7e2f41692ae2bd2b
|
4
|
+
data.tar.gz: af10130421ae850aea53f6b8396de32530605655
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bab3ca8847f2dbe92becd2fee1c201a0abd72378c922b0814920cba71ff8390f1fb9237ff1efa32c40ffbf8678d7fa3f255b9e85f45932fd57523de6c5982d2a
|
7
|
+
data.tar.gz: ae557540edc4d20a2e993da24f81d3ea35a092a2dd76b28815bede1ce9cac5a477aa9106277067749bc0a3a3487cc6a264b10d1fb10e09697a913868a6f33305
|
data/README.md
CHANGED
@@ -18,8 +18,10 @@ There is now a MultiJson adapter added for JrJackson
|
|
18
18
|
|
19
19
|
#### NEWS
|
20
20
|
|
21
|
+
11th May 2014 - Added to_time method call for Ruby object serialization
|
22
|
+
|
21
23
|
26th October 2013 - Added support to serialize arbitary (non JSON datatypes)
|
22
|
-
ruby objects. Normally the toJava internal
|
24
|
+
ruby objects. Normally the toJava internal method is called, but additionally
|
23
25
|
to_h, to_hash, to_a and finally to_json are tried. Be aware that the to_json
|
24
26
|
method might invoke a new selialization session and impact performance.
|
25
27
|
|
data/changelog.md
CHANGED
@@ -1,8 +1,22 @@
|
|
1
|
+
v0.2.8
|
2
|
+
fixes for issues-28,29,31
|
3
|
+
correction for Time#to_s
|
4
|
+
new options to control date serialization
|
5
|
+
optimizations suggested by @headius
|
6
|
+
jar compiled for jruby 1.7.17
|
7
|
+
jruby 1.7.17
|
8
|
+
jackson 2.4.4
|
9
|
+
|
10
|
+
v0.2.7
|
11
|
+
fixes for issues-23,24
|
12
|
+
add to_time as option for serializing Time like objects
|
13
|
+
jar compiled for jruby 1.7.11
|
14
|
+
|
1
15
|
v0.2.6
|
2
16
|
fix issue-20
|
3
17
|
allow jruby to convert Ruby StringIO into Java
|
4
18
|
by not type checking passed arg
|
5
|
-
this is because jruby 1.7.9 has changed the type of java object backing Ruby
|
19
|
+
this is because jruby 1.7.9 has changed the type of java object backing Ruby StringIO
|
6
20
|
jar compiled for jruby 1.7.8 (jruby 1.7.9 in the maven repo has an error in the pom.xml)
|
7
21
|
jruby 1.7.8, jruby 1.7.9 (tested)
|
8
22
|
jackson 2.3.0
|
data/dependency-reduced-pom.xml
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
<groupId>com.jrjackson.jruby</groupId>
|
5
5
|
<artifactId>jrjackson</artifactId>
|
6
6
|
<name>jrjackson</name>
|
7
|
-
<version>1.2.
|
7
|
+
<version>1.2.11</version>
|
8
8
|
<url>http://maven.apache.org</url>
|
9
9
|
<build>
|
10
10
|
<plugins>
|
@@ -22,14 +22,14 @@
|
|
22
22
|
</plugin>
|
23
23
|
<plugin>
|
24
24
|
<artifactId>maven-surefire-plugin</artifactId>
|
25
|
-
<version>2.
|
25
|
+
<version>2.17</version>
|
26
26
|
<configuration>
|
27
27
|
<skipTests>true</skipTests>
|
28
28
|
</configuration>
|
29
29
|
</plugin>
|
30
30
|
<plugin>
|
31
31
|
<artifactId>maven-shade-plugin</artifactId>
|
32
|
-
<version>2.
|
32
|
+
<version>2.3</version>
|
33
33
|
<executions>
|
34
34
|
<execution>
|
35
35
|
<phase>package</phase>
|
@@ -66,7 +66,7 @@
|
|
66
66
|
<dependency>
|
67
67
|
<groupId>org.jruby</groupId>
|
68
68
|
<artifactId>jruby</artifactId>
|
69
|
-
<version>1.7.
|
69
|
+
<version>1.7.17</version>
|
70
70
|
<scope>provided</scope>
|
71
71
|
<exclusions>
|
72
72
|
<exclusion>
|
data/lib/jrjackson/build_info.rb
CHANGED
Binary file
|
data/lib/jrjackson/jrjackson.rb
CHANGED
@@ -3,8 +3,8 @@ unless RUBY_PLATFORM =~ /java/
|
|
3
3
|
exit 255
|
4
4
|
end
|
5
5
|
|
6
|
-
require_relative "jars/jrjackson-1.2.
|
7
|
-
# require_relative "linked/jrjackson-1.2.
|
6
|
+
require_relative "jars/jrjackson-1.2.11.jar"
|
7
|
+
# require_relative "linked/jrjackson-1.2.11.jar"
|
8
8
|
|
9
9
|
require 'com/jrjackson/jr_jackson'
|
10
10
|
|
@@ -31,10 +31,10 @@ module JrJackson
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
def dump(object)
|
34
|
+
def dump(object, options = {})
|
35
35
|
case object
|
36
|
-
when
|
37
|
-
JrJackson::Raw.generate(object)
|
36
|
+
when Hash, Array, String, Java::JavaUtil::Map, Java::JavaUtil::List
|
37
|
+
JrJackson::Raw.generate(object, options)
|
38
38
|
when true, false
|
39
39
|
object.to_s
|
40
40
|
when nil
|
data/pom.xml
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
<groupId>com.jrjackson.jruby</groupId>
|
6
6
|
<artifactId>jrjackson</artifactId>
|
7
7
|
<packaging>jar</packaging>
|
8
|
-
<version>1.2.
|
8
|
+
<version>1.2.11</version>
|
9
9
|
<name>jrjackson</name>
|
10
10
|
<url>http://maven.apache.org</url>
|
11
11
|
|
@@ -23,28 +23,28 @@
|
|
23
23
|
<dependency>
|
24
24
|
<groupId>org.jruby</groupId>
|
25
25
|
<artifactId>jruby</artifactId>
|
26
|
-
<version>1.7.
|
26
|
+
<version>1.7.17</version>
|
27
27
|
<scope>provided</scope>
|
28
28
|
</dependency>
|
29
29
|
<dependency>
|
30
30
|
<groupId>com.fasterxml.jackson.core</groupId>
|
31
31
|
<artifactId>jackson-core</artifactId>
|
32
|
-
<version>2.
|
32
|
+
<version>2.4.4</version>
|
33
33
|
</dependency>
|
34
34
|
<dependency>
|
35
35
|
<groupId>com.fasterxml.jackson.core</groupId>
|
36
36
|
<artifactId>jackson-annotations</artifactId>
|
37
|
-
<version>2.
|
37
|
+
<version>2.4.4</version>
|
38
38
|
</dependency>
|
39
39
|
<dependency>
|
40
40
|
<groupId>com.fasterxml.jackson.core</groupId>
|
41
41
|
<artifactId>jackson-databind</artifactId>
|
42
|
-
<version>2.
|
42
|
+
<version>2.4.4</version>
|
43
43
|
</dependency>
|
44
44
|
<dependency>
|
45
45
|
<groupId>com.fasterxml.jackson.module</groupId>
|
46
46
|
<artifactId>jackson-module-afterburner</artifactId>
|
47
|
-
<version>2.
|
47
|
+
<version>2.4.4</version>
|
48
48
|
</dependency>
|
49
49
|
</dependencies>
|
50
50
|
<build>
|
@@ -65,7 +65,7 @@
|
|
65
65
|
<plugin>
|
66
66
|
<groupId>org.apache.maven.plugins</groupId>
|
67
67
|
<artifactId>maven-surefire-plugin</artifactId>
|
68
|
-
<version>2.
|
68
|
+
<version>2.17</version>
|
69
69
|
<configuration>
|
70
70
|
<skipTests>true</skipTests>
|
71
71
|
</configuration>
|
@@ -73,7 +73,7 @@
|
|
73
73
|
<plugin>
|
74
74
|
<groupId>org.apache.maven.plugins</groupId>
|
75
75
|
<artifactId>maven-shade-plugin</artifactId>
|
76
|
-
<version>2.
|
76
|
+
<version>2.3</version>
|
77
77
|
<executions>
|
78
78
|
<execution>
|
79
79
|
<phase>package</phase>
|
data/run_all_individual_bench.sh
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
#!/bin/sh
|
2
2
|
|
3
|
-
ruby benchmarking/individual/json-mri-sourced.rb
|
4
|
-
ruby benchmarking/individual/oj-mri-sourced.rb
|
3
|
+
chruby-exec ruby-2.2.0 -- ruby benchmarking/individual/json-mri-sourced.rb
|
4
|
+
chruby-exec ruby-2.2.0 -- ruby benchmarking/individual/oj-mri-sourced.rb
|
5
5
|
|
6
|
-
chruby-exec jruby --
|
7
|
-
chruby-exec jruby --
|
6
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/gson-jr-sourced.rb
|
7
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/json-jr-sourced.rb
|
8
8
|
|
9
|
-
chruby-exec jruby --
|
10
|
-
chruby-exec jruby --
|
9
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/string-jr-sourced.rb
|
10
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/str-bd-jr-sourced.rb
|
11
11
|
|
12
|
-
chruby-exec jruby --
|
13
|
-
chruby-exec jruby --
|
12
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/symbol-jr-sourced.rb
|
13
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/sym-bd-jr-sourced.rb
|
14
14
|
|
15
|
-
chruby-exec jruby --
|
16
|
-
chruby-exec jruby --
|
15
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/raw-jr-sourced.rb
|
16
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/raw-bd-jr-sourced.rb
|
17
17
|
|
18
|
-
ruby benchmarking/individual/json-gen-mri-sourced.rb
|
19
|
-
ruby benchmarking/individual/oj-gen-mri-sourced.rb
|
18
|
+
chruby-exec ruby-2.2.0 -- ruby benchmarking/individual/json-gen-mri-sourced.rb
|
19
|
+
chruby-exec ruby-2.2.0 -- ruby benchmarking/individual/oj-gen-mri-sourced.rb
|
20
20
|
|
21
|
-
chruby-exec jruby --
|
22
|
-
chruby-exec jruby --
|
23
|
-
chruby-exec jruby --
|
21
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/json-gen-jr-sourced.rb
|
22
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/gson-gen-jr-sourced.rb
|
23
|
+
chruby-exec jruby-1.7.17 -- ruby -J-Xmn512m -J-Xms2048m -J-Xmx2048m benchmarking/individual/raw-gen-jr-sourced.rb
|
@@ -6,20 +6,23 @@ import org.jruby.RubyObject;
|
|
6
6
|
import org.jruby.RubyString;
|
7
7
|
import org.jruby.RubySymbol;
|
8
8
|
import org.jruby.RubyHash;
|
9
|
+
import org.jruby.RubyIO;
|
9
10
|
import org.jruby.anno.JRubyMethod;
|
10
11
|
import org.jruby.anno.JRubyModule;
|
11
|
-
import org.jruby.java.addons.IOJavaAddons;
|
12
12
|
import org.jruby.runtime.ThreadContext;
|
13
13
|
import org.jruby.runtime.builtin.IRubyObject;
|
14
|
+
import org.jruby.ext.stringio.StringIO;
|
14
15
|
|
15
|
-
import java.io.InputStream;
|
16
16
|
import java.io.IOException;
|
17
|
-
|
17
|
+
|
18
|
+
import java.text.SimpleDateFormat;
|
19
|
+
import java.util.TimeZone;
|
18
20
|
|
19
21
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
20
22
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
21
23
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
22
24
|
|
25
|
+
|
23
26
|
@JRubyModule(name = "JrJacksonRaw")
|
24
27
|
public class JrJacksonRaw extends RubyObject {
|
25
28
|
|
@@ -41,7 +44,7 @@ public class JrJacksonRaw extends RubyObject {
|
|
41
44
|
throws IOException {
|
42
45
|
RubyHash options = null;
|
43
46
|
ObjectMapper local = null;
|
44
|
-
Ruby _ruby = context.
|
47
|
+
Ruby _ruby = context.runtime;
|
45
48
|
|
46
49
|
if (opts != context.nil) {
|
47
50
|
options = opts.convertToHash();
|
@@ -68,37 +71,44 @@ public class JrJacksonRaw extends RubyObject {
|
|
68
71
|
@JRubyMethod(module = true, name = {"parse_raw", "load_raw"}, required = 1)
|
69
72
|
public static IRubyObject parse_raw(ThreadContext context, IRubyObject self, IRubyObject arg)
|
70
73
|
throws IOException {
|
71
|
-
ObjectMapper mapper = RubyJacksonModule.mappedAs("raw", context.
|
74
|
+
ObjectMapper mapper = RubyJacksonModule.mappedAs("raw", context.runtime);
|
72
75
|
return _parse(context, arg, mapper);
|
73
76
|
}
|
74
77
|
|
75
78
|
@JRubyMethod(module = true, name = {"parse_sym", "load_sym"}, required = 1)
|
76
79
|
public static IRubyObject parse_sym(ThreadContext context, IRubyObject self, IRubyObject arg)
|
77
80
|
throws IOException {
|
78
|
-
ObjectMapper mapper = RubyJacksonModule.mappedAs("sym", context.
|
81
|
+
ObjectMapper mapper = RubyJacksonModule.mappedAs("sym", context.runtime);
|
79
82
|
return _parse(context, arg, mapper);
|
80
83
|
}
|
81
84
|
|
82
85
|
@JRubyMethod(module = true, name = {"parse_str", "load_str"}, required = 1)
|
83
86
|
public static IRubyObject parse_str(ThreadContext context, IRubyObject self, IRubyObject arg)
|
84
87
|
throws IOException {
|
85
|
-
ObjectMapper mapper = RubyJacksonModule.mappedAs("str", context.
|
88
|
+
ObjectMapper mapper = RubyJacksonModule.mappedAs("str", context.runtime);
|
86
89
|
return _parse(context, arg, mapper);
|
87
90
|
}
|
88
91
|
|
89
92
|
private static IRubyObject _parse(ThreadContext context, IRubyObject arg, ObjectMapper mapper)
|
90
93
|
throws IOException {
|
91
|
-
Ruby ruby = context.
|
94
|
+
Ruby ruby = context.runtime;
|
95
|
+
// same format as Ruby Time #to_s
|
96
|
+
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
|
97
|
+
mapper.setDateFormat(simpleFormat);
|
92
98
|
try {
|
93
99
|
Object o;
|
94
100
|
if (arg instanceof RubyString) {
|
95
101
|
o = mapper.readValue(
|
96
|
-
|
102
|
+
((RubyString) arg).getByteList().bytes(), Object.class
|
103
|
+
);
|
104
|
+
} else if (arg instanceof StringIO) {
|
105
|
+
RubyString content = (RubyString)((StringIO) arg).string(context);
|
106
|
+
o = mapper.readValue(
|
107
|
+
content.getByteList().bytes(), Object.class
|
97
108
|
);
|
98
109
|
} else {
|
99
110
|
// must be an IO object then
|
100
|
-
|
101
|
-
o = mapper.readValue((InputStream) stream.toJava(InputStream.class), Object.class);
|
111
|
+
o = mapper.readValue(((RubyIO)arg).getInStream(), Object.class);
|
102
112
|
}
|
103
113
|
return RubyUtils.rubyObject(ruby, o);
|
104
114
|
} catch (JsonProcessingException e) {
|
@@ -109,13 +119,28 @@ public class JrJacksonRaw extends RubyObject {
|
|
109
119
|
}
|
110
120
|
|
111
121
|
// serialize
|
112
|
-
@JRubyMethod(module = true, name = {"generate", "dump"}, required = 1)
|
113
|
-
public static IRubyObject generate(ThreadContext context, IRubyObject self, IRubyObject
|
122
|
+
@JRubyMethod(module = true, name = {"generate", "dump"}, required = 1, optional = 1)
|
123
|
+
public static IRubyObject generate(ThreadContext context, IRubyObject self, IRubyObject[] args)
|
114
124
|
throws IOException, JsonProcessingException {
|
115
|
-
Ruby _ruby = context.
|
116
|
-
Object obj =
|
125
|
+
Ruby _ruby = context.runtime;
|
126
|
+
Object obj = args[0].toJava(Object.class);
|
127
|
+
RubyHash options = (args.length <= 1) ? RubyHash.newHash(_ruby) : args[1].convertToHash();
|
128
|
+
String format = (String) options.get(RubyUtils.rubySymbol(_ruby, "date_format"));
|
129
|
+
|
130
|
+
ObjectMapper mapper = RubyJacksonModule.mappedAs("raw", _ruby);
|
131
|
+
// same format as Ruby Time #to_s
|
132
|
+
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
|
133
|
+
|
134
|
+
if (format != null) {
|
135
|
+
simpleFormat = new SimpleDateFormat(format);
|
136
|
+
String timezone = (String) options.get(RubyUtils.rubySymbol(_ruby, "timezone"));
|
137
|
+
if (timezone != null) {
|
138
|
+
simpleFormat.setTimeZone(TimeZone.getTimeZone(timezone));
|
139
|
+
}
|
140
|
+
}
|
141
|
+
mapper.setDateFormat(simpleFormat);
|
142
|
+
|
117
143
|
try {
|
118
|
-
ObjectMapper mapper = RubyJacksonModule.mappedAs("raw", _ruby);
|
119
144
|
String s = mapper.writeValueAsString(obj);
|
120
145
|
return RubyUtils.rubyString(_ruby, s);
|
121
146
|
} catch (JsonProcessingException e) {
|
@@ -38,9 +38,8 @@ public class RubyAnySerializer extends StdSerializer<RubyObject> {
|
|
38
38
|
return val;
|
39
39
|
}
|
40
40
|
|
41
|
-
private void serializeUnknownRubyObject(RubyObject rubyObject, JsonGenerator jgen, SerializerProvider provider)
|
41
|
+
private void serializeUnknownRubyObject(ThreadContext ctx, RubyObject rubyObject, JsonGenerator jgen, SerializerProvider provider)
|
42
42
|
throws IOException, JsonGenerationException {
|
43
|
-
ThreadContext ctx = rubyObject.getRuntime().getCurrentContext();
|
44
43
|
RubyClass meta = rubyObject.getMetaClass();
|
45
44
|
|
46
45
|
DynamicMethod method = meta.searchMethod("to_time");
|
@@ -87,16 +86,20 @@ public class RubyAnySerializer extends StdSerializer<RubyObject> {
|
|
87
86
|
@Override
|
88
87
|
public void serialize(RubyObject value, JsonGenerator jgen, SerializerProvider provider)
|
89
88
|
throws IOException, JsonGenerationException {
|
89
|
+
ThreadContext ctx = value.getRuntime().getCurrentContext();
|
90
90
|
if (value instanceof RubySymbol || value instanceof RubyString) {
|
91
91
|
jgen.writeString(value.toString());
|
92
92
|
} else if (value instanceof RubyHash) {
|
93
93
|
provider.findTypedValueSerializer(Map.class, true, null).serialize(value, jgen, provider);
|
94
94
|
} else if (value instanceof RubyArray) {
|
95
95
|
provider.findTypedValueSerializer(List.class, true, null).serialize(value, jgen, provider);
|
96
|
+
} else if (value instanceof RubyStruct) {
|
97
|
+
RubyObject obj = (RubyObject)value.callMethod(ctx, "to_a");
|
98
|
+
provider.findTypedValueSerializer(List.class, true, null).serialize(obj, jgen, provider);
|
96
99
|
} else {
|
97
100
|
Object val = value.toJava(rubyJavaClassLookup(value.getClass()));
|
98
101
|
if (val instanceof RubyObject) {
|
99
|
-
serializeUnknownRubyObject((RubyObject) val, jgen, provider);
|
102
|
+
serializeUnknownRubyObject(ctx, (RubyObject) val, jgen, provider);
|
100
103
|
} else {
|
101
104
|
provider.defaultSerializeValue(val, jgen);
|
102
105
|
}
|
@@ -19,7 +19,6 @@ public class RubyJacksonModule extends SimpleModule {
|
|
19
19
|
static {
|
20
20
|
static_mapper.registerModule(new AfterburnerModule());
|
21
21
|
static_mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
22
|
-
static_mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"));
|
23
22
|
}
|
24
23
|
|
25
24
|
private RubyJacksonModule() {
|
@@ -45,14 +44,9 @@ public class RubyJacksonModule extends SimpleModule {
|
|
45
44
|
);
|
46
45
|
}
|
47
46
|
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
48
|
-
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"));
|
49
47
|
return mapper;
|
50
48
|
}
|
51
49
|
|
52
|
-
// public static SimpleModule asRaw()
|
53
|
-
// {
|
54
|
-
// return static_mapper;
|
55
|
-
// }
|
56
50
|
public static SimpleModule asSym(Ruby ruby) {
|
57
51
|
return new RubyJacksonModule().addSerializer(
|
58
52
|
RubyObject.class, RubyAnySerializer.instance
|
@@ -1,7 +1,6 @@
|
|
1
1
|
package com.jrjackson;
|
2
2
|
|
3
3
|
import java.io.IOException;
|
4
|
-
import java.util.*;
|
5
4
|
|
6
5
|
import com.fasterxml.jackson.core.*;
|
7
6
|
|
@@ -23,7 +22,8 @@ public class RubyObjectDeserializer
|
|
23
22
|
|
24
23
|
private Ruby _ruby;
|
25
24
|
|
26
|
-
private RubyKeyConverter
|
25
|
+
private RubyKeyConverter key_converter;
|
26
|
+
final private RubyStringConverter str_converter = new RubyStringConverter();
|
27
27
|
|
28
28
|
public RubyObjectDeserializer() {
|
29
29
|
super(RubyObject.class);
|
@@ -35,12 +35,12 @@ public class RubyObjectDeserializer
|
|
35
35
|
}
|
36
36
|
|
37
37
|
public RubyObjectDeserializer setStringStrategy() {
|
38
|
-
|
38
|
+
key_converter = str_converter;
|
39
39
|
return this;
|
40
40
|
}
|
41
41
|
|
42
42
|
public RubyObjectDeserializer setSymbolStrategy() {
|
43
|
-
|
43
|
+
key_converter = new RubySymbolConverter();
|
44
44
|
return this;
|
45
45
|
}
|
46
46
|
|
@@ -65,20 +65,20 @@ public class RubyObjectDeserializer
|
|
65
65
|
return mapArray(jp, ctxt);
|
66
66
|
|
67
67
|
case FIELD_NAME:
|
68
|
-
return
|
68
|
+
return key_converter.convert(_ruby, jp);
|
69
69
|
|
70
70
|
case VALUE_EMBEDDED_OBJECT:
|
71
71
|
return RubyUtils.rubyObject(_ruby, jp.getEmbeddedObject());
|
72
72
|
|
73
73
|
case VALUE_STRING:
|
74
|
-
|
75
|
-
return RubyUtils.rubyString(_ruby, jp.getText());
|
74
|
+
return str_converter.convert(_ruby, jp);
|
76
75
|
|
77
76
|
case VALUE_NUMBER_INT:
|
78
77
|
/* [JACKSON-100]: caller may want to get all integral values
|
79
78
|
* returned as BigInteger, for consistency
|
80
79
|
*/
|
81
|
-
|
80
|
+
JsonParser.NumberType numberType = jp.getNumberType();
|
81
|
+
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS) || numberType == JsonParser.NumberType.BIG_INTEGER) {
|
82
82
|
return RubyUtils.rubyBignum(_ruby, jp.getBigIntegerValue());
|
83
83
|
}
|
84
84
|
return RubyUtils.rubyFixnum(_ruby, jp.getLongValue());
|
@@ -165,14 +165,14 @@ public class RubyObjectDeserializer
|
|
165
165
|
return RubyHash.newHash(_ruby);
|
166
166
|
}
|
167
167
|
|
168
|
-
RubyObject field1 =
|
168
|
+
RubyObject field1 = key_converter.convert(_ruby, jp);
|
169
169
|
jp.nextToken();
|
170
170
|
RubyObject value1 = deserialize(jp, ctxt);
|
171
171
|
if (jp.nextToken() != JsonToken.FIELD_NAME) { // single entry; but we want modifiable
|
172
172
|
return RuntimeHelpers.constructHash(_ruby, field1, value1);
|
173
173
|
}
|
174
174
|
|
175
|
-
RubyObject field2 =
|
175
|
+
RubyObject field2 = key_converter.convert(_ruby, jp);
|
176
176
|
jp.nextToken();
|
177
177
|
RubyObject value2 = deserialize(jp, ctxt);
|
178
178
|
if (jp.nextToken() != JsonToken.FIELD_NAME) {
|
@@ -182,7 +182,7 @@ public class RubyObjectDeserializer
|
|
182
182
|
// And then the general case; default map size is 16
|
183
183
|
RubyHash result = RuntimeHelpers.constructHash(_ruby, field1, value1, field2, value2);
|
184
184
|
do {
|
185
|
-
RubyObject fieldName =
|
185
|
+
RubyObject fieldName = key_converter.convert(_ruby, jp);
|
186
186
|
jp.nextToken();
|
187
187
|
result.fastASetCheckString(_ruby, fieldName, deserialize(jp, ctxt));
|
188
188
|
} while (jp.nextToken() != JsonToken.END_OBJECT);
|
@@ -3,6 +3,7 @@ package com.jrjackson;
|
|
3
3
|
import com.fasterxml.jackson.core.*;
|
4
4
|
|
5
5
|
import java.io.IOException;
|
6
|
+
import java.nio.CharBuffer;
|
6
7
|
import org.jruby.Ruby;
|
7
8
|
import org.jruby.RubyObject;
|
8
9
|
|
@@ -10,6 +11,9 @@ public class RubyStringConverter implements RubyKeyConverter {
|
|
10
11
|
|
11
12
|
@Override
|
12
13
|
public RubyObject convert(Ruby ruby, JsonParser jp) throws IOException {
|
13
|
-
return RubyUtils.rubyString(ruby, jp.getText()
|
14
|
+
// return RubyUtils.rubyString(ruby, jp.getText());
|
15
|
+
return RubyUtils.rubyString(ruby,
|
16
|
+
CharBuffer.wrap(jp.getTextCharacters(), 0, jp.getTextLength())
|
17
|
+
);
|
14
18
|
}
|
15
19
|
}
|
@@ -4,6 +4,7 @@ import java.util.*;
|
|
4
4
|
|
5
5
|
import java.math.BigDecimal;
|
6
6
|
import java.math.BigInteger;
|
7
|
+
import java.nio.CharBuffer;
|
7
8
|
|
8
9
|
import org.jruby.*;
|
9
10
|
import org.jruby.javasupport.JavaUtil;
|
@@ -24,10 +25,14 @@ public class RubyUtils {
|
|
24
25
|
return RubyString.newString(ruby, node);
|
25
26
|
}
|
26
27
|
|
28
|
+
public static RubyString rubyString(Ruby ruby, CharSequence node) {
|
29
|
+
return RubyString.newUnicodeString(ruby, node);
|
30
|
+
}
|
31
|
+
|
27
32
|
public static RubySymbol rubySymbol(Ruby ruby, String node) {
|
28
33
|
return RubySymbol.newSymbol(ruby, node);
|
29
34
|
}
|
30
|
-
|
35
|
+
|
31
36
|
public static RubyArray rubyArray(Ruby ruby, Object[] arg) {
|
32
37
|
return (RubyArray) JavaUtil.convertJavaToRuby(ruby, arg);
|
33
38
|
}
|
data/test/jrjackson_test.rb
CHANGED
@@ -9,6 +9,7 @@ require 'thread'
|
|
9
9
|
require 'bigdecimal'
|
10
10
|
require 'jrjackson'
|
11
11
|
require 'stringio'
|
12
|
+
require 'time'
|
12
13
|
|
13
14
|
class JrJacksonTest < Test::Unit::TestCase
|
14
15
|
|
@@ -92,17 +93,66 @@ class JrJacksonTest < Test::Unit::TestCase
|
|
92
93
|
json_string = JrJackson::Json.dump(source)
|
93
94
|
expected = {
|
94
95
|
:sym => "a_symbol",
|
95
|
-
:dt => dt.strftime("%F %T %
|
96
|
+
:dt => dt.strftime("%F %T %z"),
|
96
97
|
:co1 => {:one => "uno", :two => "two", :six => 6.0 },
|
97
98
|
:co2 => {:one => "uno", :two => "two", :six => 6.0 },
|
98
99
|
:co3 => {:one => 1.0, :two => 2.0, :six => 6.0 },
|
99
100
|
:co4 => [1, 2, 6],
|
100
|
-
:co5 => dt.strftime("%F %T %
|
101
|
+
:co5 => dt.strftime("%F %T %z")
|
101
102
|
}
|
102
103
|
actual = JrJackson::Json.load(json_string, :symbolize_keys => true)
|
103
104
|
assert_equal expected, actual
|
104
105
|
end
|
105
106
|
|
107
|
+
def test_raw_serialize_base_classes
|
108
|
+
# String
|
109
|
+
assert_equal JrJackson::Json.dump("foo"), "\"foo\""
|
110
|
+
|
111
|
+
# Hash and implementations of the Java Hash interface
|
112
|
+
assert_equal JrJackson::Json.dump({"foo" => 1}), "{\"foo\":1}"
|
113
|
+
assert_equal JrJackson::Json.dump(Java::JavaUtil::HashMap.new({"foo" => 1})), "{\"foo\":1}"
|
114
|
+
assert_equal JrJackson::Json.dump(Java::JavaUtil::LinkedHashMap.new({"foo" => 1})), "{\"foo\":1}"
|
115
|
+
|
116
|
+
# Array and implementations of the Java List interface
|
117
|
+
assert_equal JrJackson::Json.dump(["foo", 1]), "[\"foo\",1]"
|
118
|
+
assert_equal JrJackson::Json.dump(Java::JavaUtil::ArrayList.new(["foo", 1])), "[\"foo\",1]"
|
119
|
+
assert_equal JrJackson::Json.dump(Java::JavaUtil::LinkedList.new(["foo", 1])), "[\"foo\",1]"
|
120
|
+
assert_equal JrJackson::Json.dump(Java::JavaUtil::Vector.new(["foo", 1])), "[\"foo\",1]"
|
121
|
+
|
122
|
+
# true/false
|
123
|
+
assert_equal JrJackson::Json.dump(true), "true"
|
124
|
+
assert_equal JrJackson::Json.dump(false), "false"
|
125
|
+
|
126
|
+
# nil
|
127
|
+
assert_equal JrJackson::Json.dump(nil), "null"
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_serialize_date
|
131
|
+
# default date format
|
132
|
+
time_string = "2014-06-10 18:18:40 EDT"
|
133
|
+
source_time = Time.parse(time_string)
|
134
|
+
serialized_output = JrJackson::Json.dump({"time" => source_time})
|
135
|
+
other_time = Time.parse(serialized_output.split('"')[-2])
|
136
|
+
assert_equal other_time.to_f, source_time.to_f
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_serialize_date_date_format
|
140
|
+
|
141
|
+
time = Time.new(2014,6,10,18,18,40, "-04:00")
|
142
|
+
# using date_format option
|
143
|
+
assert_equal "{\"time\":\"2014-06-10\"}", JrJackson::Json.dump({"time" => time}, :date_format => "yyyy-MM-dd")
|
144
|
+
assert_match /\{"time"\:"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}[+-]\d{4}"\}/, JrJackson::Json.dump({"time" => time}, :date_format => "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_serialize_date_date_format_timezone
|
148
|
+
|
149
|
+
time = Time.new(2014,6,10,18,18,40, "-04:00")
|
150
|
+
# using date_format and timezone options
|
151
|
+
assert_equal "{\"time\":\"2014-06-10T22:18:40.000+0000\"}", JrJackson::Json.dump({"time" => time}, :date_format => "yyyy-MM-dd'T'HH:mm:ss.SSSZ", :timezone => "UTC")
|
152
|
+
# iso8601 date_format and timezone
|
153
|
+
assert_equal "{\"time\":\"2014-06-10T22:18:40.000Z\"}", JrJackson::Json.dump({"time" => time}, :date_format => "yyyy-MM-dd'T'HH:mm:ss.SSSX", :timezone => "UTC")
|
154
|
+
end
|
155
|
+
|
106
156
|
def test_can_parse_nulls
|
107
157
|
expected = {"foo" => nil}
|
108
158
|
json = '{"foo":null}'
|
@@ -111,8 +161,17 @@ class JrJacksonTest < Test::Unit::TestCase
|
|
111
161
|
end
|
112
162
|
|
113
163
|
def test_stringio
|
114
|
-
expected = {"foo" => 5}
|
115
|
-
json = ::StringIO.new('{"foo":5}')
|
164
|
+
expected = {"foo" => 5, "utf8" => "żółć"}
|
165
|
+
json = ::StringIO.new('{"foo":5, "utf8":"żółć"}')
|
166
|
+
actual = JrJackson::Json.load(json)
|
167
|
+
assert_equal expected, actual
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_ruby_io
|
171
|
+
expected = {"foo" => 5, "bar" => 6, "utf8" => "żółć"}
|
172
|
+
json, w = IO.pipe
|
173
|
+
w.write('{"foo":5, "bar":6, "utf8":"żółć"}')
|
174
|
+
w.close
|
116
175
|
actual = JrJackson::Json.load(json)
|
117
176
|
assert_equal expected, actual
|
118
177
|
end
|
@@ -123,6 +182,14 @@ class JrJacksonTest < Test::Unit::TestCase
|
|
123
182
|
end
|
124
183
|
end
|
125
184
|
|
185
|
+
def test_can_parse_bignum
|
186
|
+
expected = 12345678901234567890123456789
|
187
|
+
json = '{"foo":12345678901234567890123456789}'
|
188
|
+
|
189
|
+
actual = JrJackson::Json.parse(json)['foo']
|
190
|
+
assert_equal expected, actual
|
191
|
+
end
|
192
|
+
|
126
193
|
def test_can_parse_big_decimals
|
127
194
|
expected = BigDecimal.new '0.12345678901234567890123456789'
|
128
195
|
json = '{"foo":0.12345678901234567890123456789}'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jrjackson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guy Boertje
|
@@ -41,7 +41,7 @@ files:
|
|
41
41
|
- jrjackson.gemspec
|
42
42
|
- lib/jrjackson.rb
|
43
43
|
- lib/jrjackson/build_info.rb
|
44
|
-
- lib/jrjackson/jars/jrjackson-1.2.
|
44
|
+
- lib/jrjackson/jars/jrjackson-1.2.11.jar
|
45
45
|
- lib/jrjackson/jrjackson.rb
|
46
46
|
- lib/require_relative_patch.rb
|
47
47
|
- pom.xml
|
@@ -79,8 +79,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
79
|
version: '0'
|
80
80
|
requirements: []
|
81
81
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.2.
|
82
|
+
rubygems_version: 2.2.2
|
83
83
|
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: A JRuby wrapper for the java jackson json processor jar
|
86
86
|
test_files: []
|
87
|
+
has_rdoc:
|
Binary file
|