jrjackson 0.3.9-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.__jrubyrc +433 -0
  3. data/.gitignore +10 -0
  4. data/.mvn/extensions.xml +8 -0
  5. data/Gemfile +10 -0
  6. data/Mavenfile +32 -0
  7. data/README.md +150 -0
  8. data/Rakefile +10 -0
  9. data/alt_bench.rb +46 -0
  10. data/changelog.md +131 -0
  11. data/jrjackson.gemspec +32 -0
  12. data/lib/com/fasterxml/jackson/core/jackson-annotations/2.7.1/jackson-annotations-2.7.1.jar +0 -0
  13. data/lib/com/fasterxml/jackson/core/jackson-core/2.7.1/jackson-core-2.7.1.jar +0 -0
  14. data/lib/com/fasterxml/jackson/core/jackson-databind/2.7.1/jackson-databind-2.7.1.jar +0 -0
  15. data/lib/com/fasterxml/jackson/module/jackson-module-afterburner/2.6.3/jackson-module-afterburner-2.6.3.jar +0 -0
  16. data/lib/com/fasterxml/jackson/module/jackson-module-afterburner/2.7.1/jackson-module-afterburner-2.7.1.jar +0 -0
  17. data/lib/jrjackson.rb +2 -0
  18. data/lib/jrjackson/build_info.rb +15 -0
  19. data/lib/jrjackson/jars/jrjackson-1.2.18.jar +0 -0
  20. data/lib/jrjackson/jars/jrjackson-1.2.7.jar +0 -0
  21. data/lib/jrjackson/jrjackson.rb +94 -0
  22. data/lib/jrjackson_jars.rb +7 -0
  23. data/lib/require_relative_patch.rb +6 -0
  24. data/pom.xml +193 -0
  25. data/profiling/profiled.rb +15 -0
  26. data/run_all_individual_bench.sh +25 -0
  27. data/run_jruby_individual_bench.sh +20 -0
  28. data/run_mri_individual_bench.sh +7 -0
  29. data/src/main/java/com/jrjackson/IParseHandler.java +53 -0
  30. data/src/main/java/com/jrjackson/JavaBigDecimalValueConverter.java +17 -0
  31. data/src/main/java/com/jrjackson/JavaBigIntValueConverter.java +17 -0
  32. data/src/main/java/com/jrjackson/JavaConverter.java +10 -0
  33. data/src/main/java/com/jrjackson/JavaFloatValueConverter.java +16 -0
  34. data/src/main/java/com/jrjackson/JavaHandler.java +118 -0
  35. data/src/main/java/com/jrjackson/JavaLongValueConverter.java +16 -0
  36. data/src/main/java/com/jrjackson/JjParse.java +147 -0
  37. data/src/main/java/com/jrjackson/JrJacksonBase.java +152 -0
  38. data/src/main/java/com/jrjackson/JrJacksonJava.java +81 -0
  39. data/src/main/java/com/jrjackson/JrJacksonRaw.java +108 -0
  40. data/src/main/java/com/jrjackson/JrJacksonRuby.java +89 -0
  41. data/src/main/java/com/jrjackson/JrJacksonSaj.java +26 -0
  42. data/src/main/java/com/jrjackson/JrJacksonSch.java +25 -0
  43. data/src/main/java/com/jrjackson/JrJacksonService.java +38 -0
  44. data/src/main/java/com/jrjackson/JrParse.java +149 -0
  45. data/src/main/java/com/jrjackson/ParseError.java +16 -0
  46. data/src/main/java/com/jrjackson/RubyAnySerializer.java +254 -0
  47. data/src/main/java/com/jrjackson/RubyBigDecimalValueConverter.java +18 -0
  48. data/src/main/java/com/jrjackson/RubyBigIntValueConverter.java +21 -0
  49. data/src/main/java/com/jrjackson/RubyConverter.java +12 -0
  50. data/src/main/java/com/jrjackson/RubyDateFormat.java +34 -0
  51. data/src/main/java/com/jrjackson/RubyFloatValueConverter.java +18 -0
  52. data/src/main/java/com/jrjackson/RubyHandler.java +119 -0
  53. data/src/main/java/com/jrjackson/RubyIntValueConverter.java +18 -0
  54. data/src/main/java/com/jrjackson/RubyJacksonModule.java +72 -0
  55. data/src/main/java/com/jrjackson/RubyKeyConverter.java +12 -0
  56. data/src/main/java/com/jrjackson/RubyNameConverter.java +9 -0
  57. data/src/main/java/com/jrjackson/RubyObjectDeserializer.java +182 -0
  58. data/src/main/java/com/jrjackson/RubyStringConverter.java +18 -0
  59. data/src/main/java/com/jrjackson/RubyStringKeyConverter.java +15 -0
  60. data/src/main/java/com/jrjackson/RubyStringNameConverter.java +12 -0
  61. data/src/main/java/com/jrjackson/RubySymbolKeyConverter.java +15 -0
  62. data/src/main/java/com/jrjackson/RubySymbolNameConverter.java +12 -0
  63. data/src/main/java/com/jrjackson/RubyUtils.java +150 -0
  64. data/src/main/java/com/jrjackson/SajParse.java +169 -0
  65. data/src/main/java/com/jrjackson/SchParse.java +209 -0
  66. data/src/main/java/com/jrjackson/StreamParse.java +66 -0
  67. data/test/jrjackson_test.rb +533 -0
  68. metadata +162 -0
@@ -0,0 +1,16 @@
1
+ package com.jrjackson;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.RubyClass;
5
+ import org.jruby.RubyException;
6
+ import org.jruby.anno.JRubyClass;
7
+ import org.jruby.exceptions.RaiseException;
8
+
9
+ @JRubyClass(name = "JrJackson::ParseError", parent = "RuntimeError")
10
+ public class ParseError {
11
+
12
+ public static RaiseException newParseError(Ruby ruby, String message) {
13
+ RubyClass errorClass = ruby.getModule("JrJackson").getClass("ParseError");
14
+ return new RaiseException(RubyException.newException(ruby, errorClass, message), true);
15
+ }
16
+ }
@@ -0,0 +1,254 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonGenerationException;
4
+ import com.fasterxml.jackson.core.JsonGenerator;
5
+ import com.fasterxml.jackson.databind.JsonSerializer;
6
+ import com.fasterxml.jackson.databind.SerializerProvider;
7
+ import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
8
+ import org.jruby.ext.bigdecimal.RubyBigDecimal;
9
+
10
+ import org.jruby.internal.runtime.methods.DynamicMethod;
11
+ import org.jruby.java.proxies.JavaProxy;
12
+ import org.jruby.runtime.ThreadContext;
13
+ import org.jruby.runtime.builtin.IRubyObject;
14
+
15
+ import java.io.IOException;
16
+ import java.text.DateFormat;
17
+ import java.text.SimpleDateFormat;
18
+ import org.jruby.RubyArray;
19
+ import org.jruby.RubyBignum;
20
+ import org.jruby.RubyClass;
21
+ import org.jruby.RubyHash;
22
+ import org.jruby.RubyNumeric;
23
+ import org.jruby.RubyObject;
24
+ import org.jruby.RubyString;
25
+ import org.jruby.RubyTime;
26
+
27
+
28
+ public class RubyAnySerializer extends JsonSerializer<IRubyObject> {
29
+
30
+ enum RUBYCLASS {
31
+
32
+ String,
33
+ Float,
34
+ BigDecimal,
35
+ Time,
36
+ Array,
37
+ Hash,
38
+ Fixnum,
39
+ Bignum,
40
+ Date,
41
+ Symbol,
42
+ Struct,
43
+ TrueClass,
44
+ FalseClass;
45
+ }
46
+
47
+ /**
48
+ * Singleton instance to use.""
49
+ */
50
+ public static final RubyAnySerializer instance = new RubyAnySerializer();
51
+
52
+ public RubyAnySerializer() {
53
+ // super(IRubyObject.class);
54
+
55
+ }
56
+
57
+ private void serializeUnknownRubyObject(ThreadContext ctx, IRubyObject rubyObject, JsonGenerator jgen, SerializerProvider provider)
58
+ throws IOException, JsonGenerationException {
59
+ RubyClass meta = rubyObject.getMetaClass();
60
+
61
+ DynamicMethod method = meta.searchMethod("to_json_data");
62
+ if (!method.isUndefined()) {
63
+ RubyObject obj = (RubyObject) method.call(ctx, rubyObject, meta, "to_json_data");
64
+ if (obj instanceof RubyString) {
65
+ RubyUtils.writeBytes(obj, jgen);
66
+ } else {
67
+ serialize(obj, jgen, provider);
68
+ }
69
+ return;
70
+ }
71
+
72
+ method = meta.searchMethod("to_time");
73
+ if (!method.isUndefined()) {
74
+ RubyTime dt = (RubyTime) method.call(ctx, rubyObject, meta, "to_time");
75
+ serializeTime(dt, jgen, provider);
76
+ return;
77
+ }
78
+
79
+ method = meta.searchMethod("to_h");
80
+ if (!method.isUndefined()) {
81
+ RubyObject obj = (RubyObject) method.call(ctx, rubyObject, meta, "to_h");
82
+ serializeHash(obj, jgen, provider);
83
+ return;
84
+ }
85
+
86
+ method = meta.searchMethod("to_hash");
87
+ if (!method.isUndefined()) {
88
+ RubyObject obj = (RubyObject) method.call(ctx, rubyObject, meta, "to_hash");
89
+ serializeHash(obj, jgen, provider);
90
+ return;
91
+ }
92
+
93
+ method = meta.searchMethod("to_a");
94
+ if (!method.isUndefined()) {
95
+ RubyObject obj = (RubyObject) method.call(ctx, rubyObject, meta, "to_a");
96
+ serializeArray(obj, jgen, provider);
97
+ return;
98
+ }
99
+
100
+ method = meta.searchMethod("to_json");
101
+ if (!method.isUndefined()) {
102
+ RubyObject obj = (RubyObject) method.call(ctx, rubyObject, meta, "to_json");
103
+ if (obj instanceof RubyString) {
104
+ jgen.writeRawValue(obj.toString());
105
+ } else {
106
+ serialize(obj, jgen, provider);
107
+ }
108
+ return;
109
+ }
110
+ throw new JsonGenerationException("Cannot serialize instance of: " + meta.getRealClass().getName(), jgen);
111
+ }
112
+
113
+ @Override
114
+ public void serialize(IRubyObject value, JsonGenerator jgen, SerializerProvider provider)
115
+ throws IOException, JsonGenerationException {
116
+
117
+ if (value.isNil()) {
118
+
119
+ jgen.writeNull(); // for RubyNil and NullObjects
120
+
121
+ } else if (value instanceof JavaProxy) {
122
+
123
+ provider.defaultSerializeValue(((JavaProxy) value).getObject(), jgen);
124
+
125
+ } else {
126
+
127
+ String rubyClassName = value.getMetaClass().getRealClass().getName();
128
+ RUBYCLASS clazz;
129
+
130
+ try {
131
+ clazz = RUBYCLASS.valueOf(rubyClassName);
132
+ } catch (IllegalArgumentException e) {
133
+ serializeUnknownRubyObject(value.getRuntime().getCurrentContext(), value, jgen, provider);
134
+ return;
135
+ }
136
+
137
+ switch (clazz) {
138
+ case Hash:
139
+ serializeHash(value, jgen, provider);
140
+ break;
141
+ case Array:
142
+ serializeArray(value, jgen, provider);
143
+ break;
144
+ case String:
145
+ RubyUtils.writeBytes(value, jgen);
146
+ break;
147
+ case Symbol:
148
+ case Date:
149
+ // Date to_s -> yyyy-mm-dd
150
+ RubyString s = value.asString();
151
+ jgen.writeUTF8String(s.getBytes(), 0, s.size());
152
+ break;
153
+ case TrueClass:
154
+ case FalseClass:
155
+ jgen.writeBoolean(value.isTrue());
156
+ break;
157
+ case Float:
158
+ jgen.writeNumber(RubyNumeric.num2dbl(value));
159
+ break;
160
+ case Fixnum:
161
+ jgen.writeNumber(RubyNumeric.num2long(value));
162
+ break;
163
+ case Bignum:
164
+ jgen.writeNumber(((RubyBignum) value).getBigIntegerValue());
165
+ break;
166
+ case BigDecimal:
167
+ jgen.writeNumber(((RubyBigDecimal) value).getBigDecimalValue());
168
+ break;
169
+ case Struct:
170
+ IRubyObject obj = value.callMethod(value.getRuntime().getCurrentContext(), "to_a");
171
+ serializeArray(obj, jgen, provider);
172
+ break;
173
+ case Time:
174
+ serializeTime((RubyTime) value, jgen, provider);
175
+ break;
176
+ default:
177
+ serializeUnknownRubyObject(value.getRuntime().getCurrentContext(), value, jgen, provider);
178
+ break;
179
+ }
180
+ }
181
+ }
182
+
183
+ private void serializeArray(IRubyObject value, JsonGenerator jgen, SerializerProvider provider)
184
+ throws IOException, JsonGenerationException {
185
+ // System.err.println("----->> RubyArray");
186
+
187
+ RubyArray arr = (RubyArray) value;
188
+ IRubyObject[] a = arr.toJavaArray();
189
+ jgen.writeStartArray();
190
+ for (IRubyObject val : a) {
191
+ serialize(val, jgen, provider);
192
+ }
193
+ jgen.writeEndArray();
194
+ }
195
+
196
+ private void serializeHash(IRubyObject value, JsonGenerator jgen, SerializerProvider provider)
197
+ throws IOException, JsonGenerationException {
198
+ // System.err.println("----->> RubyHash");
199
+
200
+ RubyHash h = (RubyHash) value;
201
+ jgen.writeStartObject();
202
+ for (Object o : h.directEntrySet()) {
203
+ RubyHash.RubyHashEntry next = (RubyHash.RubyHashEntry) o;
204
+ serializeKey((IRubyObject) next.getKey(), jgen, provider);
205
+ serialize((IRubyObject) next.getValue(), jgen, provider);
206
+ }
207
+ jgen.writeEndObject();
208
+ }
209
+
210
+ private void serializeTime(RubyTime dt, JsonGenerator jgen, SerializerProvider provider)
211
+ throws IOException, JsonGenerationException {
212
+ DateFormat df = provider.getConfig().getDateFormat();
213
+ if (df == null) {
214
+ // DateFormat should always be set
215
+ provider.defaultSerializeDateValue(dt.getJavaDate(), jgen);
216
+ } else if (df instanceof RubyDateFormat) {
217
+ // why another branch? I thought there was an easy win on to_s
218
+ // maybe with jruby 9000
219
+ RubyDateFormat rdf = (RubyDateFormat) df.clone();
220
+ jgen.writeString(rdf.format(dt.getJavaDate()));
221
+ } else {
222
+ SimpleDateFormat sdf = (SimpleDateFormat) df.clone();
223
+ jgen.writeString(df.format(dt.getJavaDate()));
224
+ }
225
+ }
226
+
227
+ private void serializeKey(IRubyObject key, JsonGenerator jgen, SerializerProvider provider)
228
+ throws IOException, JsonGenerationException {
229
+ if (key instanceof RubyString) {
230
+ jgen.writeFieldName(((RubyString) key).decodeString());
231
+ } else {
232
+ // includes RubySymbol and non RubyString objects
233
+ jgen.writeFieldName(key.toString());
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Default implementation will write type prefix, call regular serialization method (since assumption is that value itself does not need JSON Array or Object start/end markers), and then write type suffix. This should work for most cases; some sub-classes may want to change this behavior.
239
+ *
240
+ * @param value
241
+ * @param jgen
242
+ * @param provider
243
+ * @param typeSer
244
+ * @throws java.io.IOException
245
+ * @throws com.fasterxml.jackson.core.JsonGenerationException
246
+ */
247
+ @Override
248
+ public void serializeWithType(IRubyObject value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer)
249
+ throws IOException, JsonGenerationException {
250
+ typeSer.writeTypePrefixForScalar(value, jgen);
251
+ serialize(value, jgen, provider);
252
+ typeSer.writeTypeSuffixForScalar(value, jgen);
253
+ }
254
+ }
@@ -0,0 +1,18 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import java.io.IOException;
5
+ import org.jruby.Ruby;
6
+ import org.jruby.RubyObject;
7
+
8
+ /**
9
+ *
10
+ * @author Guy Boertje
11
+ */
12
+ public class RubyBigDecimalValueConverter implements RubyConverter {
13
+
14
+ @Override
15
+ public RubyObject convert(Ruby ruby, JsonParser jp) throws IOException {
16
+ return RubyUtils.rubyBigDecimal(ruby, jp.getDecimalValue());
17
+ }
18
+ }
@@ -0,0 +1,21 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import java.io.IOException;
5
+ import org.jruby.Ruby;
6
+ import org.jruby.RubyObject;
7
+
8
+ /**
9
+ *
10
+ * @author Guy Boertje
11
+ */
12
+ public class RubyBigIntValueConverter implements RubyConverter {
13
+
14
+ @Override
15
+ public RubyObject convert(Ruby ruby, JsonParser jp) throws IOException {
16
+ if (jp.getNumberType() == JsonParser.NumberType.BIG_INTEGER) {
17
+ return RubyUtils.rubyBignum(ruby, jp.getBigIntegerValue());
18
+ }
19
+ return RubyUtils.rubyFixnum(ruby, jp.getLongValue());
20
+ }
21
+ }
@@ -0,0 +1,12 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.*;
4
+
5
+ import java.io.IOException;
6
+ import org.jruby.Ruby;
7
+ import org.jruby.RubyObject;
8
+
9
+ public interface RubyConverter {
10
+
11
+ public RubyObject convert(Ruby ruby, JsonParser jp) throws IOException;
12
+ }
@@ -0,0 +1,34 @@
1
+ /*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+ package com.jrjackson;
7
+
8
+ import java.text.DateFormatSymbols;
9
+ import java.text.SimpleDateFormat;
10
+ import java.util.Locale;
11
+
12
+ /**
13
+ *
14
+ * @author guy
15
+ */
16
+ public class RubyDateFormat extends SimpleDateFormat{
17
+
18
+ public RubyDateFormat() {
19
+ }
20
+
21
+ public RubyDateFormat(String pattern) {
22
+ super(pattern);
23
+ }
24
+
25
+ public RubyDateFormat(String pattern, Locale locale) {
26
+ super(pattern, locale);
27
+ }
28
+
29
+ public RubyDateFormat(String pattern, DateFormatSymbols formatSymbols) {
30
+
31
+ super(pattern, formatSymbols);
32
+ }
33
+
34
+ }
@@ -0,0 +1,18 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import java.io.IOException;
5
+ import org.jruby.Ruby;
6
+ import org.jruby.RubyObject;
7
+
8
+ /**
9
+ *
10
+ * @author Guy Boertje
11
+ */
12
+ public class RubyFloatValueConverter implements RubyConverter {
13
+
14
+ @Override
15
+ public RubyObject convert(Ruby ruby, JsonParser jp) throws IOException {
16
+ return RubyUtils.rubyFloat(ruby, jp.getDoubleValue());
17
+ }
18
+ }
@@ -0,0 +1,119 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import java.io.IOException;
5
+ import org.jruby.Ruby;
6
+ import org.jruby.RubyArray;
7
+ import org.jruby.RubyHash;
8
+ import org.jruby.runtime.ThreadContext;
9
+ import org.jruby.runtime.builtin.IRubyObject;
10
+
11
+ /**
12
+ *
13
+ * @author Guy Boertje
14
+ */
15
+ public class RubyHandler implements IParseHandler<IRubyObject, RubyArray, RubyHash> {
16
+
17
+ private final Ruby _ruby;
18
+ private final ThreadContext _ctx;
19
+ private final RubyNameConverter _keyConv;
20
+ private final RubyConverter _intConv;
21
+ private final RubyConverter _floatConv;
22
+ private final RubyConverter _strConv;
23
+ private IRubyObject _result;
24
+
25
+ public RubyHandler(ThreadContext ctx,
26
+ RubyNameConverter keyConverter,
27
+ RubyConverter intConverter,
28
+ RubyConverter floatConverter) {
29
+
30
+ _ctx = ctx;
31
+ _ruby = ctx.runtime;
32
+ _keyConv = keyConverter;
33
+ _intConv = intConverter;
34
+ _floatConv = floatConverter;
35
+ _strConv = new RubyStringConverter();
36
+ }
37
+
38
+ @Override
39
+ public void addValue(IRubyObject value) {
40
+
41
+ _result = value;
42
+ }
43
+
44
+ @Override
45
+ public IRubyObject hashStart() {
46
+ return RubyHash.newHash(_ruby);
47
+ }
48
+
49
+ @Override
50
+ public void hashEnd() {
51
+
52
+ }
53
+
54
+ @Override
55
+ public IRubyObject hashKey(String key) {
56
+ return _keyConv.convert(_ruby, key);
57
+ }
58
+
59
+ @Override
60
+ public void hashSet(RubyHash hash, IRubyObject key, IRubyObject value) {
61
+ hash.fastASet(key, value);
62
+ }
63
+
64
+ @Override
65
+ public IRubyObject arrayStart() {
66
+ return RubyArray.newArray(_ruby);
67
+ }
68
+
69
+ @Override
70
+ public void arrayEnd() {
71
+
72
+ }
73
+
74
+ @Override
75
+ public void arrayAppend(RubyArray array, IRubyObject value) {
76
+ array.append(value);
77
+ }
78
+
79
+ @Override
80
+ public IRubyObject treatNull() {
81
+ return _ctx.nil;
82
+ }
83
+
84
+ @Override
85
+ public IRubyObject treatInt(JsonParser jp) throws IOException {
86
+ return _intConv.convert(_ruby, jp);
87
+ }
88
+
89
+ @Override
90
+ public IRubyObject treatFloat(JsonParser jp) throws IOException {
91
+ return _floatConv.convert(_ruby, jp);
92
+ }
93
+
94
+ @Override
95
+ public IRubyObject treatString(JsonParser jp) throws IOException {
96
+ return _strConv.convert(_ruby, jp);
97
+ }
98
+
99
+ @Override
100
+ public IRubyObject trueValue() {
101
+ return _ruby.newBoolean(true);
102
+ }
103
+
104
+ @Override
105
+ public IRubyObject falseValue() {
106
+ return _ruby.newBoolean(false);
107
+ }
108
+
109
+ @Override
110
+ public IRubyObject getResult() {
111
+ return _result;
112
+ }
113
+
114
+ @Override
115
+ public void raiseError(String e) {
116
+ throw ParseError.newParseError(_ruby, e);
117
+ }
118
+
119
+ }