mormot2 JSON序列
mormot2 JSON序列
record序列为json,lazarus是buffer的base64字符串,因为lazarus rtti对record支持不全。buffer的base64字符串,对于rest api跨语言开发无疑是致命的。
unit mormot2.json.serial; /// <author>cxg 202371</author> {$IFDEF fpc} {$MODE DELPHI} {$ENDIF} interface uses mormot.core.buffers, mormot.core.text, mormot.core.json, mormot.core.base, Classes, SysUtils; type { TSerial } TSerial = class class function marshal<T>(const aRec: T): rawutf8; overload; class function marshal(const aObj: TObject): rawutf8; overload; class function unmarshal<T>(const json: rawutf8): T; overload; class procedure unmarshal(const json: rawutf8; aObj: TObject); overload; end; implementation { TSerial } class function TSerial.marshal(const aObj: TObject): rawutf8; begin Result := mormot.core.text.ObjectToJson(aObj); end; class function TSerial.marshal<T>(const aRec: T): rawutf8; begin result := mormot.core.json.RecordSaveJson(aRec, TypeInfo(T)); end; class procedure TSerial.unmarshal(const json: rawutf8; aObj: TObject); begin mormot.core.json.ObjectLoadJson(aObj, json); end; class function TSerial.unmarshal<T>(const json: rawutf8): T; begin mormot.core.json.RecordLoadJson(result, json, TypeInfo(T)); end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/18101800