use of com.alipay.hessian.generic.model.GenericObject in project sofa-rpc by sofastack.
the class SofaResponseHessianSerializer method decodeObjectByTemplate.
@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template) throws SofaRpcException {
try {
UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
Hessian2Input input = new Hessian2Input(inputStream);
// 根据SerializeType信息决定序列化器
boolean genericSerialize = context != null && isGenericResponse(context.get(RemotingConstants.HEAD_GENERIC_TYPE));
if (genericSerialize) {
input.setSerializerFactory(genericSerializerFactory);
GenericObject genericObject = (GenericObject) input.readObject();
template.setErrorMsg((String) genericObject.getField("errorMsg"));
template.setAppResponse(genericObject.getField("appResponse"));
template.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
} else {
input.setSerializerFactory(serializerFactory);
SofaResponse tmp = (SofaResponse) input.readObject();
// copy values to template
template.setErrorMsg(tmp.getErrorMsg());
template.setAppResponse(tmp.getAppResponse());
template.setResponseProps(tmp.getResponseProps());
}
input.close();
} catch (IOException e) {
throw buildDeserializeError(e.getMessage(), e);
}
}
use of com.alipay.hessian.generic.model.GenericObject in project sofa-boot by sofastack.
the class SofaBootRpcAllTest method testGeneric.
@Test
public void testGeneric() {
GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.boot.test.bean.generic.GenericParamModel");
genericObject.putField("name", "Bible");
GenericObject result = (GenericObject) genericService.$genericInvoke("sayGeneric", new String[] { "com.alipay.sofa.rpc.boot.test.bean.generic.GenericParamModel" }, new Object[] { genericObject });
Assert.assertEquals("com.alipay.sofa.rpc.boot.test.bean.generic.GenericResultModel", result.getType());
Assert.assertEquals("Bible", result.getField("name"));
Assert.assertEquals("sample generic value", result.getField("value"));
}
use of com.alipay.hessian.generic.model.GenericObject in project sofa-boot by alipay.
the class SofaBootRpcAllTest method testGeneric.
@Test
public void testGeneric() {
GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.boot.test.bean.generic.GenericParamModel");
genericObject.putField("name", "Bible");
GenericObject result = (GenericObject) genericService.$genericInvoke("sayGeneric", new String[] { "com.alipay.sofa.rpc.boot.test.bean.generic.GenericParamModel" }, new Object[] { genericObject });
Assert.assertEquals("com.alipay.sofa.rpc.boot.test.bean.generic.GenericResultModel", result.getType());
Assert.assertEquals("Bible", result.getField("name"));
Assert.assertEquals("sample generic value", result.getField("value"));
}
use of com.alipay.hessian.generic.model.GenericObject in project incubator-shenyu by apache.
the class SofaParamResolveServiceImpl method convertToParameterValue.
/**
* convert to parameter value.
*
* @param value value support [json object string, json array string,string]
* @param parameterType parameter type support [javaPackage.ClassName#GenericType1#GenericType2], split is ,
* @return GenericObject, GenericMap, List, string, array...
* @see com.alipay.hessian.generic.model
*/
@SuppressWarnings("all")
private Object convertToParameterValue(final Object value, final String[] parameterType) {
if (isSingleType(parameterType)) {
return value;
}
if (value instanceof JsonObject && parameterType[0].contains("Map")) {
final Map<String, Object> mapValue = GsonUtils.getInstance().convertToMap(value.toString());
if (parameterType.length == 1) {
// no generic info
return mapValue;
}
assert parameterType.length == 3;
// generic map
final GenericMap genericMap = new GenericMap(parameterType[2]);
mapValue.replaceAll((k, v) -> convertToGenericObject(parameterType[2], mapValue.get(k)));
genericMap.setMap(mapValue);
return genericMap;
}
if (value instanceof JsonObject) {
return convertToGenericObject(parameterType[0], value);
}
if (value instanceof JsonArray) {
if (parameterType.length == 1) {
// no generic info
return GsonUtils.getInstance().fromList(value.toString(), Object.class);
}
// generic collection
final GenericCollection genericCollection = new GenericCollection(parameterType[1]);
genericCollection.setCollection(convertToGenericObjects(parameterType[1], (Iterable<Object>) value));
return genericCollection;
}
return value;
}
use of com.alipay.hessian.generic.model.GenericObject in project incubator-shenyu by apache.
the class SofaParamResolveServiceImpl method convertToGenericObject.
/**
* convert json object to {@link GenericObject}.
*
* @param paramType param type string
* @param paramValue param value (if is object, auto to convert string)
* @return {@link GenericObject},if is single customize type return paramValue
* @see GenericObject
* @see #isSingleType(String)
*/
private static Object convertToGenericObject(final String paramType, final Object paramValue) {
if (isSingleType(paramType)) {
return paramValue;
}
final Map<String, Object> mapValue = GsonUtils.getInstance().convertToMap(paramValue.toString());
GenericObject genericObject = new GenericObject(paramType);
mapValue.forEach(genericObject::putField);
return genericObject;
}
Aggregations