jrjackson 0.1.1 → 0.2.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.
@@ -0,0 +1,23 @@
1
+ package com.jrjackson;
2
+
3
+ import java.io.IOException;
4
+ import java.util.*;
5
+
6
+ import com.fasterxml.jackson.core.*;
7
+
8
+ import org.jruby.Ruby;
9
+
10
+ import org.jruby.RubyObject;
11
+
12
+ public class RubyObjectStrDeserializer
13
+ extends RubyObjectDeserializer
14
+ {
15
+
16
+ public final static RubyObjectStrDeserializer instance = new RubyObjectStrDeserializer();
17
+
18
+ public RubyObjectStrDeserializer() { super(); }
19
+
20
+ protected RubyObject convertKey(JsonParser jp) throws IOException {
21
+ return RubyUtils.rubyString(Ruby.getGlobalRuntime(), jp.getText());
22
+ }
23
+ }
@@ -5,76 +5,19 @@ import java.util.*;
5
5
 
6
6
  import com.fasterxml.jackson.core.*;
7
7
 
8
- import com.fasterxml.jackson.databind.DeserializationContext;
9
- import com.fasterxml.jackson.databind.DeserializationFeature;
10
- import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
11
- import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
12
- import com.fasterxml.jackson.databind.util.ObjectBuffer;
13
- import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
14
-
15
8
  import org.jruby.Ruby;
16
- import org.jruby.RubyObject;
17
- import org.jruby.RubyArray;
18
- import org.jruby.RubyHash;
19
- import org.jruby.RubyString;
20
- import org.jruby.RubySymbol;
21
- import org.jruby.runtime.builtin.IRubyObject;
22
- import org.jruby.javasupport.JavaUtil;
23
- import org.jruby.javasupport.util.RuntimeHelpers;
24
9
 
10
+ import org.jruby.RubyObject;
25
11
 
26
12
  public class RubyObjectSymDeserializer
27
- // extends RubyObjectDeserializer<RubyObject>
28
13
  extends RubyObjectDeserializer
29
14
  {
30
15
 
31
- protected final static Ruby __ruby__ = Ruby.getGlobalRuntime();
32
-
33
16
  public final static RubyObjectSymDeserializer instance = new RubyObjectSymDeserializer();
34
17
 
35
18
  public RubyObjectSymDeserializer() { super(); }
36
19
 
37
- /*
38
- /**********************************************************
39
- /* Internal methods
40
- /**********************************************************
41
- */
42
-
43
- protected RubySymbol rubySymbol(JsonParser jp) throws IOException {
44
- return __ruby__.newSymbol(jp.getText());
45
- }
46
-
47
- protected RubyObject mapObject(JsonParser jp, DeserializationContext ctxt)
48
- throws IOException, JsonProcessingException
49
- {
50
- JsonToken t = jp.getCurrentToken();
51
- if (t == JsonToken.START_OBJECT) {
52
- t = jp.nextToken();
53
- }
54
- // 1.6: minor optimization; let's handle 1 and 2 entry cases separately
55
- if (t != JsonToken.FIELD_NAME) { // and empty one too
56
- // empty map might work; but caller may want to modify... so better just give small modifiable
57
- return RubyHash.newHash(__ruby__);
58
- }
59
- RubySymbol field1 = rubySymbol(jp);
60
- jp.nextToken();
61
- RubyObject value1 = deserialize(jp, ctxt);
62
- if (jp.nextToken() != JsonToken.FIELD_NAME) { // single entry; but we want modifiable
63
- return RuntimeHelpers.constructHash(__ruby__, field1, value1);
64
- }
65
- RubySymbol field2 = rubySymbol(jp);
66
- jp.nextToken();
67
- RubyObject value2 = deserialize(jp, ctxt);
68
- if (jp.nextToken() != JsonToken.FIELD_NAME) {
69
- return RuntimeHelpers.constructHash(__ruby__, field1, value1, field2, value2);
70
- }
71
- RubyHash result = RuntimeHelpers.constructHash(__ruby__, field1, value1, field2, value2);
72
- do {
73
- RubySymbol fieldName = rubySymbol(jp);
74
- jp.nextToken();
75
- result.fastASet(fieldName, deserialize(jp, ctxt));
76
- } while (jp.nextToken() != JsonToken.END_OBJECT);
77
- return result;
20
+ protected RubyObject convertKey(JsonParser jp) throws IOException {
21
+ return RubyUtils.rubySymbol(Ruby.getGlobalRuntime(), jp.getText());
78
22
  }
79
23
  }
80
-
@@ -0,0 +1,77 @@
1
+ package com.jrjackson;
2
+
3
+ import java.io.IOException;
4
+ import java.util.*;
5
+
6
+ import java.math.BigDecimal;
7
+ import java.math.BigInteger;
8
+
9
+ import org.jruby.*;
10
+ import org.jruby.javasupport.JavaUtil;
11
+ import org.jruby.runtime.builtin.IRubyObject;
12
+ import org.jruby.ext.bigdecimal.RubyBigDecimal;
13
+ import org.jruby.runtime.Block;
14
+
15
+ public class RubyUtils
16
+ {
17
+
18
+ public static RubyObject rubyObject(Ruby ruby, Object node)
19
+ {
20
+ return (RubyObject)JavaUtil.convertJavaToRuby(ruby, node);
21
+ }
22
+
23
+ public static RubyString rubyString(Ruby ruby, String node)
24
+ {
25
+ return ruby.newString(node);
26
+ }
27
+
28
+ public static RubySymbol rubySymbol(Ruby ruby, String node)
29
+ {
30
+ return RubySymbol.newSymbol(ruby, node);
31
+ }
32
+
33
+ public static RubyArray rubyArray(Ruby ruby, Object[] arg)
34
+ {
35
+ return (RubyArray)JavaUtil.convertJavaToRuby(ruby, arg);
36
+ }
37
+
38
+ public static RubyArray rubyArray(Ruby ruby, List arg)
39
+ {
40
+ return (RubyArray)JavaUtil.convertJavaToRuby(ruby, arg);
41
+ }
42
+
43
+ public static RubyHash rubyHash(Ruby ruby, Map arg)
44
+ {
45
+ return (RubyHash)JavaUtil.convertJavaToRuby(ruby, arg);
46
+ }
47
+
48
+ public static RubyFixnum rubyFixnum(Ruby ruby, int arg)
49
+ {
50
+ return ruby.newFixnum(arg);
51
+ }
52
+
53
+ public static RubyFixnum rubyFixnum(Ruby ruby, long arg)
54
+ {
55
+ return ruby.newFixnum(arg);
56
+ }
57
+
58
+ public static RubyBignum rubyBignum(Ruby ruby, BigInteger arg)
59
+ {
60
+ return RubyBignum.newBignum(ruby, arg);
61
+ }
62
+
63
+ public static RubyFloat rubyFloat(Ruby ruby, double arg)
64
+ {
65
+ return ruby.newFloat(arg);
66
+ }
67
+
68
+ public static RubyBigDecimal rubyBigDecimal(Ruby ruby, BigDecimal arg)
69
+ {
70
+ return new RubyBigDecimal(ruby, arg);
71
+ }
72
+
73
+ public static RubyBoolean rubyBoolean(Ruby ruby, Boolean arg)
74
+ {
75
+ return ruby.newBoolean(arg);
76
+ }
77
+ }
@@ -0,0 +1,38 @@
1
+ package com.jrjackson.jruby;
2
+
3
+ import junit.framework.Test;
4
+ import junit.framework.TestCase;
5
+ import junit.framework.TestSuite;
6
+
7
+ /**
8
+ * Unit test for simple App.
9
+ */
10
+ public class AppTest
11
+ extends TestCase
12
+ {
13
+ /**
14
+ * Create the test case
15
+ *
16
+ * @param testName name of the test case
17
+ */
18
+ public AppTest( String testName )
19
+ {
20
+ super( testName );
21
+ }
22
+
23
+ /**
24
+ * @return the suite of tests being tested
25
+ */
26
+ public static Test suite()
27
+ {
28
+ return new TestSuite( AppTest.class );
29
+ }
30
+
31
+ /**
32
+ * Rigourous Test :-)
33
+ */
34
+ public void testApp()
35
+ {
36
+ assertTrue( true );
37
+ }
38
+ }
@@ -0,0 +1,63 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'test/unit'
4
+ require 'thread'
5
+ require 'bigdecimal'
6
+ require 'jrjackson'
7
+
8
+ class JrJacksonTest < Test::Unit::TestCase
9
+
10
+ def test_threading
11
+ q1, q2, q3 = Queue.new, Queue.new, Queue.new
12
+
13
+ s1 = %Q|{"a":2.5, "b":0.214, "c":3.4567}|
14
+ th1 = Thread.new(s1) do |string|
15
+ q1 << JrJackson::Json.load(string, {use_bigdecimal: true, raw: true})
16
+ end
17
+ th2 = Thread.new(s1) do |string|
18
+ q2 << JrJackson::Json.load(string, {use_bigdecimal: true, symbolize_keys: true})
19
+ end
20
+ th3 = Thread.new(s1) do |string|
21
+ q3 << JrJackson::Json.load(string, {use_bigdecimal: false, symbolize_keys: true})
22
+ end
23
+ a1, a2, a3 = q1.pop, q2.pop, q3.pop
24
+ assert_equal ["a", "b", "c"], a1.keys
25
+ assert a1.values.all? {|v| v.is_a?(Java::JavaMath::BigDecimal)}, "Expected all values to be Java::JavaMath::BigDecimal"
26
+ assert_equal [:a, :b, :c], a2.keys
27
+ assert a2.values.all? {|v| v.is_a?(BigDecimal)}, "Expected all values to be BigDecimal"
28
+ assert a3.values.all? {|v| v.is_a?(Float)}, "Expected all values to be Float"
29
+ end
30
+
31
+ def test_serialize_symbols_as_values
32
+ source = {"first" => :first_symbol, "second" => {"inner" => :inner_symbol}}
33
+ expected = {:first => "first_symbol", :second => {:inner => "inner_symbol"}}
34
+ actual = JrJackson::Json.load(JrJackson::Json.dump(source), :symbolize_keys => true)
35
+ assert_equal expected, actual
36
+ end
37
+
38
+ def test_can_parse_big_decimals
39
+ expected = BigDecimal.new '0.12345678901234567890123456789'
40
+ json = '{"foo":0.12345678901234567890123456789}'
41
+
42
+ actual = JrJackson::Json.parse(json, :use_bigdecimal => true)['foo']
43
+ assert_bigdecimal_equal expected, actual
44
+
45
+ actual = JrJackson::Json.parse(json, :use_bigdecimal => true, :symbolize_keys => true)[:foo]
46
+ assert_bigdecimal_equal expected, actual
47
+
48
+ actual = JrJackson::Json.parse(json, :use_bigdecimal => true, :raw => true)['foo']
49
+ assert_bigdecimal_similar expected, actual
50
+ end
51
+
52
+ def assert_bigdecimal_equal(expected, actual)
53
+ assert_equal expected, actual
54
+ assert_equal expected.class, actual.class
55
+ assert_equal BigDecimal, actual.class
56
+ end
57
+
58
+ def assert_bigdecimal_similar(expected, actual)
59
+ assert_equal BigDecimal.new(expected.to_s), BigDecimal.new(actual.to_s)
60
+ assert_equal Java::JavaMath::BigDecimal, actual.class
61
+ end
62
+
63
+ end
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.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-03 00:00:00.000000000 Z
12
+ date: 2013-09-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  version_requirements: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ~>
19
19
  - !ruby/object:Gem::Version
20
20
  version: '1.0'
21
21
  none: false
22
22
  requirement: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
27
  none: false
@@ -34,14 +34,17 @@ executables: []
34
34
  extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
+ - .jrubyrc
37
38
  - Gemfile
38
39
  - README.md
39
40
  - Rakefile
40
41
  - benchmarking/.jrubyrc
41
42
  - benchmarking/benchmark.rb
43
+ - benchmarking/benchmark_threaded.rb
44
+ - dependency-reduced-pom.xml
42
45
  - jrjackson.gemspec
43
46
  - lib/jrjackson.rb
44
- - lib/jrjackson/jars/jrjackson-1.0.jar
47
+ - lib/jrjackson/jars/jrjackson-1.2.2.jar
45
48
  - lib/jrjackson/jrjackson.rb
46
49
  - lib/jrjackson/version.rb
47
50
  - lib/require_relative_patch.rb
@@ -49,11 +52,14 @@ files:
49
52
  - profiling/profiled.rb
50
53
  - src/main/java/com/jrjackson/JrJacksonRaw.java
51
54
  - src/main/java/com/jrjackson/JrJacksonService.java
52
- - src/main/java/com/jrjackson/JrJacksonStr.java
53
- - src/main/java/com/jrjackson/JrJacksonSym.java
54
55
  - src/main/java/com/jrjackson/ParseError.java
56
+ - src/main/java/com/jrjackson/RubyJacksonModule.java
55
57
  - src/main/java/com/jrjackson/RubyObjectDeserializer.java
58
+ - src/main/java/com/jrjackson/RubyObjectStrDeserializer.java
56
59
  - src/main/java/com/jrjackson/RubyObjectSymDeserializer.java
60
+ - src/main/java/com/jrjackson/RubyUtils.java
61
+ - src/test/java/com/jrjackson/jruby/AppTest.java
62
+ - test/jrjackson_test.rb
57
63
  homepage: http://github.com/guyboertje/jrjackson
58
64
  licenses: []
59
65
  post_install_message:
@@ -62,17 +68,15 @@ require_paths:
62
68
  - lib
63
69
  required_ruby_version: !ruby/object:Gem::Requirement
64
70
  requirements:
65
- - - ">="
71
+ - - '>='
66
72
  - !ruby/object:Gem::Version
67
- version: !binary |-
68
- MA==
73
+ version: '0'
69
74
  none: false
70
75
  required_rubygems_version: !ruby/object:Gem::Requirement
71
76
  requirements:
72
- - - ">="
77
+ - - '>='
73
78
  - !ruby/object:Gem::Version
74
- version: !binary |-
75
- MA==
79
+ version: '0'
76
80
  none: false
77
81
  requirements: []
78
82
  rubyforge_project:
Binary file
@@ -1,80 +0,0 @@
1
- package com.jrjackson;
2
-
3
- import org.jruby.Ruby;
4
- import org.jruby.RubyClass;
5
- import org.jruby.RubyObject;
6
- import org.jruby.RubyString;
7
- import org.jruby.RubyIO;
8
- import org.jruby.anno.JRubyMethod;
9
- import org.jruby.anno.JRubyModule;
10
- import org.jruby.exceptions.RaiseException;
11
- import org.jruby.ext.stringio.RubyStringIO;
12
- import org.jruby.java.addons.IOJavaAddons;
13
- import org.jruby.javasupport.JavaUtil;
14
- import org.jruby.runtime.ThreadContext;
15
- import org.jruby.runtime.builtin.IRubyObject;
16
- import org.jruby.util.RubyDateFormat;
17
-
18
- import java.io.InputStream;
19
- import java.io.IOException;
20
- import java.text.DateFormat;
21
- import java.util.*;
22
-
23
- import com.fasterxml.jackson.databind.ObjectMapper;
24
- import com.fasterxml.jackson.databind.module.SimpleModule;
25
- import com.fasterxml.jackson.core.Version;
26
- import com.fasterxml.jackson.core.JsonProcessingException;
27
-
28
- @JRubyModule(name = "JrJacksonStr")
29
- public class JrJacksonStr extends RubyObject {
30
- private static final ObjectMapper mapper = new ObjectMapper();
31
- private static final SimpleModule module = new SimpleModule("JrJacksonStrModule");
32
-
33
- static {
34
- mapper.registerModule(module.addDeserializer(Object.class, RubyObjectDeserializer.instance));
35
- mapper.setDateFormat(new RubyDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US, true));
36
- }
37
-
38
- public JrJacksonStr(Ruby ruby, RubyClass metaclass) {
39
- super(ruby, metaclass);
40
- }
41
-
42
- @JRubyMethod(module = true, name = {"parse", "load"}, required = 1)
43
- public static IRubyObject parse(ThreadContext context, IRubyObject self, IRubyObject arg) {
44
- Ruby ruby = context.getRuntime();
45
- try {
46
- Object o;
47
- if (arg instanceof RubyString) {
48
- o = mapper.readValue(arg.toString(), Object.class);
49
- } else if ((arg instanceof RubyIO) || (arg instanceof RubyStringIO)) {
50
- IRubyObject stream = IOJavaAddons.AnyIO.any_to_inputstream(context, arg);
51
- o = mapper.readValue((InputStream)stream.toJava(InputStream.class), Object.class);
52
- } else {
53
- throw ruby.newArgumentError("Unsupported source. This method accepts String or IO");
54
- }
55
- return (RubyObject)JavaUtil.convertJavaToRuby(ruby, o);
56
- }
57
- catch (JsonProcessingException e) {
58
- throw ParseError.newParseError(ruby, e.getLocalizedMessage());
59
- }
60
- catch (IOException e) {
61
- throw ruby.newIOError(e.getLocalizedMessage());
62
- }
63
- }
64
-
65
- @JRubyMethod(module = true, name = {"generate", "dump"}, required = 1)
66
- public static IRubyObject generate(ThreadContext context, IRubyObject self, IRubyObject arg) {
67
- Ruby ruby = context.getRuntime();
68
- Object obj = arg.toJava(Object.class);
69
- try {
70
- String s = mapper.writeValueAsString(obj);
71
- return ruby.newString(s);
72
- }
73
- catch (JsonProcessingException e) {
74
- throw ParseError.newParseError(ruby, e.getLocalizedMessage());
75
- }
76
- catch (IOException e) {
77
- throw ruby.newIOError(e.getLocalizedMessage());
78
- }
79
- }
80
- }