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,17 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import java.io.IOException;
5
+ import java.math.BigInteger;
6
+
7
+ /**
8
+ *
9
+ * @author Guy Boertje
10
+ */
11
+ public class JavaBigIntValueConverter implements JavaConverter {
12
+
13
+ @Override
14
+ public BigInteger convert(JsonParser jp) throws IOException {
15
+ return jp.getBigIntegerValue();
16
+ }
17
+ }
@@ -0,0 +1,10 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.*;
4
+
5
+ import java.io.IOException;
6
+
7
+ public interface JavaConverter {
8
+
9
+ public Object convert(JsonParser jp) throws IOException;
10
+ }
@@ -0,0 +1,16 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import java.io.IOException;
5
+
6
+ /**
7
+ *
8
+ * @author Guy Boertje
9
+ */
10
+ public class JavaFloatValueConverter implements JavaConverter {
11
+
12
+ @Override
13
+ public Double convert(JsonParser jp) throws IOException {
14
+ return jp.getDoubleValue();
15
+ }
16
+ }
@@ -0,0 +1,118 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import java.io.IOException;
5
+ import java.util.ArrayList;
6
+ import java.util.HashMap;
7
+
8
+
9
+ /**
10
+ *
11
+ * @author Guy Boertje
12
+ */
13
+
14
+ /*
15
+ Long -> Integer
16
+ Double -> Float
17
+ JavaMath::BigDecimal -> BigDecimal
18
+ JavaMath::BigInteger -> Bignum
19
+ JavaUtil::ArrayList -> Array
20
+ JavaUtil::LinkedHashMap -> Hash
21
+ String -> String
22
+ */
23
+
24
+ public class JavaHandler implements IParseHandler<Object, ArrayList<Object>, HashMap<String, Object>> {
25
+
26
+ private Object _result;
27
+ private final JavaConverter _intConv;
28
+ private final JavaConverter _floatConv;
29
+
30
+ public JavaHandler(
31
+ JavaConverter intConverter,
32
+ JavaConverter floatConverter) {
33
+
34
+ _intConv = intConverter;
35
+ _floatConv = floatConverter;
36
+
37
+ }
38
+
39
+ @Override
40
+ public void addValue(Object value) {
41
+ _result = value;
42
+ }
43
+
44
+ @Override
45
+ public Object hashStart() {
46
+ return new HashMap<String, Object>();
47
+ }
48
+
49
+ @Override
50
+ public void hashEnd() {
51
+
52
+ }
53
+
54
+ @Override
55
+ public Object hashKey(String key) {
56
+ return key;
57
+ }
58
+
59
+ @Override
60
+ public void hashSet(HashMap<String, Object> hash, Object key, Object value) {
61
+ hash.put((String)key, value);
62
+ }
63
+
64
+ @Override
65
+ public Object arrayStart() {
66
+ return new ArrayList<Object>();
67
+ }
68
+
69
+ @Override
70
+ public void arrayEnd() {
71
+
72
+ }
73
+
74
+ @Override
75
+ public void arrayAppend(ArrayList<Object> array, Object value) {
76
+ array.add(value);
77
+ }
78
+
79
+ @Override
80
+ public Object treatNull() {
81
+ return null;
82
+ }
83
+
84
+ @Override
85
+ public Object treatInt(JsonParser jp) throws IOException {
86
+ return _intConv.convert(jp);
87
+ }
88
+
89
+ @Override
90
+ public Object treatFloat(JsonParser jp) throws IOException {
91
+ return _floatConv.convert(jp);
92
+ }
93
+
94
+ @Override
95
+ public Object treatString(JsonParser jp) throws IOException {
96
+ return jp.getText();
97
+ }
98
+
99
+ @Override
100
+ public Object trueValue() {
101
+ return true;
102
+ }
103
+
104
+ @Override
105
+ public Object falseValue() {
106
+ return false;
107
+ }
108
+
109
+ @Override
110
+ public Object getResult() {
111
+ return _result;
112
+ }
113
+
114
+ @Override
115
+ public void raiseError(String e) {
116
+
117
+ }
118
+ }
@@ -0,0 +1,16 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import java.io.IOException;
5
+
6
+ /**
7
+ *
8
+ * @author Guy Boertje
9
+ */
10
+ public class JavaLongValueConverter implements JavaConverter {
11
+
12
+ @Override
13
+ public Long convert(JsonParser jp) throws IOException {
14
+ return jp.getLongValue();
15
+ }
16
+ }
@@ -0,0 +1,147 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import com.fasterxml.jackson.core.JsonProcessingException;
5
+ import com.fasterxml.jackson.core.JsonStreamContext;
6
+ import java.io.IOException;
7
+ import java.util.ArrayList;
8
+ import java.util.HashMap;
9
+
10
+
11
+ /**
12
+ *
13
+ * @author Guy Boertje
14
+ */
15
+ public class JjParse {
16
+ private final JavaHandler _handler;
17
+ protected final HashMap<JsonStreamContext, Object> _objectMap = new HashMap<JsonStreamContext, Object>();
18
+ protected JsonStreamContext _deepestContext;
19
+
20
+ public JjParse(JavaHandler handler) {
21
+ _handler = handler;
22
+
23
+ }
24
+
25
+ public void deserialize(JsonParser jp) throws JsonProcessingException, IOException {
26
+ try {
27
+
28
+ while (jp.nextValue() != null) {
29
+ handleCurrentToken(jp);
30
+ }
31
+
32
+ } catch (JsonProcessingException e) {
33
+ _handler.raiseError(e.getLocalizedMessage());
34
+ } catch (IOException e) {
35
+ _handler.raiseError(e.getLocalizedMessage());
36
+ }
37
+ }
38
+
39
+ private void callAddValue(JsonStreamContext x) {
40
+ JsonStreamContext px = x.getParent();
41
+ Object dtarget = _objectMap.get(_deepestContext);
42
+
43
+ if (px == null) {
44
+ _handler.addValue(dtarget);
45
+ return;
46
+ }
47
+
48
+ Object value = _objectMap.get(x);
49
+
50
+ if (x.inArray()) {
51
+ _handler.arrayAppend(
52
+ (ArrayList<Object>)value, dtarget);
53
+ } else if (x.inObject()) {
54
+ _handler.hashSet(
55
+ (HashMap<String, Object>)value, callHashKey(x), dtarget);
56
+
57
+ } else {
58
+ _handler.addValue(value);
59
+ }
60
+ }
61
+
62
+ private void callAddValue(JsonStreamContext x, Object val) {
63
+
64
+ if (x.inArray()) {
65
+ ArrayList<Object> a = (ArrayList<Object>)_objectMap.get(x);
66
+ _handler.arrayAppend(a, val);
67
+ } else if (x.inObject()) {
68
+ HashMap<String, Object> h = (HashMap<String, Object>)_objectMap.get(x);
69
+ _handler.hashSet(h, callHashKey(x), val);
70
+
71
+ } else {
72
+ _handler.addValue(val);
73
+ }
74
+ }
75
+
76
+ private String callHashKey(JsonStreamContext x) {
77
+ String k = x.getCurrentName();
78
+ if (k == null) {
79
+ return null;
80
+ }
81
+ return (String)_handler.hashKey(k);
82
+ }
83
+
84
+ private void handleCurrentToken(JsonParser jp)
85
+ throws IOException, JsonProcessingException {
86
+
87
+ JsonStreamContext cx = jp.getParsingContext();
88
+
89
+ switch (jp.getCurrentToken()) {
90
+ case START_OBJECT:
91
+ _deepestContext = cx;
92
+ _objectMap.put(cx, _handler.hashStart());
93
+ break;
94
+
95
+ case START_ARRAY:
96
+ _deepestContext = cx;
97
+ _objectMap.put(cx, _handler.arrayStart());
98
+
99
+ case FIELD_NAME:
100
+ break;
101
+
102
+ case VALUE_EMBEDDED_OBJECT:
103
+ System.out.println("-------- VALUE_EMBEDDED_OBJECT ????????? --------");
104
+ System.out.println(jp.getEmbeddedObject());
105
+ break;
106
+
107
+ case VALUE_STRING:
108
+ callAddValue(cx,
109
+ _handler.treatString(jp));
110
+ break;
111
+
112
+ case VALUE_NUMBER_INT:
113
+ callAddValue(cx,
114
+ _handler.treatInt(jp));
115
+ break;
116
+
117
+ case VALUE_NUMBER_FLOAT:
118
+ callAddValue(cx,
119
+ _handler.treatFloat(jp));
120
+ break;
121
+
122
+ case VALUE_TRUE:
123
+ callAddValue(cx, _handler.trueValue());
124
+ break;
125
+
126
+ case VALUE_FALSE:
127
+ callAddValue(cx, _handler.falseValue());
128
+ break;
129
+
130
+ case VALUE_NULL: // should not get this but...
131
+ callAddValue(cx, _handler.treatNull());
132
+ break;
133
+
134
+ case END_ARRAY:
135
+ _handler.arrayEnd();
136
+ callAddValue(cx);
137
+ _deepestContext = cx;
138
+ break;
139
+
140
+ case END_OBJECT:
141
+ _handler.hashEnd();
142
+ callAddValue(cx);
143
+ _deepestContext = cx;
144
+ break;
145
+ }
146
+ }
147
+ }
@@ -0,0 +1,152 @@
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 com.fasterxml.jackson.core.JsonEncoding;
9
+ import com.fasterxml.jackson.core.JsonFactory;
10
+ import com.fasterxml.jackson.core.JsonGenerator;
11
+ import com.fasterxml.jackson.core.JsonParser;
12
+ import com.fasterxml.jackson.core.JsonProcessingException;
13
+ import com.fasterxml.jackson.databind.ObjectMapper;
14
+ import com.fasterxml.jackson.databind.SerializerProvider;
15
+ import java.io.ByteArrayOutputStream;
16
+ import org.jruby.RubyHash;
17
+ import org.jruby.RubyIO;
18
+ import org.jruby.RubyObject;
19
+ import org.jruby.RubyString;
20
+ import org.jruby.anno.JRubyMethod;
21
+ import org.jruby.exceptions.RaiseException;
22
+ import org.jruby.ext.stringio.StringIO;
23
+ import org.jruby.runtime.ThreadContext;
24
+ import org.jruby.runtime.builtin.IRubyObject;
25
+
26
+ import java.io.IOException;
27
+ import java.text.SimpleDateFormat;
28
+ import java.util.TimeZone;
29
+ import org.jcodings.specific.UTF8Encoding;
30
+ import org.jruby.Ruby;
31
+ import org.jruby.RubyClass;
32
+ import org.jruby.RubySymbol;
33
+ import org.jruby.util.ByteList;
34
+
35
+ /**
36
+ *
37
+ * @author guy
38
+ */
39
+ public class JrJacksonBase extends RubyObject {
40
+
41
+ // serialize
42
+ @JRubyMethod(module = true, name = {"generate", "dump"}, required = 1, optional = 1)
43
+ public static IRubyObject generate(ThreadContext context, IRubyObject self, IRubyObject[] args)
44
+ throws IOException, RaiseException {
45
+ Ruby _ruby = context.runtime;
46
+ RubyHash options = (args.length <= 1) ? RubyHash.newHash(_ruby) : args[1].convertToHash();
47
+ String format = (String) options.get(RubyUtils.rubySymbol(_ruby, "date_format"));
48
+
49
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
50
+ JsonGenerator jgen = RubyJacksonModule.factory.createGenerator(
51
+ baos, JsonEncoding.UTF8);
52
+
53
+ if (flagged(options, RubyUtils.rubySymbol(_ruby, "pretty"))) {
54
+ jgen.useDefaultPrettyPrinter();
55
+ }
56
+
57
+ SerializerProvider provider;
58
+ if (format != null) {
59
+ SimpleDateFormat simpleFormat = new SimpleDateFormat(format);
60
+ String timezone = (String) options.get(RubyUtils.rubySymbol(_ruby, "timezone"));
61
+ if (timezone != null) {
62
+ simpleFormat.setTimeZone(TimeZone.getTimeZone(timezone));
63
+ }
64
+ provider = RubyJacksonModule.createProvider(simpleFormat);
65
+ } else {
66
+ provider = RubyJacksonModule.createProvider();
67
+ }
68
+
69
+ try {
70
+ RubyAnySerializer.instance.serialize(args[0], jgen, provider);
71
+ jgen.close();
72
+ ByteList bl = new ByteList(baos.toByteArray(),
73
+ UTF8Encoding.INSTANCE);
74
+ return RubyString.newString(_ruby, bl);
75
+ } catch (JsonProcessingException e) {
76
+ throw ParseError.newParseError(_ruby, e.getLocalizedMessage());
77
+ }
78
+ }
79
+
80
+ protected static boolean flagged(RubyHash opts, RubySymbol key) {
81
+ Object val = opts.get(key);
82
+ if (val == null) {
83
+ return false;
84
+ }
85
+ boolean flag = (Boolean) val;
86
+ return flag;
87
+ }
88
+
89
+ protected static boolean flagged(RubyHash opts, RubySymbol key, boolean returnVal) {
90
+ if (!opts.containsKey(key)) {
91
+ return returnVal;
92
+ }
93
+ Object val = opts.get(key);
94
+ if (val == null) {
95
+ return returnVal;
96
+ }
97
+ boolean flag = (Boolean) val;
98
+ return flag;
99
+ }
100
+
101
+ protected static IRubyObject _sjcparse(ThreadContext context, IRubyObject handler, IRubyObject arg, IRubyObject opts, StreamParse sp) throws RaiseException {
102
+
103
+ JsonParser jp;
104
+ try {
105
+ jp = buildParser(context, RubyJacksonModule.factory, arg);
106
+ } catch (IOException e) {
107
+ throw context.runtime.newIOError(e.getLocalizedMessage());
108
+ }
109
+ return sp.deserialize(jp);
110
+ }
111
+
112
+ protected static JsonParser buildParser(ThreadContext ctx, JsonFactory jf, IRubyObject arg) throws IOException {
113
+ if (arg instanceof RubyString) {
114
+ return jf.createParser(((RubyString) arg).getByteList().bytes());
115
+ } else if (arg instanceof StringIO) {
116
+ RubyString content = (RubyString) ((StringIO) arg).string(ctx);
117
+ return jf.createParser(content.getByteList().bytes());
118
+ } else {
119
+ // must be an IO object then
120
+ return jf.createParser(((RubyIO) arg).getInStream());
121
+ }
122
+ }
123
+
124
+ protected static IRubyObject _parse(ThreadContext context, IRubyObject arg, ObjectMapper mapper) throws IOException, RaiseException {
125
+ Ruby ruby = context.runtime;
126
+ // same format as Ruby Time #to_s
127
+ SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
128
+ mapper.setDateFormat(simpleFormat);
129
+ try {
130
+ Object o;
131
+ if (arg instanceof RubyString) {
132
+ o = mapper.readValue(((RubyString) arg).getByteList().bytes(), Object.class);
133
+ } else if (arg instanceof StringIO) {
134
+ RubyString content = (RubyString) ((StringIO) arg).string(context);
135
+ o = mapper.readValue(content.getByteList().bytes(), Object.class);
136
+ } else {
137
+ // must be an IO object then
138
+ o = mapper.readValue(((RubyIO) arg).getInStream(), Object.class);
139
+ }
140
+ return RubyUtils.rubyObject(ruby, o);
141
+ } catch (JsonProcessingException e) {
142
+ throw ParseError.newParseError(ruby, e.getLocalizedMessage());
143
+ } catch (IOException e) {
144
+ throw ruby.newIOError(e.getLocalizedMessage());
145
+ }
146
+ }
147
+
148
+ public JrJacksonBase(Ruby runtime, RubyClass metaClass) {
149
+ super(runtime, metaClass);
150
+ }
151
+
152
+ }