Skip to content
Permalink
Browse files
Fix hessian2 serialized short, byte is converted to int bug(#20)
fixes #19
  • Loading branch information
chickenlj committed Mar 16, 2020
2 parents 6e78128 + 6538354 commit 73b4dd65f9c05023530d2f04fc0b7ae9fa2c0053
Showing 3 changed files with 53 additions and 6 deletions.
@@ -59,6 +59,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

/**
@@ -374,7 +375,11 @@ else if (Map.class.equals(type)
} else if (List.class.equals(type)
&& field.getGenericType() != field.getType()) {
deser = new ObjectListFieldDeserializer(field);
} else {
} else if (Set.class.equals(type)
&& field.getGenericType() != field.getType()) {
deser = new ObjectSetFieldDeserializer(field);
}
else {
deser = new ObjectFieldDeserializer(field);
}

@@ -531,6 +536,33 @@ void deserialize(AbstractHessianInput in, Object obj)
}
}

static class ObjectSetFieldDeserializer extends FieldDeserializer {
private final Field _field;

ObjectSetFieldDeserializer(Field field) {
_field = field;
}

@Override
void deserialize(AbstractHessianInput in, Object obj)
throws IOException {
Object value = null;

try {

Type[] types = ((ParameterizedType) _field.getGenericType()).getActualTypeArguments();
value = in.readObject(_field.getType(),
isPrimitive(types[0]) ? (Class<?>) types[0] : null
);

_field.set(obj, value);
} catch (Exception e) {
logDeserializeError(_field, obj, value, e);
}
}
}


static class IntFieldDeserializer extends FieldDeserializer {
private final Field _field;

@@ -24,11 +24,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
@@ -207,4 +203,20 @@ public void serialize_list_then_deserialize0() throws Exception {
assertEquals(Short.valueOf((short)0), deserialize.get(0));
assertEquals(Short.valueOf((short)60), deserialize.get(1));
}

@Test
public void serialize_short_set_then_deserialize() throws Exception {

Hessian2StringShortType stringShort = new Hessian2StringShortType();
Set<Short> shortSet = new HashSet<Short>();
shortSet.add((short) 0);
shortSet.add((short) 60);
stringShort.shortSet = shortSet;

Hessian2StringShortType deserialize = baseHessian2Serialize(stringShort);
assertTrue(deserialize.shortSet != null);
assertTrue(deserialize.shortSet.size() == 2);
assertTrue(deserialize.shortSet.contains((short) 0));
assertTrue(deserialize.shortSet.contains((short) 60));
}
}
@@ -18,12 +18,15 @@

import java.io.Serializable;
import java.util.Map;
import java.util.Set;

/**
* test short serialize & deserialize model
*/
public class Hessian2StringShortType implements Serializable {

public Set<Short> shortSet;

public Map<String, Short> stringShortMap;

public Map<String, Byte> stringByteMap;

0 comments on commit 73b4dd6

Please sign in to comment.