jrjackson 0.2.9 → 0.3.0

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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/{.jrubyrc → .__jrubyrc} +2 -2
  3. data/.gitignore +5 -0
  4. data/Gemfile +1 -0
  5. data/alt_bench.rb +46 -0
  6. data/dependency-reduced-pom.xml +6 -5
  7. data/lib/jrjackson/build_info.rb +2 -2
  8. data/lib/jrjackson/jars/jrjackson-1.2.16.jar +0 -0
  9. data/lib/jrjackson/jrjackson.rb +38 -13
  10. data/pom.xml +11 -10
  11. data/run_all_individual_bench.sh +17 -15
  12. data/run_jruby_individual_bench.sh +20 -0
  13. data/run_mri_individual_bench.sh +7 -0
  14. data/src/main/java/com/jrjackson/IParseHandler.java +53 -0
  15. data/src/main/java/com/jrjackson/JavaBigDecimalValueConverter.java +17 -0
  16. data/src/main/java/com/jrjackson/JavaBigIntValueConverter.java +17 -0
  17. data/src/main/java/com/jrjackson/JavaConverter.java +10 -0
  18. data/src/main/java/com/jrjackson/JavaFloatValueConverter.java +16 -0
  19. data/src/main/java/com/jrjackson/JavaHandler.java +118 -0
  20. data/src/main/java/com/jrjackson/JavaLongValueConverter.java +16 -0
  21. data/src/main/java/com/jrjackson/JjParse.java +147 -0
  22. data/src/main/java/com/jrjackson/JrJacksonBase.java +159 -0
  23. data/src/main/java/com/jrjackson/JrJacksonJava.java +81 -0
  24. data/src/main/java/com/jrjackson/JrJacksonRaw.java +59 -102
  25. data/src/main/java/com/jrjackson/JrJacksonRuby.java +117 -0
  26. data/src/main/java/com/jrjackson/JrJacksonSaj.java +26 -0
  27. data/src/main/java/com/jrjackson/JrJacksonSch.java +25 -0
  28. data/src/main/java/com/jrjackson/JrJacksonService.java +15 -0
  29. data/src/main/java/com/jrjackson/JrParse.java +149 -0
  30. data/src/main/java/com/jrjackson/RubyAnySerializer.java +112 -55
  31. data/src/main/java/com/jrjackson/RubyBigDecimalValueConverter.java +18 -0
  32. data/src/main/java/com/jrjackson/RubyBigIntValueConverter.java +21 -0
  33. data/src/main/java/com/jrjackson/RubyConverter.java +12 -0
  34. data/src/main/java/com/jrjackson/RubyDateFormat.java +36 -0
  35. data/src/main/java/com/jrjackson/RubyFloatValueConverter.java +18 -0
  36. data/src/main/java/com/jrjackson/RubyHandler.java +119 -0
  37. data/src/main/java/com/jrjackson/RubyIntValueConverter.java +18 -0
  38. data/src/main/java/com/jrjackson/RubyJacksonModule.java +40 -38
  39. data/src/main/java/com/jrjackson/RubyNameConverter.java +9 -0
  40. data/src/main/java/com/jrjackson/RubyObjectDeserializer.java +24 -33
  41. data/src/main/java/com/jrjackson/RubyStringConverter.java +1 -2
  42. data/src/main/java/com/jrjackson/{RubySymbolConverter.java → RubyStringKeyConverter.java} +3 -2
  43. data/src/main/java/com/jrjackson/RubyStringNameConverter.java +12 -0
  44. data/src/main/java/com/jrjackson/RubySymbolKeyConverter.java +15 -0
  45. data/src/main/java/com/jrjackson/RubySymbolNameConverter.java +12 -0
  46. data/src/main/java/com/jrjackson/RubyUtils.java +40 -1
  47. data/src/main/java/com/jrjackson/SajParse.java +169 -0
  48. data/src/main/java/com/jrjackson/SchParse.java +209 -0
  49. data/src/main/java/com/jrjackson/StreamParse.java +66 -0
  50. data/test/jrjackson_test.rb +271 -6
  51. metadata +49 -17
  52. data/src/test/java/com/jrjackson/jruby/AppTest.java +0 -38
@@ -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,159 @@
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 java.io.ByteArrayOutputStream;
15
+ import org.jruby.RubyHash;
16
+ import org.jruby.RubyIO;
17
+ import org.jruby.RubyObject;
18
+ import org.jruby.RubyString;
19
+ import org.jruby.anno.JRubyMethod;
20
+ import org.jruby.exceptions.RaiseException;
21
+ import org.jruby.ext.stringio.StringIO;
22
+ import org.jruby.runtime.ThreadContext;
23
+ import org.jruby.runtime.builtin.IRubyObject;
24
+
25
+ import java.io.IOException;
26
+ import java.text.SimpleDateFormat;
27
+ import java.util.TimeZone;
28
+ import org.jcodings.specific.UTF8Encoding;
29
+ import org.jruby.Ruby;
30
+ import org.jruby.RubyClass;
31
+ import org.jruby.RubySymbol;
32
+ import org.jruby.util.ByteList;
33
+
34
+ /**
35
+ *
36
+ * @author guy
37
+ */
38
+ public class JrJacksonBase extends RubyObject {
39
+
40
+ private static final SimpleDateFormat RDF = new RubyDateFormat("yyyy-MM-dd HH:mm:ss Z");
41
+
42
+ // serialize
43
+ @JRubyMethod(module = true, name = {"generate", "dump"}, required = 1, optional = 1)
44
+ public static IRubyObject generate(ThreadContext context, IRubyObject self, IRubyObject[] args)
45
+ throws IOException, RaiseException {
46
+ Ruby _ruby = context.runtime;
47
+ RubyHash options = (args.length <= 1) ? RubyHash.newHash(_ruby) : args[1].convertToHash();
48
+ String format = (String) options.get(RubyUtils.rubySymbol(_ruby, "date_format"));
49
+
50
+ // StringWriter out = new StringWriter();
51
+ // JsonGenerator jgen = RubyJacksonModule.factory.createGenerator(out);
52
+
53
+
54
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
55
+ JsonGenerator jgen = RubyJacksonModule.factory.createGenerator(
56
+ baos, JsonEncoding.UTF8);
57
+
58
+ if (flagged(options, RubyUtils.rubySymbol(_ruby, "pretty"))) {
59
+ jgen.useDefaultPrettyPrinter();
60
+ }
61
+
62
+ SimpleDateFormat simpleFormat;
63
+ if (format != null) {
64
+ simpleFormat = new SimpleDateFormat(format);
65
+ String timezone = (String) options.get(RubyUtils.rubySymbol(_ruby, "timezone"));
66
+ if (timezone != null) {
67
+ simpleFormat.setTimeZone(TimeZone.getTimeZone(timezone));
68
+ }
69
+ } else {
70
+ // using a 'marker' class instance, will not use later but default to #to_s
71
+ simpleFormat = RDF;
72
+ }
73
+
74
+ try {
75
+ RubyAnySerializer.instance.serialize(args[0], jgen,
76
+ RubyJacksonModule.createProvider(simpleFormat));
77
+ jgen.close();
78
+ ByteList bl = new ByteList(baos.toByteArray(),
79
+ UTF8Encoding.INSTANCE);
80
+ return RubyString.newString(_ruby, bl);
81
+ // return RubyUtils.rubyString(_ruby, out.toString());
82
+ } catch (JsonProcessingException e) {
83
+ throw ParseError.newParseError(_ruby, e.getLocalizedMessage());
84
+ }
85
+ }
86
+
87
+ protected static boolean flagged(RubyHash opts, RubySymbol key) {
88
+ Object val = opts.get(key);
89
+ if (val == null) {
90
+ return false;
91
+ }
92
+ boolean flag = (Boolean) val;
93
+ return flag;
94
+ }
95
+
96
+ protected static boolean flagged(RubyHash opts, RubySymbol key, boolean returnVal) {
97
+ if(!opts.containsKey(key)) {
98
+ return returnVal;
99
+ }
100
+ Object val = opts.get(key);
101
+ if (val == null) {
102
+ return returnVal;
103
+ }
104
+ boolean flag = (Boolean) val;
105
+ return flag;
106
+ }
107
+
108
+ protected static IRubyObject _sjcparse(ThreadContext context, IRubyObject handler, IRubyObject arg, IRubyObject opts, StreamParse sp) throws RaiseException {
109
+
110
+ JsonParser jp;
111
+ try {
112
+ jp = buildParser(context, RubyJacksonModule.factory, arg);
113
+ } catch (IOException e) {
114
+ throw context.runtime.newIOError(e.getLocalizedMessage());
115
+ }
116
+ return sp.deserialize(jp);
117
+ }
118
+
119
+ protected static JsonParser buildParser(ThreadContext ctx, JsonFactory jf, IRubyObject arg) throws IOException {
120
+ if (arg instanceof RubyString) {
121
+ return jf.createParser(((RubyString) arg).getByteList().bytes());
122
+ } else if (arg instanceof StringIO) {
123
+ RubyString content = (RubyString) ((StringIO) arg).string(ctx);
124
+ return jf.createParser(content.getByteList().bytes());
125
+ } else {
126
+ // must be an IO object then
127
+ return jf.createParser(((RubyIO) arg).getInStream());
128
+ }
129
+ }
130
+
131
+ protected static IRubyObject _parse(ThreadContext context, IRubyObject arg, ObjectMapper mapper) throws IOException, RaiseException {
132
+ Ruby ruby = context.runtime;
133
+ // same format as Ruby Time #to_s
134
+ SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
135
+ mapper.setDateFormat(simpleFormat);
136
+ try {
137
+ Object o;
138
+ if (arg instanceof RubyString) {
139
+ o = mapper.readValue(((RubyString) arg).getByteList().bytes(), Object.class);
140
+ } else if (arg instanceof StringIO) {
141
+ RubyString content = (RubyString) ((StringIO) arg).string(context);
142
+ o = mapper.readValue(content.getByteList().bytes(), Object.class);
143
+ } else {
144
+ // must be an IO object then
145
+ o = mapper.readValue(((RubyIO) arg).getInStream(), Object.class);
146
+ }
147
+ return RubyUtils.rubyObject(ruby, o);
148
+ } catch (JsonProcessingException e) {
149
+ throw ParseError.newParseError(ruby, e.getLocalizedMessage());
150
+ } catch (IOException e) {
151
+ throw ruby.newIOError(e.getLocalizedMessage());
152
+ }
153
+ }
154
+
155
+ public JrJacksonBase(Ruby runtime, RubyClass metaClass) {
156
+ super(runtime, metaClass);
157
+ }
158
+
159
+ }
@@ -0,0 +1,81 @@
1
+ package com.jrjackson;
2
+
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import org.jruby.Ruby;
5
+ import org.jruby.RubyClass;
6
+ import org.jruby.anno.JRubyModule;
7
+ import org.jruby.anno.JRubyMethod;
8
+ import org.jruby.runtime.ThreadContext;
9
+ import org.jruby.runtime.builtin.IRubyObject;
10
+
11
+ import java.io.IOException;
12
+
13
+ import com.fasterxml.jackson.core.JsonProcessingException;
14
+ import org.jruby.RubyHash;
15
+ import org.jruby.exceptions.RaiseException;
16
+
17
+
18
+ @JRubyModule(name = "JrJacksonJava")
19
+ public class JrJacksonJava extends JrJacksonBase {
20
+
21
+ public JrJacksonJava(Ruby ruby, RubyClass metaclass) {
22
+ super(ruby, metaclass);
23
+ }
24
+
25
+ // deserialize
26
+ @JRubyMethod(module = true, name = {"parse", "load"}, required = 2)
27
+ public static IRubyObject parse(ThreadContext context, IRubyObject self, IRubyObject arg, IRubyObject opts)
28
+ throws JsonProcessingException, IOException, RaiseException {
29
+
30
+ JavaConverter ikonv = new JavaBigIntValueConverter();
31
+ JavaConverter dkonv = new JavaBigDecimalValueConverter();
32
+ if (opts != context.nil) {
33
+ RubyHash options = opts.convertToHash();
34
+ if (options.size() > 0) {
35
+ if (!flagged(options,
36
+ RubyUtils.rubySymbol(context.runtime, "use_bigdecimal"), true)) {
37
+ dkonv = new JavaFloatValueConverter();
38
+ }
39
+ if (flagged(options,
40
+ RubyUtils.rubySymbol(context.runtime, "use_smallint"))) {
41
+ ikonv = new JavaLongValueConverter();
42
+ }
43
+ }
44
+ }
45
+
46
+ JavaHandler handler = new JavaHandler(ikonv, dkonv);
47
+ JjParse parse = new JjParse(handler);
48
+ JsonParser jp;
49
+ try {
50
+ jp = buildParser(context, RubyJacksonModule.factory, arg);
51
+ } catch (IOException e) {
52
+ throw context.runtime.newIOError(e.getLocalizedMessage());
53
+ }
54
+
55
+ parse.deserialize(jp);
56
+ jp.close();
57
+ return RubyUtils.rubyObject(context.runtime, handler.getResult());
58
+ }
59
+
60
+ // deserialize
61
+ @JRubyMethod(module = true, name = {"parse_raw", "load_raw"}, required = 2)
62
+ public static IRubyObject parse_raw(ThreadContext context, IRubyObject self, IRubyObject arg, IRubyObject opts)
63
+ throws JsonProcessingException, IOException, RaiseException {
64
+
65
+ JavaConverter ikonv = new JavaBigIntValueConverter();
66
+ JavaConverter dkonv = new JavaBigDecimalValueConverter();
67
+
68
+ JavaHandler handler = new JavaHandler(ikonv, dkonv);
69
+ JjParse parse = new JjParse(handler);
70
+ JsonParser jp;
71
+ try {
72
+ jp = buildParser(context, RubyJacksonModule.factory, arg);
73
+ } catch (IOException e) {
74
+ throw context.runtime.newIOError(e.getLocalizedMessage());
75
+ }
76
+
77
+ parse.deserialize(jp);
78
+ jp.close();
79
+ return RubyUtils.rubyObject(context.runtime, handler.getResult());
80
+ }
81
+ }
@@ -2,150 +2,107 @@ package com.jrjackson;
2
2
 
3
3
  import org.jruby.Ruby;
4
4
  import org.jruby.RubyClass;
5
- import org.jruby.RubyObject;
6
- import org.jruby.RubyString;
7
- import org.jruby.RubySymbol;
8
5
  import org.jruby.RubyHash;
9
- import org.jruby.RubyIO;
10
6
  import org.jruby.anno.JRubyMethod;
11
7
  import org.jruby.anno.JRubyModule;
12
8
  import org.jruby.runtime.ThreadContext;
13
9
  import org.jruby.runtime.builtin.IRubyObject;
14
- import org.jruby.ext.stringio.StringIO;
15
10
 
16
11
  import java.io.IOException;
17
12
 
18
- import java.text.SimpleDateFormat;
19
- import java.util.TimeZone;
20
-
21
13
  import com.fasterxml.jackson.databind.ObjectMapper;
22
14
  import com.fasterxml.jackson.databind.DeserializationFeature;
23
- import com.fasterxml.jackson.core.JsonProcessingException;
15
+ import org.jruby.exceptions.RaiseException;
24
16
 
25
17
 
26
18
  @JRubyModule(name = "JrJacksonRaw")
27
- public class JrJacksonRaw extends RubyObject {
19
+ public class JrJacksonRaw extends JrJacksonBase {
28
20
 
29
21
  public JrJacksonRaw(Ruby ruby, RubyClass metaclass) {
30
22
  super(ruby, metaclass);
31
23
  }
32
24
 
33
- private static boolean flagged(RubyHash opts, RubySymbol key) {
34
- Object val = opts.get(key);
35
- if (val == null) {
36
- return false;
37
- }
38
- return (Boolean) val;
39
- }
40
-
41
25
  // deserialize
42
26
  @JRubyMethod(module = true, name = {"parse", "load"}, required = 2)
43
27
  public static IRubyObject parse(ThreadContext context, IRubyObject self, IRubyObject arg, IRubyObject opts)
44
28
  throws IOException {
45
- RubyHash options = null;
46
- ObjectMapper local = null;
47
29
  Ruby _ruby = context.runtime;
30
+ RubyHash options = null;
48
31
 
49
32
  if (opts != context.nil) {
50
33
  options = opts.convertToHash();
51
- if (flagged(options, RubyUtils.rubySymbol(_ruby, "symbolize_keys"))) {
52
- local = RubyJacksonModule.mappedAs("sym", _ruby);
53
- }
54
- if (flagged(options, RubyUtils.rubySymbol(_ruby, "raw"))) {
55
- local = RubyJacksonModule.mappedAs("raw", _ruby);
56
- }
57
- if (local == null) {
58
- local = RubyJacksonModule.mappedAs("str", _ruby);
59
- }
60
- if (flagged(options, RubyUtils.rubySymbol(_ruby, "use_bigdecimal"))) {
61
- local.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
62
- } else {
63
- local.disable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
64
- }
34
+
35
+ return direct(context, self, arg,
36
+ flagged(options, RubyUtils.rubySymbol(_ruby, "use_bigdecimal")),
37
+ !flagged(options, RubyUtils.rubySymbol(_ruby, "use_smallint")),
38
+ flagged(options, RubyUtils.rubySymbol(_ruby, "symbolize_keys")),
39
+ flagged(options, RubyUtils.rubySymbol(_ruby, "raw")));
40
+
65
41
  } else {
66
- local = RubyJacksonModule.mappedAs("str", _ruby);
42
+ return direct(context, self, arg, false, true, false, false);
43
+ }
44
+ }
45
+
46
+ private static IRubyObject direct(ThreadContext context, IRubyObject self, IRubyObject arg,
47
+ boolean use_big_decimal, boolean use_big_int,
48
+ boolean use_symbols, boolean use_raw) throws IOException {
49
+ ObjectMapper local = RubyJacksonModule.rawBigNumberMapper();
50
+
51
+ RubyConverter vci = new RubyIntValueConverter();
52
+ RubyConverter vcf = new RubyFloatValueConverter();
53
+ RubyKeyConverter kcn;
54
+
55
+ if (use_big_decimal) {
56
+ vcf = new RubyBigDecimalValueConverter();
57
+ } else {
58
+ local.disable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
59
+ }
60
+
61
+ if (use_big_int) {
62
+ vci = new RubyBigIntValueConverter();
63
+ } else {
64
+ local.disable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
65
+ }
66
+
67
+ if(use_raw) {
68
+ return _parse(context, arg, local);
67
69
  }
70
+
71
+ if (use_symbols) {
72
+ kcn = new RubySymbolKeyConverter();
73
+ } else {
74
+ kcn = new RubyStringKeyConverter();
75
+ }
76
+
77
+ local = RubyJacksonModule.mapperWith(context.runtime, kcn, vci, vcf);
68
78
  return _parse(context, arg, local);
69
79
  }
70
80
 
71
81
  @JRubyMethod(module = true, name = {"parse_raw", "load_raw"}, required = 1)
72
82
  public static IRubyObject parse_raw(ThreadContext context, IRubyObject self, IRubyObject arg)
73
- throws IOException {
74
- ObjectMapper mapper = RubyJacksonModule.mappedAs("raw", context.runtime);
75
- return _parse(context, arg, mapper);
83
+ throws IOException, RaiseException {
84
+ return direct(context, self, arg, false, false, false, true);
85
+ }
86
+
87
+ @JRubyMethod(module = true, name = {"parse_raw_bd", "load_raw_bd"}, required = 1)
88
+ public static IRubyObject parse_raw_bd(ThreadContext context, IRubyObject self, IRubyObject arg)
89
+ throws IOException, RaiseException {
90
+
91
+ return direct(context, self, arg, true, true, false, true);
76
92
  }
77
93
 
78
94
  @JRubyMethod(module = true, name = {"parse_sym", "load_sym"}, required = 1)
79
95
  public static IRubyObject parse_sym(ThreadContext context, IRubyObject self, IRubyObject arg)
80
- throws IOException {
81
- ObjectMapper mapper = RubyJacksonModule.mappedAs("sym", context.runtime);
82
- return _parse(context, arg, mapper);
96
+ throws IOException, RaiseException {
97
+
98
+ return direct(context, self, arg, false, false, true, false);
83
99
  }
84
100
 
85
101
  @JRubyMethod(module = true, name = {"parse_str", "load_str"}, required = 1)
86
102
  public static IRubyObject parse_str(ThreadContext context, IRubyObject self, IRubyObject arg)
87
- throws IOException {
88
- ObjectMapper mapper = RubyJacksonModule.mappedAs("str", context.runtime);
89
- return _parse(context, arg, mapper);
90
- }
103
+ throws IOException, RaiseException {
91
104
 
92
- private static IRubyObject _parse(ThreadContext context, IRubyObject arg, ObjectMapper mapper)
93
- throws IOException {
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);
98
- try {
99
- Object o;
100
- if (arg instanceof RubyString) {
101
- o = mapper.readValue(
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
108
- );
109
- } else {
110
- // must be an IO object then
111
- o = mapper.readValue(((RubyIO)arg).getInStream(), Object.class);
112
- }
113
- return RubyUtils.rubyObject(ruby, o);
114
- } catch (JsonProcessingException e) {
115
- throw ParseError.newParseError(ruby, e.getLocalizedMessage());
116
- } catch (IOException e) {
117
- throw ruby.newIOError(e.getLocalizedMessage());
118
- }
119
- }
105
+ return direct(context, self, arg, false, true, false, false);
120
106
 
121
- // serialize
122
- @JRubyMethod(module = true, name = {"generate", "dump"}, required = 1, optional = 1)
123
- public static IRubyObject generate(ThreadContext context, IRubyObject self, IRubyObject[] args)
124
- throws IOException, JsonProcessingException {
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
- try {
143
- String s = mapper.writeValueAsString(obj);
144
- return RubyUtils.rubyString(_ruby, s);
145
- } catch (JsonProcessingException e) {
146
- throw ParseError.newParseError(_ruby, e.getLocalizedMessage());
147
- } catch (IOException e) {
148
- throw _ruby.newIOError(e.getLocalizedMessage());
149
- }
150
107
  }
151
108
  }