jrjackson 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
1
3
  require 'rubygems'
2
4
  require 'benchmark'
5
+ require 'thread'
3
6
  require 'digest'
4
7
  require 'json'
5
- require 'lib/jrjackson'
8
+ require 'gson'
9
+
10
+ require File.expand_path('lib/jrjackson')
6
11
 
7
12
  HASH = {:one => nil, :two => nil, :three => nil, :four => {:a => nil, :b => nil, :c =>nil},
8
13
  :five => {:d => nil, :e => nil},
9
14
  :six => {:f => nil, :g => nil, :h =>nil, :i => nil, :j => nil, :k => nil, :l => nil},
10
- :seven => nil, :eight => nil,
15
+ :seven => nil, :eight => [],
11
16
  :nine => {:m => {:A => nil, :B => nil}}}
12
17
 
13
18
  def random_string
@@ -22,6 +27,14 @@ def random_float
22
27
  random_number + rand
23
28
  end
24
29
 
30
+ def fill_array
31
+ value = []
32
+ 5.times do
33
+ value.push send(METHODS[rand(3)])
34
+ end
35
+ value
36
+ end
37
+
25
38
  def randomize_entries hsh
26
39
  new_hsh = {}
27
40
  hsh.each_pair do |key, value|
@@ -30,6 +43,8 @@ def randomize_entries hsh
30
43
  new_hsh[key] = send METHODS[rand(3)]
31
44
  when Hash
32
45
  new_hsh[key] = randomize_entries value
46
+ when Array
47
+ new_hsh[key] = fill_array
33
48
  end
34
49
  end
35
50
  new_hsh
@@ -39,44 +54,101 @@ METHODS = [:random_string, :random_number, :random_float]
39
54
 
40
55
  org_array = []
41
56
  one = []
42
- #
43
- 0.upto(50000) do |i|
57
+
58
+ 0.upto(5000) do |i|
44
59
  hsh = HASH.dup
45
60
  org_array << randomize_entries(hsh)
46
61
  end
47
62
 
48
63
  generated_array = []
49
64
  generated_smile = []
50
-
65
+ q = Queue.new
51
66
 
52
67
  org_array.each do |hsh|
53
- generated_array << JrJackson::Json.generate(hsh)
54
- generated_smile << JrJackson::Smile.generate(hsh)
68
+ generated_array << JrJackson::Raw.generate(hsh)
55
69
  end
56
70
 
57
- Benchmark.bmbm("jackson generate: plus some margin".size) do |x|
71
+ Benchmark.bmbm("jackson parse symbol keys: ".size) do |x|
58
72
 
59
- x.report("jackson generate:") do
60
- org_array.each {|hsh| JrJackson::Json.generate(hsh) }
73
+ x.report("json java parse:") do
74
+ th1 = Thread.new(generated_array) do |arry|
75
+ 50.times {arry.each {|string| ::JSON.parse("[#{string}]").first }}
76
+ q << true
77
+ end
78
+ th2 = Thread.new(generated_array) do |arry|
79
+ 50.times {arry.each {|string| ::JSON.parse("[#{string}]").first }}
80
+ q << true
81
+ end
82
+ q.pop
83
+ q.pop
84
+ # 50.times {generated_array.each {|string| ::JSON.parse("[#{string}]").first }}
61
85
  end
62
86
 
63
- x.report("smile generate:") do
64
- org_array.each {|hsh| JrJackson::Smile.generate(hsh) }
87
+ x.report("gson parse:") do
88
+ th1 = Thread.new(generated_array) do |arry|
89
+ 50.times {arry.each {|string| ::Gson::Decoder.new({}).decode(string) }}
90
+ q << true
91
+ end
92
+ th2 = Thread.new(generated_array) do |arry|
93
+ 50.times {arry.each {|string| ::Gson::Decoder.new({}).decode(string) }}
94
+ q << true
95
+ end
96
+ q.pop
97
+ q.pop
98
+ # 50.times {generated_array.each {|string| ::Gson::Decoder.new({}).decode(string) }}
65
99
  end
66
100
 
67
- x.report("ruby generate:") do
68
- org_array.each {|hsh| JSON.fast_generate(hsh) }
101
+ x.report("jackson parse raw:") do
102
+ th1 = Thread.new(generated_array) do |arry|
103
+ 50.times {arry.each {|string| JrJackson::Raw.parse(string) }}
104
+ q << true
105
+ end
106
+ th2 = Thread.new(generated_array) do |arry|
107
+ 50.times {arry.each {|string| JrJackson::Raw.parse(string) }}
108
+ q << true
109
+ end
110
+ q.pop
111
+ q.pop
112
+ # 50.times {generated_array.each {|string| JrJackson::Raw.parse(string) }}
69
113
  end
70
114
 
71
- x.report("jackson parse:") do
72
- generated_array.each {|string| JrJackson::Json.parse(string) }
115
+ x.report("jackson parse symbol keys:") do
116
+ th1 = Thread.new(generated_array) do |arry|
117
+ 50.times {arry.each {|string| JrJackson::Sym.parse(string) }}
118
+ q << true
119
+ end
120
+ th2 = Thread.new(generated_array) do |arry|
121
+ 50.times {arry.each {|string| JrJackson::Sym.parse(string) }}
122
+ q << true
123
+ end
124
+ q.pop
125
+ q.pop
126
+ # 50.times {generated_array.each {|string| JrJackson::Sym.parse(string) }}
73
127
  end
74
128
 
75
- x.report("smile parse:") do
76
- generated_smile.each {|string| JrJackson::Smile.parse(string) }
129
+ x.report("jackson parse string keys:") do
130
+ th1 = Thread.new(generated_array) do |arry|
131
+ 50.times {arry.each {|string| JrJackson::Str.parse(string) }}
132
+ q << true
133
+ end
134
+ th2 = Thread.new(generated_array) do |arry|
135
+ 50.times {arry.each {|string| JrJackson::Str.parse(string) }}
136
+ q << true
137
+ end
138
+ q.pop
139
+ q.pop
140
+ # 50.times {generated_array.each {|string| JrJackson::Str.parse(string) }}
77
141
  end
78
142
 
79
- x.report("ruby parse:") do
80
- generated_array.each {|string| JSON.parse(string) }
143
+ x.report("json java generate:") do
144
+ 50.times {org_array.each {|hsh| hsh.to_json }}
145
+ end
146
+
147
+ x.report("gson generate:") do
148
+ 50.times {org_array.each {|hsh| ::Gson::Encoder.new({}).encode(hsh) }}
149
+ end
150
+
151
+ x.report("jackson generate:") do
152
+ 50.times {org_array.each {|hsh| JrJackson::Raw.generate(hsh) }}
81
153
  end
82
154
  end
data/jrjackson.gemspec CHANGED
@@ -2,39 +2,40 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'jrjackson'
5
- s.version = '0.0.7'
6
- s.date = '2012-03-14'
5
+ s.version = '0.1.0'
6
+ s.date = '2013-03-31'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ['Guy Boertje']
9
9
  s.email = ['guyboertje@gmail.com']
10
10
  s.homepage = "http://github.com/guyboertje/jrjackson"
11
11
  s.summary = %q{A JRuby wrapper for the java jackson json processor jar}
12
- s.description = %q{}
12
+ s.description = %q{A mostly native JRuby wrapper for the java jackson json processor jar}
13
+
14
+ s.add_development_dependency 'bundler', '~> 1.0'
13
15
 
14
16
  # = MANIFEST =
15
17
  s.files = %w[
16
18
  Gemfile
17
- README
19
+ README.md
18
20
  Rakefile
21
+ benchmarking/.jrubyrc
19
22
  benchmarking/benchmark.rb
20
23
  jrjackson.gemspec
21
24
  lib/jrjackson.rb
22
- lib/jrjackson/jackson-core-asl-1.9.5.jar
23
- lib/jrjackson/jackson-mapper-asl-1.9.5.jar
24
- lib/jrjackson/jackson-smile-1.9.5.jar
25
+ lib/jrjackson/jars/jrjackson-1.0.jar
25
26
  lib/jrjackson/jrjackson.rb
26
- lib/jrjackson/rubify.rb
27
- lib/jrjackson/rubify_with_symbol_keys.rb
28
27
  lib/jrjackson/version.rb
29
- lib/jrjackson_r.rb
30
- lib/jrjackson_r_sym.rb
28
+ lib/require_relative_patch.rb
29
+ pom.xml
31
30
  profiling/profiled.rb
31
+ src/main/java/com/jrjackson/JrJacksonRaw.java
32
+ src/main/java/com/jrjackson/JrJacksonService.java
33
+ src/main/java/com/jrjackson/JrJacksonStr.java
34
+ src/main/java/com/jrjackson/JrJacksonSym.java
35
+ src/main/java/com/jrjackson/ParseError.java
36
+ src/main/java/com/jrjackson/RubyObjectDeserializer.java
37
+ src/main/java/com/jrjackson/RubyObjectSymDeserializer.java
32
38
  ]
33
39
  # = MANIFEST =
34
40
 
35
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
36
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
37
-
38
- s.add_development_dependency 'awesome_print', '~> 0.4.0'
39
-
40
41
  end
data/lib/jrjackson.rb CHANGED
@@ -1,10 +1,2 @@
1
- unless RUBY_PLATFORM =~ /java/
2
- error "This library is only compatible with a java-based ruby environment like JRuby."
3
- exit 255
4
- end
5
-
6
- require 'java'
7
-
8
- $CLASSPATH << 'jrjackson'
9
-
10
- require File.join("jrjackson","jrjackson")
1
+ require "require_relative_patch" unless Kernel.respond_to?(:require_relative)
2
+ require_relative "jrjackson/jrjackson"
@@ -1,41 +1,51 @@
1
- %W(jackson-core-asl-1.9.5.jar jackson-mapper-asl-1.9.5.jar jackson-smile-1.9.5.jar).each {|f| require File.join("jrjackson",f)}
1
+ unless RUBY_PLATFORM =~ /java/
2
+ puts "This library is only compatible with a java-based ruby environment like JRuby."
3
+ exit 255
4
+ end
2
5
 
3
- module JrJackson
4
- include_package "org.codehaus.jackson"
5
- include_package "org.codehaus.jackson.map"
6
- include_package "org.codehaus.jackson.smile"
6
+ require_relative "jars/jrjackson-1.0.jar"
7
+ require 'com/jrjackson/jr_jackson'
7
8
 
8
- Jclass = java.lang.Object.java_class
9
+ module JrJackson
10
+ module Json
11
+ class << self
12
+ TIME_REGEX = %r(\A\d{4}-\d\d-\d\d\s\d\d:\d\d:\d\d)
13
+ def load(json_string, options = {})
14
+ mod = if options[:raw]
15
+ JrJackson::Raw
16
+ elsif options[:symbolize_keys]
17
+ JrJackson::Sym
18
+ else
19
+ JrJackson::Str
20
+ end
21
+ if json_string.is_a?(String) && json_string =~ TIME_REGEX
22
+ mod.parse("\"#{json_string}\"")
23
+ else
24
+ mod.parse(json_string)
25
+ end
26
+ end
9
27
 
10
- Mapper = ObjectMapper.new
11
- SmileMapper = ObjectMapper.new(SmileFactory.new)
28
+ def dump(object)
29
+ case object
30
+ when Array, Hash
31
+ JrJackson::Raw.generate(object)
32
+ when String
33
+ "\"#{object}\""
34
+ when nil, true, false
35
+ object
36
+ else
37
+ if object.respond_to?(:to_json)
38
+ object.to_json
39
+ elsif object.respond_to?(:to_s)
40
+ object.to_s
41
+ else
42
+ object
43
+ end
44
+ end
45
+ end
12
46
 
13
- module Json
14
- def self.parse(json_string)
15
- Mapper.read_value json_string, Jclass
16
- end
17
- def self.generate(object)
18
- Mapper.writeValueAsString(object)
19
- end
20
- def self.load(json_string)
21
- Mapper.read_value json_string, Jclass
22
- end
23
- def self.dump(object)
24
- Mapper.writeValueAsString(object)
25
- end
26
- end
27
- module Smile
28
- def self.parse(smile_bytes)
29
- SmileMapper.read_value smile_bytes,0,smile_bytes.size, Jclass
30
- end
31
- def self.generate(object)
32
- SmileMapper.writeValueAsBytes(object)
33
- end
34
- def self.load(smile_bytes)
35
- SmileMapper.read_value smile_bytes,0,smile_bytes.size, Jclass
36
- end
37
- def self.dump(object)
38
- SmileMapper.writeValueAsBytes(object)
47
+ alias :parse :load
48
+ alias :generate :dump
39
49
  end
40
50
  end
41
51
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module JrJackson
3
- VERSION = '0.0.7'
3
+ VERSION = '0.1.0'
4
4
  end
@@ -0,0 +1,6 @@
1
+ module Kernel
2
+ def require_relative(path)
3
+ require File.join(File.dirname(caller[0]), path.to_str)
4
+ end
5
+ end
6
+
data/pom.xml ADDED
@@ -0,0 +1,86 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
+ <modelVersion>4.0.0</modelVersion>
4
+
5
+ <groupId>com.jrjackson.jruby</groupId>
6
+ <artifactId>jrjackson</artifactId>
7
+ <packaging>jar</packaging>
8
+ <version>1.0</version>
9
+ <name>jrjackson</name>
10
+ <url>http://maven.apache.org</url>
11
+
12
+ <properties>
13
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14
+ </properties>
15
+
16
+ <dependencies>
17
+ <dependency>
18
+ <groupId>junit</groupId>
19
+ <artifactId>junit</artifactId>
20
+ <version>4.11</version>
21
+ <scope>test</scope>
22
+ </dependency>
23
+ <dependency>
24
+ <groupId>org.jruby</groupId>
25
+ <artifactId>jruby</artifactId>
26
+ <version>1.7.3</version>
27
+ </dependency>
28
+ <dependency>
29
+ <groupId>com.fasterxml.jackson.core</groupId>
30
+ <artifactId>jackson-core</artifactId>
31
+ <version>2.1.4</version>
32
+ </dependency>
33
+ <dependency>
34
+ <groupId>com.fasterxml.jackson.core</groupId>
35
+ <artifactId>jackson-annotations</artifactId>
36
+ <version>2.1.4</version>
37
+ </dependency>
38
+ <dependency>
39
+ <groupId>com.fasterxml.jackson.core</groupId>
40
+ <artifactId>jackson-databind</artifactId>
41
+ <version>2.1.4</version>
42
+ </dependency>
43
+ </dependencies>
44
+ <build>
45
+ <plugins>
46
+ <plugin>
47
+ <groupId>org.apache.maven.plugins</groupId>
48
+ <artifactId>maven-compiler-plugin</artifactId>
49
+ <version>3.0</version>
50
+ <configuration>
51
+ <source>1.6</source>
52
+ <target>1.6</target>
53
+ </configuration>
54
+ </plugin>
55
+ <plugin>
56
+ <groupId>org.apache.maven.plugins</groupId>
57
+ <artifactId>maven-surefire-plugin</artifactId>
58
+ <version>2.12.2</version>
59
+ <configuration>
60
+ <parallel>methods</parallel>
61
+ <threadCount>10</threadCount>
62
+ </configuration>
63
+ </plugin>
64
+ <plugin>
65
+ <groupId>org.apache.maven.plugins</groupId>
66
+ <artifactId>maven-shade-plugin</artifactId>
67
+ <version>1.6</version>
68
+ <executions>
69
+ <execution>
70
+ <phase>package</phase>
71
+ <goals>
72
+ <goal>shade</goal>
73
+ </goals>
74
+ <configuration>
75
+ <artifactSet>
76
+ <excludes>
77
+ <exclude>org.jruby:jruby</exclude>
78
+ </excludes>
79
+ </artifactSet>
80
+ </configuration>
81
+ </execution>
82
+ </executions>
83
+ </plugin>
84
+ </plugins>
85
+ </build>
86
+ </project>
@@ -0,0 +1,76 @@
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.core.JsonProcessingException;
25
+
26
+ @JRubyModule(name = "JrJacksonRaw")
27
+ public class JrJacksonRaw extends RubyObject {
28
+ protected static final ObjectMapper mapper = new ObjectMapper();
29
+
30
+ static {
31
+ mapper.setDateFormat(new RubyDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US, true));
32
+ }
33
+
34
+ public JrJacksonRaw(Ruby ruby, RubyClass metaclass) {
35
+ super(ruby, metaclass);
36
+ }
37
+
38
+ @JRubyMethod(module = true, name = {"parse", "load"}, required = 1)
39
+ public static IRubyObject parse(ThreadContext context, IRubyObject self, IRubyObject arg) {
40
+ Ruby ruby = context.getRuntime();
41
+ try {
42
+ Object o;
43
+ if (arg instanceof RubyString) {
44
+ o = mapper.readValue(arg.toString(), Object.class);
45
+ } else if ((arg instanceof RubyIO) || (arg instanceof RubyStringIO)) {
46
+ IRubyObject stream = IOJavaAddons.AnyIO.any_to_inputstream(context, arg);
47
+ o = mapper.readValue((InputStream)stream.toJava(InputStream.class), Object.class);
48
+ } else {
49
+ throw ruby.newArgumentError("Unsupported source. This method accepts String or IO");
50
+ }
51
+ return (RubyObject)JavaUtil.convertJavaToRuby(ruby, o);
52
+ }
53
+ catch (JsonProcessingException e) {
54
+ throw ParseError.newParseError(ruby, e.getLocalizedMessage());
55
+ }
56
+ catch (IOException e) {
57
+ throw ruby.newIOError(e.getLocalizedMessage());
58
+ }
59
+ }
60
+
61
+ @JRubyMethod(module = true, name = {"generate", "dump"}, required = 1)
62
+ public static IRubyObject generate(ThreadContext context, IRubyObject self, IRubyObject arg) {
63
+ Ruby ruby = context.getRuntime();
64
+ Object obj = arg.toJava(Object.class);
65
+ try {
66
+ String s = mapper.writeValueAsString(obj);
67
+ return ruby.newString(s);
68
+ }
69
+ catch (JsonProcessingException e) {
70
+ throw ParseError.newParseError(ruby, e.getLocalizedMessage());
71
+ }
72
+ catch (IOException e) {
73
+ throw ruby.newIOError(e.getLocalizedMessage());
74
+ }
75
+ }
76
+ }