原来的DynamicObjectSerializeUtil序列化偶有问题,推荐大家使用DataEntitySerializer来做序列化
下面的案例是一个序列化的案例:
@Override
public void itemClick(ItemClickEvent evt) {
//新建一个单据数据包DynamicObject
DynamicObject newObject =BusinessDataServiceHelper.newDynamicObject("bidt_aaa");
newObject.set("billno", "1100");
newObject.set("billstatus", "A");
//通过此方法把DynamicObject转成json
String json=saveSrcBill(newObject);
//通过此方法把json转成DynamicObject
DynamicObject dyn =getSrcDataEntities(json);
//保存刚才转化的DynamicObject
SaveServiceHelper.saveOperate("bidt_aaa",new DynamicObject[] {dyn});
}
/**
* 保存源单
*
* @param subMainType 单据主实体模型节选,只包含部分字段
* @param dataEntities 单据数据包,只包含部分字段
* @param selectFields 包含的字段清单:反序列化时,需要据此重构节选单据主实体模型
*/
public String saveSrcBill( DynamicObject cacheObject){
DataEntitySerializerOption option = new DataEntitySerializerOption();
option.setIncludeDataEntityState(false);
option.setIncludeComplexProperty(true);
option.setIncludeCollectionProperty(true);
option.setIncludeType(false);
String json = DataEntitySerializer.serializerToString(cacheObject, option);
return json;
}
/**
* 输出源单数据包
* @return
* @remark 使用单据节选实体模型,反序列化数据包(实体模型中的字段,必须与序列化时数据包中包含的字段一致)
*/
public DynamicObject getSrcDataEntities(String json) {
MainEntityType subMainType = (MainEntityType) EntityMetadataCache.getDataEntityType("bidt_aaa");
DynamicObject dyn = (DynamicObject) DataEntitySerializer.deSerializerFromString(json, subMainType);
return dyn;
}
推荐阅读