下面列出了怎么用org.springframework.util.SerializationUtils的API类实例代码及写法,或者点击链接到github查看源代码。
private int fetchDataValuesPageSizeFromSystemSettings( final Context context ) throws SQLException
{
int dataValuesPageSize = 0;
String sql = "SELECT value FROM systemsetting WHERE name = '" + DATA_VALUES_SYNC_PAGE_SIZE_KEY +"';";
try ( Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery( sql ); )
{
if ( rs.next() )
{
dataValuesPageSize = (Integer) SerializationUtils.deserialize( rs.getBytes( "value" ) );
}
log.info( "Value found in SystemSettings: dataValuePageSize: " + dataValuesPageSize );
}
return dataValuesPageSize;
}
@SuppressWarnings("unchecked")
@Nullable
private static <T extends Throwable> T cloneException(T exception) {
try {
return (T) SerializationUtils.deserialize(SerializationUtils.serialize(exception));
}
catch (Exception ex) {
return null; // exception parameter cannot be cloned
}
}
@Override
public void run(String... args) {
// null意味着是BootstrapClassLoader
System.out.println("Object classloader:" + Object.class.getClassLoader());
// RestartClassLoader
System.out.println("ClassLoaderApplication classloader:"
+ ClassLoaderApplication.class.getClassLoader().getClass().getSimpleName());
// AppClassLoader
System.out.println("ClassLoaderApplication classloader parent:"
+ ClassLoaderApplication.class.getClassLoader().getParent().getClass().getSimpleName());
// ExtClassLoader
System.out.println("ClassLoaderApplication classloader parent's parent:"
+ ClassLoaderApplication.class.getClassLoader().getParent().getParent().getClass().getSimpleName());
// UserBaseDto是已导入到IDE的项目其它模块类文件
UserBaseDto userBaseDto = new UserBaseDto();
System.out.println("userBaseDto object classloader:"
+ userBaseDto.getClass().getClassLoader().getClass().getSimpleName());
byte[] bytes = SerializationUtils.serialize(userBaseDto);
Object deserializeUserBaseDto = SerializationUtils.deserialize(bytes);
System.out.println("deserialize userBaseDto object classloader:"
+ deserializeUserBaseDto.getClass().getClassLoader().getClass().getSimpleName());
// userBaseDto = (UserBaseDto) deserializeUserBaseDto;
deserializeUserBaseDto = SerializeUtils.deserialize(bytes);
userBaseDto = (UserBaseDto) deserializeUserBaseDto;
System.out.println(userBaseDto);
}
@Test
public void testSerialization() {
Vet vet = new Vet();
vet.setFirstName("Zaphod");
vet.setLastName("Beeblebrox");
vet.setId(123);
Vet other = (Vet) SerializationUtils
.deserialize(SerializationUtils.serialize(vet));
assertThat(other.getFirstName()).isEqualTo(vet.getFirstName());
assertThat(other.getLastName()).isEqualTo(vet.getLastName());
assertThat(other.getId()).isEqualTo(vet.getId());
}
@Test
void testSerialization() {
Vet vet = new Vet();
vet.setFirstName("Zaphod");
vet.setLastName("Beeblebrox");
vet.setId(123);
Vet other = (Vet) SerializationUtils.deserialize(SerializationUtils.serialize(vet));
assertThat(other.getFirstName()).isEqualTo(vet.getFirstName());
assertThat(other.getLastName()).isEqualTo(vet.getLastName());
assertThat(other.getId()).isEqualTo(vet.getId());
}
@SuppressWarnings("unchecked")
@Nullable
private static <T extends Throwable> T cloneException(T exception) {
try {
return (T) SerializationUtils.deserialize(SerializationUtils.serialize(exception));
}
catch (Exception ex) {
return null; // exception parameter cannot be cloned
}
}
@Override
public <S> S merge(S source, S override) {
List<SourceMerger> mergers = produceList(FactoryPredicates::isSourceAssignableFrom, source);
S result = (S) SerializationUtils.deserialize(SerializationUtils.serialize(source));
for (SourceMerger merger : mergers) {
result = (S) merger.merge(result, override);
}
return result;
}
@Test
void testSerialization() {
Vet vet = new Vet();
vet.setFirstName("Zaphod");
vet.setLastName("Beeblebrox");
vet.setId(123);
Vet other = (Vet) SerializationUtils
.deserialize(SerializationUtils.serialize(vet));
assertThat(other.getFirstName()).isEqualTo(vet.getFirstName());
assertThat(other.getLastName()).isEqualTo(vet.getLastName());
assertThat(other.getId()).isEqualTo(vet.getId());
}
@Override
protected Serializable doCreate(Session session) {
Serializable sessionId = generateSessionId(session);
assignSessionId(session, sessionId);
// session.setAttribute("user",session.get);
byte[] keyByte = RedisKey.getKeyByte(RedisKey.SESSION_PRE, sessionId.toString());
byte[] sessionByte = SerializationUtils.serialize(session);
client.setEx(keyByte, sessionByte, SESSION_EXPIRE);
return sessionId;
}
@Override
protected Session doReadSession(Serializable sessionId) {
if (sessionId == null) {
return null;
}
byte[] keyByte = RedisKey.getKeyByte(RedisKey.SESSION_PRE, sessionId.toString());
byte[] sessionByte = client.get(keyByte);
return (Session) SerializationUtils.deserialize(sessionByte);
}
@Override
public Collection<Session> getActiveSessions() {
Set<byte[]> keyByteSet = client.keys((RedisKey.SESSION_PRE + "*").getBytes());
Set<Session> sessionSet = Sets.newHashSet();
for (byte[] keyByte : keyByteSet) {
byte[] sessionByte = client.get(keyByte);
Session session = (Session) SerializationUtils.deserialize(sessionByte);
if (session != null) {
sessionSet.add(session);
}
}
return sessionSet;
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> T cloneException(T exception) {
try {
return (T) SerializationUtils.deserialize(SerializationUtils.serialize(exception));
}
catch (Exception ex) {
return null; // exception parameter cannot be cloned
}
}
public void init(Context context){
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Session session=context.getSession();
if(type.equals(VariableType.Blob) || type.equals(VariableType.Text)){
HistoryBlob blob=(HistoryBlob)session.get(HistoryBlob.class,Long.valueOf(stringValue));
variableObject=SerializationUtils.deserialize(blob.getBlobValue());
}else if(type.equals(VariableType.String)){
variableObject=stringValue;
}else if(type.equals(VariableType.Date)){
try {
variableObject=sd.parse(stringValue);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}else if(type.equals(VariableType.Integer)){
variableObject=Integer.valueOf(stringValue);
}else if(type.equals(VariableType.Character)){
variableObject=Character.valueOf((char)(stringValue.getBytes()[0]));
}else if(type.equals(VariableType.Double)){
variableObject=Double.valueOf(stringValue);
}else if(type.equals(VariableType.Float)){
variableObject=Float.valueOf(stringValue);
}else if(type.equals(VariableType.Long)){
variableObject=Long.valueOf(stringValue);
}else if(type.equals(VariableType.Byte)){
variableObject=Byte.valueOf(stringValue);
}else if(type.equals(VariableType.Short)){
variableObject=Short.valueOf(stringValue);
}else if(type.equals(VariableType.Boolean)){
variableObject=Boolean.valueOf(stringValue);
}
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> T cloneException(T exception) {
try {
return (T) SerializationUtils.deserialize(SerializationUtils.serialize(exception));
}
catch (Exception ex) {
return null; // exception parameter cannot be cloned
}
}
public WsMessage(final Message message) {
this.source = message.getSource();
this.subject = message.getSubject();
if (message.getTargets() != null) {
this.targets = new ArrayList<>(message.getTargets());
}
this.payload = SerializationUtils.serialize(message.getPayload());
}
public RestMessage(final Message message) {
this.source = message.getSource();
this.subject = message.getSubject();
if (message.getTargets() != null) {
this.targets = new ArrayList<>(message.getTargets());
}
this.payload = SerializationUtils.serialize(message.getPayload());
}
/**
* Method to get the events.
* @return
*/
@Transient
public Event getEvent() {
if (getEventBytes() == null) {
return null;
}
return (Event) SerializationUtils.deserialize(getEventBytes());
}
/**
* Method to get data as Linked Hash Map.
*
* @return {@link ClusterConf}
*/
@Transient
public ClusterConfig getData() {
if (getDataBytes() == null) {
return null;
}
try {
return (ClusterConfig) SerializationUtils.deserialize(getDataBytes());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Method to set the ClusterConf as bytes in db.
*
*/
public void setData(ClusterConfig data) {
if (data != null) {
data.setState(Constant.Cluster.State.ERROR);
setDataBytes(SerializationUtils.serialize(data));
}
}
@Test
public void testSerializablePostProcessor() {
byte[] ser = SerializationUtils.serialize(configurableBean);
ConfigurableBean cb = (ConfigurableBean) SerializationUtils.deserialize(ser);
assertEquals(cb.getNoSerializableBean(), configurableBean.getNoSerializableBean());
ser = SerializationUtils.serialize(autowiredBean);
AutowiredBean ab = (AutowiredBean) SerializationUtils.deserialize(ser);
assertEquals(autowiredBean.getNoSerializableBean(), ab.getNoSerializableBean());
}
@SuppressWarnings("unchecked")
public static <T> T copy(T cloningObject) {
return (T) SerializationUtils.deserialize(SerializationUtils.serialize(cloningObject));
}
@Override
public S execute(CompileContext<?, ?> context, DataSet data, Supplier<S> supplier, CompileProcessor compileProcessor,
BindProcessor bindProcessor,
ValidateProcessor validateProcessor) {
return (S) SerializationUtils.deserialize(SerializationUtils.serialize(supplier.get()));
}
public static String serialize(Object object) {
return Base64.getUrlEncoder()
.encodeToString(SerializationUtils.serialize(object));
}
public static <T> T deserialize(Cookie cookie, Class<T> cls) {
return cls.cast(SerializationUtils.deserialize(
Base64.getUrlDecoder().decode(cookie.getValue())));
}
@Override
public byte[] serialization(Object obj) {
return SerializationUtils.serialize(obj);
}
@SuppressWarnings("unchecked")
@Override
public <T> T deserialize(byte[] bytes) {
return (T)SerializationUtils.deserialize(bytes);
}
@Override
public Object read(byte[] payload, Class<?> targetType) {
return SerializationUtils.deserialize(payload);
}
@Override
public byte[] write(Object target) {
return SerializationUtils.serialize(target);
}
@Override
public void update(Session session) throws UnknownSessionException {
byte[] keyByte = RedisKey.getKeyByte(RedisKey.SESSION_PRE, session.getId().toString());
byte[] sessionByte = SerializationUtils.serialize(session);
client.setEx(keyByte, sessionByte, SESSION_EXPIRE);
}
public void initValue(Context context){
Session session=context.getSession();
blob=(Blob)session.get(Blob.class,blobId);
text=(String)SerializationUtils.deserialize(blob.getBlobValue());
}