Skip to content

Commit

Permalink
add reference to RubyAnySerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
guyboertje committed Nov 8, 2015
1 parent 5b65bfc commit bfafe3b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Expand Up @@ -4,7 +4,7 @@
<groupId>com.jrjackson.jruby</groupId>
<artifactId>jrjackson</artifactId>
<name>jrjackson</name>
<version>1.2.16</version>
<version>1.2.17</version>
<url>http://maven.apache.org</url>
<build>
<plugins>
Expand Down
4 changes: 2 additions & 2 deletions lib/jrjackson/build_info.rb
@@ -1,11 +1,11 @@
module JrJackson
module BuildInfo
def self.version
'0.3.6'
'0.3.7'
end

def self.release_date
'2015-10-19'
'2015-11-09'
end

def self.files
Expand Down
4 changes: 2 additions & 2 deletions lib/jrjackson/jrjackson.rb
Expand Up @@ -3,8 +3,8 @@
exit 255
end

require_relative "jars/jrjackson-1.2.16.jar"
# require_relative "linked/jrjackson-1.2.16.jar"
require_relative "jars/jrjackson-1.2.17.jar"
# require_relative "linked/jrjackson-1.2.17.jar"

require 'com/jrjackson/jr_jackson'
require 'bigdecimal'
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -5,7 +5,7 @@
<groupId>com.jrjackson.jruby</groupId>
<artifactId>jrjackson</artifactId>
<packaging>jar</packaging>
<version>1.2.16</version>
<version>1.2.17</version>
<name>jrjackson</name>
<url>http://maven.apache.org</url>

Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/jrjackson/JrJacksonBase.java
Expand Up @@ -47,10 +47,6 @@ public static IRubyObject generate(ThreadContext context, IRubyObject self, IRub
RubyHash options = (args.length <= 1) ? RubyHash.newHash(_ruby) : args[1].convertToHash();
String format = (String) options.get(RubyUtils.rubySymbol(_ruby, "date_format"));

// StringWriter out = new StringWriter();
// JsonGenerator jgen = RubyJacksonModule.factory.createGenerator(out);


ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator jgen = RubyJacksonModule.factory.createGenerator(
baos, JsonEncoding.UTF8);
Expand Down Expand Up @@ -78,7 +74,6 @@ public static IRubyObject generate(ThreadContext context, IRubyObject self, IRub
ByteList bl = new ByteList(baos.toByteArray(),
UTF8Encoding.INSTANCE);
return RubyString.newString(_ruby, bl);
// return RubyUtils.rubyString(_ruby, out.toString());
} catch (JsonProcessingException e) {
throw ParseError.newParseError(_ruby, e.getLocalizedMessage());
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/jrjackson/RubyAnySerializer.java
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.jruby.ext.bigdecimal.RubyBigDecimal;
Expand All @@ -28,7 +29,7 @@
import org.jruby.RubySymbol;
import org.jruby.RubyTime;

public class RubyAnySerializer {
public class RubyAnySerializer extends JsonSerializer<IRubyObject> {

/**
* Singleton instance to use.""
Expand All @@ -43,7 +44,7 @@ public RubyAnySerializer() {
private void serializeUnknownRubyObject(ThreadContext ctx, IRubyObject rubyObject, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {
RubyClass meta = rubyObject.getMetaClass();

DynamicMethod method = meta.searchMethod("to_json_data");
if (!method.isUndefined()) {
RubyObject obj = (RubyObject) method.call(ctx, rubyObject, meta, "to_json_data");
Expand All @@ -54,7 +55,7 @@ private void serializeUnknownRubyObject(ThreadContext ctx, IRubyObject rubyObjec
}
return;
}

method = meta.searchMethod("to_time");
if (!method.isUndefined()) {
RubyTime dt = (RubyTime) method.call(ctx, rubyObject, meta, "to_time");
Expand Down Expand Up @@ -97,7 +98,7 @@ private void serializeUnknownRubyObject(ThreadContext ctx, IRubyObject rubyObjec
throw new JsonGenerationException("Cannot find Serializer for class: " + rubyObject.getClass().getName());
}

// @Override
@Override
public void serialize(IRubyObject value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {

Expand Down Expand Up @@ -226,6 +227,7 @@ private void serializeKey(IRubyObject key, JsonGenerator jgen, SerializerProvide
* @throws java.io.IOException
* @throws com.fasterxml.jackson.core.JsonGenerationException
*/
@Override
public void serializeWithType(IRubyObject value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer)
throws IOException, JsonGenerationException {
typeSer.writeTypePrefixForScalar(value, jgen);
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/jrjackson/RubyJacksonModule.java
Expand Up @@ -11,6 +11,7 @@
import java.text.SimpleDateFormat;

import org.jruby.Ruby;
import org.jruby.runtime.builtin.IRubyObject;

public class RubyJacksonModule extends SimpleModule {

Expand All @@ -23,7 +24,7 @@ public class RubyJacksonModule extends SimpleModule {
}

private RubyJacksonModule() {
super("JrJacksonStrModule", new Version(1, 2, 16, "0", "com.jrjackson.jruby", "jrjackson"));
super("JrJacksonStrModule", new Version(1, 2, 17, "0", "com.jrjackson.jruby", "jrjackson"));
}

public static ObjectMapper mapperWith(Ruby ruby, RubyKeyConverter nameConverter,
Expand All @@ -50,7 +51,12 @@ public static ObjectMapper rawMapper() {
}

public static DefaultSerializerProvider createProvider(SimpleDateFormat sdf) {
static_mapper.setDateFormat(sdf);
static_mapper.setDateFormat(sdf)
.registerModule(
new RubyJacksonModule().addSerializer(
IRubyObject.class, RubyAnySerializer.instance
)
);
return ((DefaultSerializerProvider)static_mapper.getSerializerProvider()).createInstance(
static_mapper.getSerializationConfig(),
static_mapper.getSerializerFactory()
Expand All @@ -63,5 +69,4 @@ public static ObjectMapper rawBigNumberMapper() {
return static_mapper;
}


}
11 changes: 11 additions & 0 deletions test/jrjackson_test.rb
Expand Up @@ -511,6 +511,17 @@ def test_can_serialise_regex_match_data
assert_equal "{\"matched\":\"bar\"}", actual
end

def test_can_mix_java_and_ruby_objects
json = '{"utf8":"żółć", "moo": "bar", "arr": [2,3,4], "flo": 3.33}'

expected = '{"mixed":{"arr":[2,3,4],"utf8":"żółć","flo":3.33,"zzz":{"one":1.0,"two":2,"six":6.0},"moo":"bar"}}'
object = JrJackson::Json.parse_java(json)
object["zzz"] = CustomToJson.new(1.0, 2, 6.0)
mixed = {"mixed" => object}
actual = JrJackson::Json.dump(mixed)
assert_equal expected, actual
end

# -----------------------------

def assert_bigdecimal_equal(expected, actual)
Expand Down

0 comments on commit bfafe3b

Please sign in to comment.