>>>>>
unit TestDef;
interface
uses
System.Rtti,
System.JSON,
System.SysUtils,
REST.Json,
REST.Json.Types,
REST.JsonReflect;
type
TGuidInterceptor =
class (TJSONInterceptor)
public
function StringConverter (AData: TObject; AField:
string):
string;
override;
procedure StringReverter (AData: TObject; AField:
string; AArg:
string);
override;
end;
type
{$METHODINFO ON}
TRec =
record
[JSonReflect (ctString, rtString, TGuidInterceptor,
nil, True)]
FID: TGUID;
end;
TTest =
class (TObject)
public
[JSonReflect (ctString, rtString, TGuidInterceptor,
nil, False)]
FID: TGUID;
// FRec: TRec;
end;
{$METHODINFO OFF}
procedure RunTest;
implementation
function TGuidInterceptor.StringConverter (AData: TObject; AField:
string):
string;
var
Ctx: TRttiContext;
GUID: TGUID;
begin
GUID := Ctx.GetType (AData.ClassType).GetField (AField).GetValue (AData).AsType<TGUID>;
Result := GUIDToString (
GUID);
end;
procedure TGuidInterceptor.StringReverter (AData: TObject; AField:
string; AArg:
string);
var
Ctx: TRttiContext;
GUID: TGUID;
begin
GUID := StringToGUID (AArg);
Ctx.GetType (AData.ClassType).GetField (AField).SetValue (AData, TValue.From<TGUID> (
GUID));
end;
procedure RunTest;
var
TestObj: TTest;
TestRev: TTest;
S1, S2:
string;
LJSONValue: TJSONValue;
LJSONObject: TJSONObject;
begin
TestObj := TTest.Create;
try
TestObj.FID := TGUID.NewGuid;
// TestObj.FRec.FID := TestObj.FID;
S1 := TJson.ObjectToJsonString (TestObj);
LJSONValue := TJSONObject.ParseJSONValue (S1);
Assert (Assigned (LJSONValue)
and (LJSONValue
is TJSONObject));
if Assigned (LJSONValue)
and (LJSONValue
is TJSONObject)
then begin
LJSONObject := LJSONValue
as TJSONObject;
TestRev := TTest.Create;
try
TJson.JsonToObject (TestRev, LJSONObject);
S2 := TJson.ObjectToJsonString (TestObj);
Assert (S1.Equals (S2));
finally
TestRev.Free;
end;
end;
finally
TestObj.Free;
end;
end;
end.
<<<<<