Skip to content
Permalink
Browse files
fix deserialize uuid
  • Loading branch information
阙沂平 committed Jun 4, 2020
1 parent aea62f0 commit b662d24a6b4f2d1cb466d02064dcfe04bec34cc2
Showing 4 changed files with 103 additions and 10 deletions.
@@ -70,15 +70,7 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
@@ -152,6 +144,10 @@ public class SerializerFactory extends AbstractSerializerFactory {
} catch (Throwable e) {
}


_staticSerializerMap.put(UUID.class, new StringValueSerializer());
_staticDeserializerMap.put(UUID.class, new UUIDDeserializer());

_staticSerializerMap.put(File.class, new StringValueSerializer());
try {
_staticDeserializerMap.put(File.class,
@@ -0,0 +1,28 @@
package com.alibaba.com.caucho.hessian.io;

import java.io.IOException;
import java.util.UUID;

/**
* Deserializing a uuid valued object
**/
public class UUIDDeserializer extends AbstractDeserializer {

@Override
public Class getType() {
return UUID.class;
}

@Override
public Object readObject(AbstractHessianInput in) throws IOException {
String uuidString = in.readString();
return UUID.fromString(uuidString);
}

@Override
public Object readObject(AbstractHessianInput in, String[] fieldNames) throws IOException {
String uuidString = in.readString();
return UUID.fromString(uuidString);
}

}
@@ -0,0 +1,70 @@
package com.alibaba.com.caucho.hessian.io;

import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;

/**
* fix hessian serialize bug:
* the uuid can not be deserialized properly
**/
public class Hessian2UUIDTest extends SerializeTestBase {

@Test
public void testUUIDObject() throws IOException {
UUID actual = UUID.randomUUID();
UUID deserialize = baseHessian2Serialize(actual);
Assert.assertEquals(actual, deserialize);
}

@Test
public void testUUIDList() throws IOException {
List<UUID> actual = new ArrayList<>(2);
actual.add(UUID.randomUUID());
actual.add(UUID.randomUUID());

ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bout);

out.writeObject(actual);
out.flush();

ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
Hessian2Input input = new Hessian2Input(bin);

List<UUID> deserialize = (List) input.readObject();
assertTrue(deserialize != null);
assertTrue(deserialize.size() == 2);
assertEquals(actual, deserialize);
}

@Test
public void testUUIDMap() throws IOException {
Map<UUID, Object> actual = new HashMap<>(8);
actual.put(UUID.randomUUID(), UUID.randomUUID());
actual.put(UUID.randomUUID(), null);

ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bout);

out.writeObject(actual);
out.flush();

ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
Hessian2Input input = new Hessian2Input(bin);

Map<UUID, Object> deserialize = (Map<UUID, Object>) input.readObject();
assertTrue(deserialize != null);
assertTrue(deserialize.size() == 2);
assertEquals(actual, deserialize);
}

}
@@ -20,7 +20,6 @@
import com.alibaba.com.caucho.hessian.io.beans.BaseUser;
import com.alibaba.com.caucho.hessian.io.beans.GrandsonUser;
import com.alibaba.com.caucho.hessian.io.beans.SubUser;

import org.junit.Assert;
import org.junit.Test;

0 comments on commit b662d24

Please sign in to comment.