本文整理汇总了Java中com.github.ddth.commons.utils.SerializationUtils类的典型用法代码示例。如果您正苦于以下问题:Java SerializationUtils类的具体用法?Java SerializationUtils怎么用?Java SerializationUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SerializationUtils类属于com.github.ddth.commons.utils包,在下文中一共展示了SerializationUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseRequest
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private static Map<String, Object> parseRequest() {
RequestBody requestBody = request().body();
String requestContent = null;
JsonNode jsonNode = requestBody.asJson();
if (jsonNode != null) {
requestContent = jsonNode.toString();
} else {
RawBuffer rawBuffer = requestBody.asRaw();
if (rawBuffer != null) {
requestContent = new String(rawBuffer.asBytes(), Constants.UTF8);
} else {
requestContent = requestBody.asText();
}
}
return SerializationUtils.fromJsonString(requestContent, Map.class);
}
示例2: initQueueMetadata
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected boolean initQueueMetadata(String queueName) {
if (!isValidQueueName(queueName)) {
return false;
}
String normalizedQueueName = normalizeQueueName(queueName);
try (Jedis jedis = jedisPool.getResource()) {
Map<String, Object> queueMetadata = new HashMap<String, Object>();
queueMetadata.put("queue_name", normalizedQueueName);
queueMetadata.put("queue_timestamp_create", new Date());
byte[] data = SerializationUtils.toJsonString(queueMetadata).getBytes(UTF8);
byte[] field = normalizedQueueName.getBytes(UTF8);
Long result = jedis.hset(metadataRedisHashName, field, data);
return result != null && result.longValue() > 0;
}
}
示例3: callApi
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private Map<String, Object> callApi(String url) {
try {
HttpResponse httpResponse = HttpRequest.get(url).timeout(5000).send();
try {
if (httpResponse.statusCode() != 200) {
return null;
}
int contentLength = Integer.parseInt(httpResponse.contentLength());
if (contentLength == 0 || contentLength > 1024) {
LOGGER.warn("Invalid response length: " + contentLength);
return null;
}
return SerializationUtils.fromJsonString(httpResponse.body(), Map.class);
} finally {
httpResponse.close();
}
} catch (Exception e) {
LOGGER.warn(e.getMessage(), e);
return null;
}
}
示例4: fromBytes
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* Deserializes from a {@code byte[]} - which has been serialized by
* {@link #toBytes()}.
*
* @param msgData
* @return
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
public static <T extends BaseUniversalQueueMessage<ID>, ID> T fromBytes(byte[] msgData,
Class<T> clazz) throws InstantiationException, IllegalAccessException {
// firstly, deserialize the input data to a map
String msgDataJson = msgData != null ? new String(msgData, QueueUtils.UTF8) : null;
Map<String, Object> dataMap = null;
try {
dataMap = msgDataJson != null
? SerializationUtils.fromJsonString(msgDataJson, Map.class) : null;
} catch (Exception e) {
dataMap = null;
}
if (dataMap == null) {
return null;
}
T msg = clazz.newInstance();
msg.fromMap(dataMap);
return msg;
}
示例5: loadAsMap
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Map<Object, Object> loadAsMap(String tableName, String entryId) {
long timestampStart = System.currentTimeMillis();
final String CQL = MessageFormat.format(CQL_LOAD, tableName);
try {
Session session = getSession();
List<Row> rows = CqlUtils.execute(session, CQL, entryId).all();
if (rows == null || rows.size() == 0) {
// not found
return null;
}
Map<Object, Object> result = new HashMap<Object, Object>();
for (Row row : rows) {
String key = row.getString(COL_KEY);
String value = row.getString(COL_VALUE);
result.put(key, SerializationUtils.fromJsonString(value, Object.class));
}
return result;
} finally {
BaseDao.addProfiling(timestampStart, CQL, System.currentTimeMillis() - timestampStart);
}
}
示例6: store
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void store(String tableName, String entryId, Map<Object, Object> data) {
long timestampStart = System.currentTimeMillis();
final String CQL = MessageFormat.format(CQL_STORE, tableName, COL_ID, COL_KEY, COL_VALUE);
try {
Session session = getSession();
for (Entry<Object, Object> entry : data.entrySet()) {
String key = entry.getKey().toString();
String value = SerializationUtils.toJsonString(entry.getValue());
CqlUtils.executeNonSelect(session, CQL, entryId, key, value);
}
} finally {
BaseDao.addProfiling(timestampStart, CQL, System.currentTimeMillis() - timestampStart);
}
}
示例7: fromJson
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* Deserializes a BO from a JSON string.
*
* @param json
* the JSON string obtained from {@link #toJson(BaseBo)}
* @param clazz
* @param classLoader
* @return
* @since 0.6.0.3
*/
@SuppressWarnings("unchecked")
public static <T extends BaseBo> T fromJson(String json, Class<T> clazz,
ClassLoader classLoader) {
if (StringUtils.isBlank(json) || clazz == null) {
return null;
}
try {
Map<String, Object> data = SerializationUtils.fromJsonString(json, Map.class);
String boClassName = DPathUtils.getValue(data, FIELD_CLASSNAME, String.class);
T bo = createObject(boClassName, classLoader, clazz);
if (bo != null) {
bo.fromJson(DPathUtils.getValue(data, FIELD_BODATA, String.class));
return bo;
} else {
return null;
}
} catch (Exception e) {
throw e instanceof DeserializationException ? (DeserializationException) e
: new DeserializationException(e);
}
}
示例8: fromBytes
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* Deserializes a BO from a byte array.
*
* @param bytes
* the byte array obtained from {@link #toBytes(BaseBo)}
* @param clazz
* @param classLoader
* @return
* @since 0.6.0.3
*/
@SuppressWarnings("unchecked")
public static <T extends BaseBo> T fromBytes(byte[] bytes, Class<T> clazz,
ClassLoader classLoader) {
if (bytes == null || clazz == null) {
return null;
}
try {
Map<String, Object> data = SerializationUtils.fromByteArray(bytes, Map.class);
String boClassName = DPathUtils.getValue(data, FIELD_CLASSNAME, String.class);
T bo = createObject(boClassName, classLoader, clazz);
if (bo != null) {
bo.fromByteArray(DPathUtils.getValue(data, FIELD_BODATA, byte[].class));
return bo;
} else {
return null;
}
} catch (Exception e) {
throw e instanceof DeserializationException ? (DeserializationException) e
: new DeserializationException(e);
}
}
示例9: setAttributes
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* Set all BO's attributes.
*
* @param attrs
* @return
*/
@SuppressWarnings("unchecked")
public BaseBo setAttributes(Map<String, Object> attrs) {
lock();
try {
if (attrs == null) {
attributes = attrs;
} else {
attributes = SerializationUtils.fromByteArray(SerializationUtils.toByteArray(attrs),
Map.class);
}
triggerPopulate();
return this;
} finally {
unlock();
}
}
示例10: setSubAttr
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* Set a sub-attribute.
*
* @param attrName
* @param value
* @return
*/
public BaseJsonBo setSubAttr(String attrName, String dPath, Object value) {
lock();
try {
JsonNode attr = cacheJsonObjs.get(attrName);
if (attr == null) {
String[] paths = DPathUtils.splitDpath(dPath);
if (paths[0].matches("^\\[(.*?)\\]$")) {
setAttribute(attrName, "[]");
} else {
setAttribute(attrName, "{}");
}
attr = cacheJsonObjs.get(attrName);
}
JacksonUtils.setValue(attr, dPath, value, true);
return (BaseJsonBo) setAttribute(attrName, SerializationUtils.toJsonString(attr),
false);
} finally {
unlock();
}
}
示例11: triggerPopulate
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected void triggerPopulate() {
super.triggerPopulate();
lock();
try {
cacheJsonObjs.clear();
for (Entry<String, Object> entry : attributeMap().entrySet()) {
String attrName = entry.getKey();
String attrValue = entry.getValue().toString();
cacheJsonObjs.put(attrName, SerializationUtils.readJson(attrValue));
}
} finally {
unlock();
}
}
示例12: deserialize
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
public static CacheEntry deserialize(byte[] data) throws TException {
TCacheEntry cacheEntry = com.github.ddth.commons.utils.ThriftUtils.fromBytes(data,
TCacheEntry.class);
if (cacheEntry == null) {
return null;
}
try {
CacheEntry ce = new CacheEntry();
ce.setExpireAfterAccess(cacheEntry.expireAfterAccess)
.setExpireAfterWrite(cacheEntry.expireAfterWrite).setKey(cacheEntry.key);
ce.setCreationTimestamp(cacheEntry.creationTimestampMs)
.setLastAccessTimestamp(cacheEntry.lastAccessTimestampMs);
if (cacheEntry.valueClass != null) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class<?> clazz = Class.forName(cacheEntry.valueClass, false, classLoader);
ce.setValue(SerializationUtils.fromByteArray(cacheEntry.getValue(), clazz));
}
return ce;
} catch (ClassNotFoundException e) {
throw new TException(e);
}
}
示例13: toBytes
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* @since 0.5.0
*/
@Override
public byte[] toBytes() throws SerializationException {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("k", this.key);
dataMap.put("tac", this.lastAccessTimestampMs);
dataMap.put("tcr", this.creationTimestampMs);
dataMap.put("eac", this.expireAfterAccess);
dataMap.put("ewr", this.expireAfterWrite);
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (value != null) {
dataMap.put("_c_", value.getClass().getName());
dataMap.put("v", SerializationUtils.toByteArray(this.value, cl));
}
return SerializationUtils.toByteArrayFst(dataMap, cl);
}
示例14: main
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
{
// bootstrap
CacheEntry ce = new CacheEntry();
SerializationUtils.toByteArray(ce);
SerializationUtils.toByteArrayKryo(ce);
ThriftUtils.serialize(ce);
}
Map<String, Object> value1 = new HashMap<String, Object>();
{
value1.put("first_name", "Thành");
value1.put("last_name", "Nguyễn");
value1.put("email", "[email protected]");
value1.put("dob", new Date());
}
test(value1);
System.out.println("========================================");
TestValue.Value value2 = new TestValue.Value();
test(value2);
}
示例15: newInstance
import com.github.ddth.commons.utils.SerializationUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static FileInfo newInstance(byte[] data) {
if (data == null || data.length <= 8) {
return null;
}
Map<String, Object> dataMap = SerializationUtils.fromByteArray(data, Map.class);
if (dataMap == null) {
return null;
}
FileInfo fileInfo = newInstance();
fileInfo.fromMap(dataMap);
return fileInfo;
}