TOL = class(TObject) public procedure W(W: TJsonWriter; Instance: TObject; Options: TTextWriterWriteObjectOptions); end; TPerson = class private FName: string; FID: integer; FSex: Byte; published property ID: integer read FID write FID; property name: string read FName write FName; property sex: Byte read FSex write FSex; end; TTestObject = class private FL: TObjectList<TPerson>; public constructor Create; published property L: TObjectList<TPerson> read FL write FL; end; procedure TForm1.Button3Click(Sender: TObject); begin var OL := TOL.Create; var TestObj := TTestObject.Create; for var I := 0 to 2 do begin var p := TPerson.Create; p.FName := 'JJW'; p.FID := 1; p.FSex := 1; TestObj.L.Add(p); end; //方法一 Rtti.RegisterClass(TObjectList<TPerson>).JsonSave := Rtti.RegisterClass(TObjectList).JsonSave; Memo1.Text := ObjectToJson(TestObj, [woObjectListWontStoreClassName]); //方法二 var rj := TRttiJson.RegisterCustomSerializerClass(TObjectList<TPerson>, nil, OL.W); var jsonStr: RawUtf8; SaveJson(TestObj, TypeInfo(TTestObject), [], jsonStr, [woObjectListWontStoreClassName]); Memo1.Text := jsonStr; end; { TOL } procedure TOL.W(W: TJsonWriter; Instance: TObject; Options: TTextWriterWriteObjectOptions); begin W.Add('['); var p := TList(Instance); for var I := 0 to P.Count-1 do W.WriteObject(P[I]); w.Add(']'); end;View Code
针对 每个实体类,必须 TObjectList<TXXXObject> 注册一遍,感觉也挺麻烦的。
标签:end,Create,笔记,JSON,FSex,TOL,mormot2,var,TObjectList From: https://www.cnblogs.com/Jiaojiawang/p/17656781.html