src/main/java/com/jrjackson/RubyObjectDeserializer.java in jrjackson-0.2.9 vs src/main/java/com/jrjackson/RubyObjectDeserializer.java in jrjackson-0.3.0

- old
+ new

@@ -3,49 +3,44 @@ import java.io.IOException; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.util.ObjectBuffer; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import org.jruby.Ruby; import org.jruby.RubyObject; import org.jruby.RubyArray; import org.jruby.RubyHash; -import org.jruby.javasupport.util.RuntimeHelpers; + public class RubyObjectDeserializer extends StdDeserializer<RubyObject> { private static final long serialVersionUID = 1L; private Ruby _ruby; - private RubyKeyConverter key_converter; - final private RubyStringConverter str_converter = new RubyStringConverter(); + private RubyKeyConverter _key_converter; + private RubyConverter _int_converter; + private RubyConverter _float_converter; + final private RubyStringConverter _str_converter = new RubyStringConverter(); public RubyObjectDeserializer() { super(RubyObject.class); } - public RubyObjectDeserializer withRuby(Ruby ruby) { + public RubyObjectDeserializer with(Ruby ruby, RubyKeyConverter nameConv, + RubyConverter intConv, RubyConverter floatConv) { _ruby = ruby; + _key_converter = nameConv; + _int_converter = intConv; + _float_converter = floatConv; return this; } - public RubyObjectDeserializer setStringStrategy() { - key_converter = str_converter; - return this; - } - - public RubyObjectDeserializer setSymbolStrategy() { - key_converter = new RubySymbolConverter(); - return this; - } - /** * /********************************************************** * /* Deserializer API /********************************************************** * @param jp * @param ctxt @@ -55,41 +50,35 @@ */ @Override public RubyObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + switch (jp.getCurrentToken()) { case START_OBJECT: return mapObject(jp, ctxt); case START_ARRAY: return mapArray(jp, ctxt); case FIELD_NAME: - return key_converter.convert(_ruby, jp); + return _key_converter.convert(_ruby, jp); case VALUE_EMBEDDED_OBJECT: return RubyUtils.rubyObject(_ruby, jp.getEmbeddedObject()); case VALUE_STRING: - return str_converter.convert(_ruby, jp); + return _str_converter.convert(_ruby, jp); case VALUE_NUMBER_INT: /* [JACKSON-100]: caller may want to get all integral values * returned as BigInteger, for consistency */ - JsonParser.NumberType numberType = jp.getNumberType(); - if (ctxt.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS) || numberType == JsonParser.NumberType.BIG_INTEGER) { - return RubyUtils.rubyBignum(_ruby, jp.getBigIntegerValue()); - } - return RubyUtils.rubyFixnum(_ruby, jp.getLongValue()); + return _int_converter.convert(_ruby, jp); case VALUE_NUMBER_FLOAT: - if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) { - return RubyUtils.rubyBigDecimal(_ruby, jp.getDecimalValue()); - } - return RubyUtils.rubyFloat(_ruby, jp.getDoubleValue()); + return _float_converter.convert(_ruby, jp); case VALUE_TRUE: return _ruby.newBoolean(Boolean.TRUE); case VALUE_FALSE: @@ -163,27 +152,29 @@ if (t != JsonToken.FIELD_NAME) { // and empty one too // empty map might work; but caller may want to modify... so better just give small modifiable return RubyHash.newHash(_ruby); } - RubyObject field1 = key_converter.convert(_ruby, jp); + RubyObject field1 = _key_converter.convert(_ruby, jp); jp.nextToken(); RubyObject value1 = deserialize(jp, ctxt); + if (jp.nextToken() != JsonToken.FIELD_NAME) { // single entry; but we want modifiable - return RuntimeHelpers.constructHash(_ruby, field1, value1); + return RubyUtils.rubyHash(_ruby, field1, value1); } - RubyObject field2 = key_converter.convert(_ruby, jp); + RubyObject field2 = _key_converter.convert(_ruby, jp); jp.nextToken(); - RubyObject value2 = deserialize(jp, ctxt); + + RubyHash result = RubyUtils.rubyHash(_ruby, field1, value1, field2, deserialize(jp, ctxt)); + if (jp.nextToken() != JsonToken.FIELD_NAME) { - return RuntimeHelpers.constructHash(_ruby, field1, value1, field2, value2); + return result; } // And then the general case; default map size is 16 - RubyHash result = RuntimeHelpers.constructHash(_ruby, field1, value1, field2, value2); do { - RubyObject fieldName = key_converter.convert(_ruby, jp); + RubyObject fieldName = _key_converter.convert(_ruby, jp); jp.nextToken(); result.fastASetCheckString(_ruby, fieldName, deserialize(jp, ctxt)); } while (jp.nextToken() != JsonToken.END_OBJECT); return result; }