|
@@ -7,7 +7,7 @@
|
|
Author : Kike Pérez
|
|
Author : Kike Pérez
|
|
Version : 1.12
|
|
Version : 1.12
|
|
Created : 21/05/2018
|
|
Created : 21/05/2018
|
|
- Modified : 03/10/2021
|
|
|
|
|
|
+ Modified : 27/12/2021
|
|
|
|
|
|
This file is part of QuickLib: https://github.com/exilon/QuickLib
|
|
This file is part of QuickLib: https://github.com/exilon/QuickLib
|
|
|
|
|
|
@@ -131,16 +131,18 @@ type
|
|
fUseBase64Stream : Boolean;
|
|
fUseBase64Stream : Boolean;
|
|
fUseNullStringsAsEmpty : Boolean;
|
|
fUseNullStringsAsEmpty : Boolean;
|
|
function GetValue(aAddr: Pointer; aType: TRTTIType): TValue; overload;
|
|
function GetValue(aAddr: Pointer; aType: TRTTIType): TValue; overload;
|
|
|
|
+ {$IFDEF FPC}
|
|
function GetValue(aAddr: Pointer; aTypeInfo: PTypeInfo): TValue; overload;
|
|
function GetValue(aAddr: Pointer; aTypeInfo: PTypeInfo): TValue; overload;
|
|
|
|
+ {$ENDIF}
|
|
function IsAllowedProperty(aObject : TObject; const aPropertyName : string) : Boolean;
|
|
function IsAllowedProperty(aObject : TObject; const aPropertyName : string) : Boolean;
|
|
- function GetPropertyValue(Instance : TObject; const PropertyName : string) : TValue;
|
|
|
|
|
|
+ //function GetPropertyValue(Instance : TObject; const PropertyName : string) : TValue;
|
|
function GetPropertyValueFromObject(Instance : TObject; const PropertyName : string) : TValue;
|
|
function GetPropertyValueFromObject(Instance : TObject; const PropertyName : string) : TValue;
|
|
{$IFNDEF FPC}
|
|
{$IFNDEF FPC}
|
|
function GetFieldValueFromRecord(const aValue : TValue; const FieldName : string) : TValue;
|
|
function GetFieldValueFromRecord(const aValue : TValue; const FieldName : string) : TValue;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
+ {$IFDEF FPC}
|
|
procedure SetPropertyValue(Instance : TObject; aPropInfo : PPropInfo; aValue : TValue); overload;
|
|
procedure SetPropertyValue(Instance : TObject; aPropInfo : PPropInfo; aValue : TValue); overload;
|
|
procedure SetPropertyValue(Instance : TObject; const PropertyName : string; aValue : TValue); overload;
|
|
procedure SetPropertyValue(Instance : TObject; const PropertyName : string; aValue : TValue); overload;
|
|
- {$IFDEF FPC}
|
|
|
|
function FloatProperty(aObject : TObject; aPropInfo: PPropInfo): string;
|
|
function FloatProperty(aObject : TObject; aPropInfo: PPropInfo): string;
|
|
function GetPropType(aPropInfo: PPropInfo): PTypeInfo;
|
|
function GetPropType(aPropInfo: PPropInfo): PTypeInfo;
|
|
procedure LoadSetProperty(aInstance : TObject; aPropInfo: PPropInfo; const aValue: string);
|
|
procedure LoadSetProperty(aInstance : TObject; aPropInfo: PPropInfo; const aValue: string);
|
|
@@ -210,7 +212,7 @@ type
|
|
property UseEnumNames : Boolean read fUseEnumNames write SetUseEnumNames;
|
|
property UseEnumNames : Boolean read fUseEnumNames write SetUseEnumNames;
|
|
property UseJsonCaseSense : Boolean read fUseJsonCaseSense write SetUseJsonCaseSense;
|
|
property UseJsonCaseSense : Boolean read fUseJsonCaseSense write SetUseJsonCaseSense;
|
|
property UseBase64Stream : Boolean read fUseBase64Stream write SetUseBase64Stream;
|
|
property UseBase64Stream : Boolean read fUseBase64Stream write SetUseBase64Stream;
|
|
- property UseNullStringsAsEmpty : Boolean read fUseNullStringsAsEmpty write fUseNullStringsAsEmpty;
|
|
|
|
|
|
+ property UseNullStringsAsEmpty : Boolean read fUseNullStringsAsEmpty write SetUseNullStringsAsEmpty;
|
|
function JsonToObject(aType : TClass; const aJson: string) : TObject; overload;
|
|
function JsonToObject(aType : TClass; const aJson: string) : TObject; overload;
|
|
function JsonToObject(aObject : TObject; const aJson: string) : TObject; overload;
|
|
function JsonToObject(aObject : TObject; const aJson: string) : TObject; overload;
|
|
function JsonStreamToObject(aObject : TObject; aJsonStream : TStream) : TObject;
|
|
function JsonStreamToObject(aObject : TObject; aJsonStream : TStream) : TObject;
|
|
@@ -463,6 +465,7 @@ begin
|
|
finally
|
|
finally
|
|
stream.Free;
|
|
stream.Free;
|
|
end;
|
|
end;
|
|
|
|
+ Result := aObject;
|
|
end;
|
|
end;
|
|
|
|
|
|
constructor TRTTIJson.Create(aSerializeLevel : TSerializeLevel; aUseEnumNames : Boolean = True);
|
|
constructor TRTTIJson.Create(aSerializeLevel : TSerializeLevel; aUseEnumNames : Boolean = True);
|
|
@@ -587,7 +590,7 @@ begin
|
|
if IsGenericList(propvalue.AsObject) then DeserializeList(propvalue.AsObject,'List',TJSONObject(aJson.GetValue(propertyname)))
|
|
if IsGenericList(propvalue.AsObject) then DeserializeList(propvalue.AsObject,'List',TJSONObject(aJson.GetValue(propertyname)))
|
|
else Result := DeserializeProperty(Result,propertyname,rProp,aJson);
|
|
else Result := DeserializeProperty(Result,propertyname,rProp,aJson);
|
|
end
|
|
end
|
|
- else if IsGenericXArray(propvalue{$IFNDEF NEXTGEN}.TypeInfo.Name{$ELSE}.TypeInfo.NameFld.ToString{$ENDIF}) then
|
|
|
|
|
|
+ else if IsGenericXArray(string(propvalue{$IFNDEF NEXTGEN}.TypeInfo.Name{$ELSE}.TypeInfo.NameFld.ToString{$ENDIF})) then
|
|
begin
|
|
begin
|
|
DeserializeXArray(Result,propvalue,rProp,propertyname,aJson);
|
|
DeserializeXArray(Result,propvalue,rProp,propertyname,aJson);
|
|
end
|
|
end
|
|
@@ -1089,49 +1092,49 @@ begin
|
|
Result := nil;
|
|
Result := nil;
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TRTTIJson.GetPropertyValue(Instance : TObject; const PropertyName : string) : TValue;
|
|
|
|
-var
|
|
|
|
- pinfo : PPropInfo;
|
|
|
|
-begin
|
|
|
|
- Result := nil;
|
|
|
|
- pinfo := GetPropInfo(Instance,PropertyName);
|
|
|
|
- if pinfo = nil then raise EJsonSerializeError.CreateFmt('Property "%s" not found!',[PropertyName]);
|
|
|
|
- case pinfo.PropType^.Kind of
|
|
|
|
- tkInteger : Result := GetOrdProp(Instance,pinfo);
|
|
|
|
- tkInt64 : Result := GetInt64Prop(Instance,PropertyName);
|
|
|
|
- tkFloat : Result := GetFloatProp(Instance,PropertyName);
|
|
|
|
- tkChar : Result := Char(GetOrdProp(Instance,PropertyName));
|
|
|
|
- {$IFDEF FPC}
|
|
|
|
- tkWString : Result := GetWideStrProp(Instance,PropertyName);
|
|
|
|
- tkSString,
|
|
|
|
- tkAString,
|
|
|
|
- {$ELSE}
|
|
|
|
- tkWString,
|
|
|
|
- {$ENDIF}
|
|
|
|
- tkLString : Result := GetStrProp(Instance,pinfo);
|
|
|
|
- {$IFDEF FPC}
|
|
|
|
- tkEnumeration :
|
|
|
|
- begin
|
|
|
|
- if fUseEnumNames then Result := GetEnumName(pinfo.PropType,GetOrdProp(Instance,PropertyName))
|
|
|
|
- else Result := GetOrdProp(Instance,PropertyName);
|
|
|
|
- end;
|
|
|
|
- {$ELSE}
|
|
|
|
- tkEnumeration :
|
|
|
|
- begin
|
|
|
|
- if fUseEnumNames then Result := GetEnumName(@pinfo.PropType,GetOrdProp(Instance,PropertyName))
|
|
|
|
- else Result := GetOrdProp(Instance,PropertyName);
|
|
|
|
- end;
|
|
|
|
- {$ENDIF}
|
|
|
|
- tkSet : Result := GetSetProp(Instance,pinfo,True);
|
|
|
|
- {$IFNDEF FPC}
|
|
|
|
- tkClass :
|
|
|
|
- {$ELSE}
|
|
|
|
- tkBool : Result := Boolean(GetOrdProp(Instance,pinfo));
|
|
|
|
- tkObject :
|
|
|
|
- {$ENDIF} Result := GetObjectProp(Instance,pinfo);
|
|
|
|
- tkDynArray : Result := GetDynArrayProp(Instance,pinfo);
|
|
|
|
- end;
|
|
|
|
-end;
|
|
|
|
|
|
+//function TRTTIJson.GetPropertyValue(Instance : TObject; const PropertyName : string) : TValue;
|
|
|
|
+//var
|
|
|
|
+// pinfo : PPropInfo;
|
|
|
|
+//begin
|
|
|
|
+// Result := nil;
|
|
|
|
+// pinfo := GetPropInfo(Instance,PropertyName);
|
|
|
|
+// if pinfo = nil then raise EJsonSerializeError.CreateFmt('Property "%s" not found!',[PropertyName]);
|
|
|
|
+// case pinfo.PropType^.Kind of
|
|
|
|
+// tkInteger : Result := GetOrdProp(Instance,pinfo);
|
|
|
|
+// tkInt64 : Result := GetInt64Prop(Instance,PropertyName);
|
|
|
|
+// tkFloat : Result := GetFloatProp(Instance,PropertyName);
|
|
|
|
+// tkChar : Result := Char(GetOrdProp(Instance,PropertyName));
|
|
|
|
+// {$IFDEF FPC}
|
|
|
|
+// tkWString : Result := GetWideStrProp(Instance,PropertyName);
|
|
|
|
+// tkSString,
|
|
|
|
+// tkAString,
|
|
|
|
+// {$ELSE}
|
|
|
|
+// tkWString,
|
|
|
|
+// {$ENDIF}
|
|
|
|
+// tkLString : Result := GetStrProp(Instance,pinfo);
|
|
|
|
+// {$IFDEF FPC}
|
|
|
|
+// tkEnumeration :
|
|
|
|
+// begin
|
|
|
|
+// if fUseEnumNames then Result := GetEnumName(pinfo.PropType,GetOrdProp(Instance,PropertyName))
|
|
|
|
+// else Result := GetOrdProp(Instance,PropertyName);
|
|
|
|
+// end;
|
|
|
|
+// {$ELSE}
|
|
|
|
+// tkEnumeration :
|
|
|
|
+// begin
|
|
|
|
+// if fUseEnumNames then Result := GetEnumName(@pinfo.PropType,GetOrdProp(Instance,PropertyName))
|
|
|
|
+// else Result := GetOrdProp(Instance,PropertyName);
|
|
|
|
+// end;
|
|
|
|
+// {$ENDIF}
|
|
|
|
+// tkSet : Result := GetSetProp(Instance,pinfo,True);
|
|
|
|
+// {$IFNDEF FPC}
|
|
|
|
+// tkClass :
|
|
|
|
+// {$ELSE}
|
|
|
|
+// tkBool : Result := Boolean(GetOrdProp(Instance,pinfo));
|
|
|
|
+// tkObject :
|
|
|
|
+// {$ENDIF} Result := GetObjectProp(Instance,pinfo);
|
|
|
|
+// tkDynArray : Result := GetDynArrayProp(Instance,pinfo);
|
|
|
|
+// end;
|
|
|
|
+//end;
|
|
|
|
|
|
function TRTTIJson.GetPropertyValueFromObject(Instance : TObject; const PropertyName : string) : TValue;
|
|
function TRTTIJson.GetPropertyValueFromObject(Instance : TObject; const PropertyName : string) : TValue;
|
|
var
|
|
var
|
|
@@ -1156,6 +1159,7 @@ begin
|
|
end;
|
|
end;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
+{$IFDEF FPC}
|
|
procedure TRTTIJson.SetPropertyValue(Instance : TObject; const PropertyName : string; aValue : TValue);
|
|
procedure TRTTIJson.SetPropertyValue(Instance : TObject; const PropertyName : string; aValue : TValue);
|
|
var
|
|
var
|
|
pinfo : PPropInfo;
|
|
pinfo : PPropInfo;
|
|
@@ -1192,7 +1196,6 @@ begin
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
-{$IFDEF FPC}
|
|
|
|
procedure TRTTIJson.LoadSetProperty(aInstance : TObject; aPropInfo: PPropInfo; const aValue: string);
|
|
procedure TRTTIJson.LoadSetProperty(aInstance : TObject; aPropInfo: PPropInfo; const aValue: string);
|
|
type
|
|
type
|
|
TCardinalSet = set of 0..SizeOf(Cardinal) * 8 - 1;
|
|
TCardinalSet = set of 0..SizeOf(Cardinal) * 8 - 1;
|
|
@@ -1298,7 +1301,7 @@ begin
|
|
// end
|
|
// end
|
|
if propvalue.IsObject then jpair.JsonValue := SerializeObject(propvalue.AsObject)
|
|
if propvalue.IsObject then jpair.JsonValue := SerializeObject(propvalue.AsObject)
|
|
{$IFNDEF FPC}
|
|
{$IFNDEF FPC}
|
|
- else if (not propvalue.IsObject) and (IsGenericXArray(propvalue{$IFNDEF NEXTGEN}.TypeInfo.Name{$ELSE}.TypeInfo.NameFld.ToString{$ENDIF})) then
|
|
|
|
|
|
+ else if (not propvalue.IsObject) and (IsGenericXArray(string(propvalue{$IFNDEF NEXTGEN}.TypeInfo.Name{$ELSE}.TypeInfo.NameFld.ToString{$ENDIF}))) then
|
|
begin
|
|
begin
|
|
jpair.JsonValue := SerializeValue(GetFieldValueFromRecord(propvalue,'fArray'));
|
|
jpair.JsonValue := SerializeValue(GetFieldValueFromRecord(propvalue,'fArray'));
|
|
end
|
|
end
|
|
@@ -1336,10 +1339,12 @@ begin
|
|
TValue.Make(aAddr,aType.Handle,Result);
|
|
TValue.Make(aAddr,aType.Handle,Result);
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$IFDEF FPC}
|
|
function TRTTIJson.GetValue(aAddr: Pointer; aTypeInfo: PTypeInfo): TValue;
|
|
function TRTTIJson.GetValue(aAddr: Pointer; aTypeInfo: PTypeInfo): TValue;
|
|
begin
|
|
begin
|
|
TValue.Make(aAddr,aTypeInfo,Result);
|
|
TValue.Make(aAddr,aTypeInfo,Result);
|
|
end;
|
|
end;
|
|
|
|
+{$ENDIF}
|
|
|
|
|
|
function TRTTIJson.SerializeValue(const aValue : TValue) : TJSONValue;
|
|
function TRTTIJson.SerializeValue(const aValue : TValue) : TJSONValue;
|
|
begin
|
|
begin
|
|
@@ -1453,7 +1458,6 @@ end;
|
|
function TRTTIJson.SerializeStream(aObject: TObject): TJSONValue;
|
|
function TRTTIJson.SerializeStream(aObject: TObject): TJSONValue;
|
|
var
|
|
var
|
|
stream : TStream;
|
|
stream : TStream;
|
|
- json : string;
|
|
|
|
begin
|
|
begin
|
|
Result := nil;
|
|
Result := nil;
|
|
try
|
|
try
|