XMLRPC/SOAP serializers: Better Boolean handling, fix Delphi bug

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@855 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
inoussa
2009-06-18 17:03:43 +00:00
parent 2738707f90
commit b0d48481ec
4 changed files with 179 additions and 96 deletions

View File

@ -1822,9 +1822,7 @@ procedure TSOAPBaseFormatter.PutScopeInnerValue(
); );
Var Var
int64SData : Int64; int64SData : Int64;
{$IFDEF FPC}
boolData : Boolean; boolData : Boolean;
{$ENDIF FPC}
{$IFDEF HAS_QWORD} {$IFDEF HAS_QWORD}
uint64Data : QWord; uint64Data : QWord;
{$ENDIF HAS_QWORD} {$ENDIF HAS_QWORD}
@ -1905,6 +1903,12 @@ begin
end; end;
tkEnumeration : tkEnumeration :
begin begin
{$IFDEF WST_DELPHI}
if ( GetTypeData(ATypeInfo)^.BaseType^ = TypeInfo(Boolean) ) then begin
boolData := Boolean(AData);
dataBuffer := BoolToSoapBool(boolData);
end else begin
{$ENDIF}
enumData := 0; enumData := 0;
case GetTypeData(ATypeInfo)^.OrdType of case GetTypeData(ATypeInfo)^.OrdType of
otSByte : enumData := ShortInt(AData); otSByte : enumData := ShortInt(AData);
@ -1915,6 +1919,9 @@ begin
otULong : enumData := LongWord(AData); otULong : enumData := LongWord(AData);
end; end;
dataBuffer := GetTypeRegistry().ItemByTypeInfo[ATypeInfo].GetExternalPropertyName(GetEnumName(ATypeInfo,enumData)) dataBuffer := GetTypeRegistry().ItemByTypeInfo[ATypeInfo].GetExternalPropertyName(GetEnumName(ATypeInfo,enumData))
{$IFDEF WST_DELPHI}
end;
{$ENDIF}
end; end;
tkFloat : tkFloat :
begin begin
@ -2132,6 +2139,17 @@ begin
{$ENDIF} {$ENDIF}
tkInteger, tkEnumeration : tkInteger, tkEnumeration :
begin begin
{$IFDEF WST_DELPHI}
if ( ATypeInfo^.Kind = tkEnumeration ) and
( GetTypeData(ATypeInfo)^.BaseType^ = TypeInfo(Boolean) )
then begin
dataBuffer := LowerCase(Trim(dataBuffer));
if IsStrEmpty(dataBuffer) then
Boolean(AData) := False
else
Boolean(AData) := StrToBool(dataBuffer);
end else begin
{$ENDIF}
if ( ATypeInfo^.Kind = tkInteger ) then if ( ATypeInfo^.Kind = tkInteger ) then
enumData := StrToInt64Def(Trim(dataBuffer),0) enumData := StrToInt64Def(Trim(dataBuffer),0)
else else
@ -2144,6 +2162,9 @@ begin
otSLong : LongInt(AData) := enumData; otSLong : LongInt(AData) := enumData;
otULong : LongWord(AData) := enumData; otULong : LongWord(AData) := enumData;
end; end;
{$IFDEF WST_DELPHI}
end;
{$ENDIF}
end; end;
tkFloat : tkFloat :
begin begin

View File

@ -41,6 +41,9 @@ const
sPARAMS = 'params'; sPARAMS = 'params';
sVALUE = 'value'; sVALUE = 'value';
XML_RPC_FALSE = '0';
XML_RPC_TRUE = '1';
type type
TwstXMLDocument = {$IFNDEF FPC}wst_delphi_xml.TXMLDocument{$ELSE}TXMLDocument{$ENDIF}; TwstXMLDocument = {$IFNDEF FPC}wst_delphi_xml.TXMLDocument{$ELSE}TXMLDocument{$ENDIF};
@ -179,13 +182,11 @@ type
Const ATypeInfo : PTypeInfo; Const ATypeInfo : PTypeInfo;
Const AData : TEnumIntType Const AData : TEnumIntType
):TDOMNode;{$IFDEF USE_INLINE}inline;{$ENDIF} ):TDOMNode;{$IFDEF USE_INLINE}inline;{$ENDIF}
{$IFDEF FPC}
function PutBool( function PutBool(
Const AName : String; Const AName : String;
Const ATypeInfo : PTypeInfo; Const ATypeInfo : PTypeInfo;
Const AData : Boolean Const AData : Boolean
):TDOMNode;{$IFDEF USE_INLINE}inline;{$ENDIF} ):TDOMNode;{$IFDEF USE_INLINE}inline;{$ENDIF}
{$ENDIF}
function PutAnsiChar( function PutAnsiChar(
Const AName : String; Const AName : String;
Const ATypeInfo : PTypeInfo; Const ATypeInfo : PTypeInfo;
@ -257,7 +258,6 @@ type
Var AName : String; Var AName : String;
Var AData : WideChar Var AData : WideChar
);{$IFDEF USE_INLINE}inline;{$ENDIF} );{$IFDEF USE_INLINE}inline;{$ENDIF}
{$IFDEF FPC}
procedure GetBool( procedure GetBool(
Const ATypeInfo : PTypeInfo; Const ATypeInfo : PTypeInfo;
Var AName : String; Var AName : String;
@ -268,7 +268,6 @@ type
Var AName : String; Var AName : String;
Var AData : Integer Var AData : Integer
); );
{$ENDIF}
procedure GetInt64( procedure GetInt64(
Const ATypeInfo : PTypeInfo; Const ATypeInfo : PTypeInfo;
Var AName : String; Var AName : String;
@ -828,7 +827,6 @@ begin
); );
end; end;
{$IFDEF FPC}
function TXmlRpcBaseFormatter.PutBool( function TXmlRpcBaseFormatter.PutBool(
const AName : String; const AName : String;
const ATypeInfo : PTypeInfo; const ATypeInfo : PTypeInfo;
@ -838,12 +836,11 @@ var
v : Char; v : Char;
begin begin
if AData then if AData then
v := '1' v := XML_RPC_TRUE
else else
v := '0'; v := XML_RPC_FALSE;
Result := InternalPutData(AName,xdtBoolean,v); Result := InternalPutData(AName,xdtBoolean,v);
end; end;
{$ENDIF}
function TXmlRpcBaseFormatter.PutAnsiChar( function TXmlRpcBaseFormatter.PutAnsiChar(
const AName: String; const AName: String;
@ -988,20 +985,13 @@ begin
AData := GetEnumValue(ATypeInfo,locBuffer) AData := GetEnumValue(ATypeInfo,locBuffer)
End; End;
{$IFDEF FPC}
procedure TXmlRpcBaseFormatter.GetBool( procedure TXmlRpcBaseFormatter.GetBool(
const ATypeInfo : PTypeInfo; const ATypeInfo : PTypeInfo;
var AName : String; var AName : String;
var AData : Boolean var AData : Boolean
); );
Var
locBuffer : String;
begin begin
locBuffer := LowerCase(Trim(GetNodeValue(AName))); AData := ( GetNodeValue(AName) = XML_RPC_TRUE );
If IsStrEmpty(locBuffer) Then
AData := False
Else
AData := StrToBool(locBuffer);
end; end;
procedure TXmlRpcBaseFormatter.GetInt( procedure TXmlRpcBaseFormatter.GetInt(
@ -1012,7 +1002,6 @@ procedure TXmlRpcBaseFormatter.GetInt(
begin begin
AData := StrToIntDef(Trim(GetNodeValue(AName)),0); AData := StrToIntDef(Trim(GetNodeValue(AName)),0);
end; end;
{$ENDIF}
procedure TXmlRpcBaseFormatter.GetAnsiChar( procedure TXmlRpcBaseFormatter.GetAnsiChar(
const ATypeInfo: PTypeInfo; const ATypeInfo: PTypeInfo;
@ -1299,7 +1288,7 @@ Var
{$ENDIF HAS_QWORD} {$ENDIF HAS_QWORD}
strData : string; strData : string;
objData : TObject; objData : TObject;
{$IFDEF FPC}boolData : Boolean;{$ENDIF} boolData : Boolean;
enumData : TEnumIntType; enumData : TEnumIntType;
floatDt : Extended; floatDt : Extended;
{$IFDEF WST_UNICODESTRING} {$IFDEF WST_UNICODESTRING}
@ -1367,6 +1356,14 @@ begin
{$ENDIF} {$ENDIF}
tkInteger, tkEnumeration : tkInteger, tkEnumeration :
Begin Begin
{$IFDEF WST_DELPHI}
if ( ATypeInfo^.Kind = tkEnumeration ) and
( GetTypeData(ATypeInfo)^.BaseType^ = TypeInfo(Boolean) )
then begin
boolData := Boolean(AData);
PutBool(AName,ATypeInfo,boolData);
end else begin
{$ENDIF}
enumData := 0; enumData := 0;
Case GetTypeData(ATypeInfo)^.OrdType Of Case GetTypeData(ATypeInfo)^.OrdType Of
otSByte : enumData := ShortInt(AData); otSByte : enumData := ShortInt(AData);
@ -1380,6 +1377,9 @@ begin
PutInt64(AName,ATypeInfo,enumData) PutInt64(AName,ATypeInfo,enumData)
Else Else
PutEnum(AName,ATypeInfo,enumData); PutEnum(AName,ATypeInfo,enumData);
{$IFDEF WST_DELPHI}
end;
{$ENDIF}
End; End;
tkFloat : tkFloat :
Begin Begin
@ -1414,9 +1414,7 @@ Var
{$IFDEF HAS_QWORD} {$IFDEF HAS_QWORD}
uint64Data : QWord; uint64Data : QWord;
{$ENDIF HAS_QWORD} {$ENDIF HAS_QWORD}
{$IFDEF FPC}
boolData : Boolean; boolData : Boolean;
{$ENDIF}
strData : string; strData : string;
enumData : TEnumIntType; enumData : TEnumIntType;
floatDt : Extended; floatDt : Extended;
@ -1477,7 +1475,10 @@ begin
tkBool : tkBool :
begin begin
boolData := Boolean(AData); boolData := Boolean(AData);
dataBuffer := BoolToStr(boolData); if boolData then
dataBuffer := XML_RPC_TRUE
else
dataBuffer := XML_RPC_FALSE;
end; end;
{$ENDIF} {$ENDIF}
tkInteger : tkInteger :
@ -1494,6 +1495,15 @@ begin
end; end;
tkEnumeration : tkEnumeration :
begin begin
{$IFDEF WST_DELPHI}
if ( GetTypeData(ATypeInfo)^.BaseType^ = TypeInfo(Boolean) ) then begin
boolData := Boolean(AData);
if boolData then
dataBuffer := XML_RPC_TRUE
else
dataBuffer := XML_RPC_FALSE;
end else begin
{$ENDIF}
enumData := 0; enumData := 0;
case GetTypeData(ATypeInfo)^.OrdType of case GetTypeData(ATypeInfo)^.OrdType of
otSByte : enumData := ShortInt(AData); otSByte : enumData := ShortInt(AData);
@ -1504,6 +1514,9 @@ begin
otULong : enumData := LongWord(AData); otULong : enumData := LongWord(AData);
end; end;
dataBuffer := GetTypeRegistry().ItemByTypeInfo[ATypeInfo].GetExternalPropertyName(GetEnumName(ATypeInfo,enumData)) dataBuffer := GetTypeRegistry().ItemByTypeInfo[ATypeInfo].GetExternalPropertyName(GetEnumName(ATypeInfo,enumData))
{$IFDEF WST_DELPHI}
end;
{$ENDIF}
end; end;
tkFloat : tkFloat :
begin begin
@ -1533,7 +1546,7 @@ Var
{$ENDIF HAS_QWORD} {$ENDIF HAS_QWORD}
strData : string; strData : string;
objData : TObject; objData : TObject;
{$IFDEF FPC}boolData : Boolean;{$ENDIF} boolData : Boolean;
enumData : TEnumIntType; enumData : TEnumIntType;
floatDt : Extended; floatDt : Extended;
recObject : Pointer; recObject : Pointer;
@ -1612,6 +1625,15 @@ begin
{$ENDIF} {$ENDIF}
tkInteger, tkEnumeration : tkInteger, tkEnumeration :
Begin Begin
{$IFDEF WST_DELPHI}
if ( ATypeInfo^.Kind = tkEnumeration ) and
( GetTypeData(ATypeInfo)^.BaseType^ = TypeInfo(Boolean) )
then begin
boolData := False;
GetBool(ATypeInfo,AName,boolData);
Boolean(AData) := boolData;
end else begin
{$ENDIF}
enumData := 0; enumData := 0;
If ( ATypeInfo^.Kind = tkInteger ) Then If ( ATypeInfo^.Kind = tkInteger ) Then
GetInt64(ATypeInfo,AName,enumData) GetInt64(ATypeInfo,AName,enumData)
@ -1625,6 +1647,9 @@ begin
otSLong : LongInt(AData) := enumData; otSLong : LongInt(AData) := enumData;
otULong : LongWord(AData) := enumData; otULong : LongWord(AData) := enumData;
End; End;
{$IFDEF WST_DELPHI}
end;
{$ENDIF}
End; End;
tkFloat : tkFloat :
Begin Begin
@ -1700,15 +1725,18 @@ begin
{$IFDEF FPC} {$IFDEF FPC}
tkBool : tkBool :
begin begin
dataBuffer := LowerCase(Trim(dataBuffer)); Boolean(AData) := ( dataBuffer = XML_RPC_TRUE );
if IsStrEmpty(dataBuffer) then
Boolean(AData) := False
else
Boolean(AData) := StrToBool(dataBuffer);
end; end;
{$ENDIF} {$ENDIF}
tkInteger, tkEnumeration : tkInteger, tkEnumeration :
begin begin
{$IFDEF WST_DELPHI}
if ( ATypeInfo^.Kind = tkEnumeration ) and
( GetTypeData(ATypeInfo)^.BaseType^ = TypeInfo(Boolean) )
then begin
Boolean(AData) := ( dataBuffer = XML_RPC_TRUE );
end else begin
{$ENDIF}
if ( ATypeInfo^.Kind = tkInteger ) then if ( ATypeInfo^.Kind = tkInteger ) then
enumData := StrToInt64Def(Trim(dataBuffer),0) enumData := StrToInt64Def(Trim(dataBuffer),0)
else else
@ -1721,6 +1749,9 @@ begin
otSLong : LongInt(AData) := enumData; otSLong : LongInt(AData) := enumData;
otULong : LongWord(AData) := enumData; otULong : LongWord(AData) := enumData;
end; end;
{$IFDEF WST_DELPHI}
end;
{$ENDIF}
end; end;
tkFloat : tkFloat :
begin begin

View File

@ -16,7 +16,7 @@ unit object_serializer;
interface interface
uses uses
Classes, SysUtils, TypInfo, Contnrs, SyncObjs, Classes, SysUtils, TypInfo, Contnrs,
base_service_intf, wst_types; base_service_intf, wst_types;
type type

View File

@ -2261,6 +2261,37 @@ begin
CheckEquals(True,a.Val_Bool); CheckEquals(True,a.Val_Bool);
CheckEquals(Ord(teThree),Ord(a.Val_Enum)); CheckEquals(Ord(teThree),Ord(a.Val_Enum));
CheckEquals('atou',a.Val_String); CheckEquals('atou',a.Val_String);
//------------------------------------
FreeAndNil(a);
a := TClass_Enum.Create();
a.Val_Bool := False;
a.Val_Enum := teTwo;
a.Val_String := 'atoukws';
f := CreateFormatter(TypeInfo(TClass_Enum));
f.BeginObject('Root',TypeInfo(TClass_Enum));
f.Put('o1',TypeInfo(TClass_Enum),a);
f.EndScope();
s.Clear();
f.SaveToStream(s);
FreeAndNil(a);
a := TClass_Enum.Create();
f := CreateFormatter(TypeInfo(TClass_Enum));
s.Position := 0;
f.LoadFromStream(s);
x := 'Root';
f.BeginObjectRead(x,TypeInfo(TClass_Enum));
x := 'o1';
f.Get(TypeInfo(TClass_Enum),x,a);
f.EndScopeRead();
CheckEquals(False,a.Val_Bool);
CheckEquals(Ord(teTwo),Ord(a.Val_Enum));
CheckEquals('atoukws',a.Val_String);
Finally Finally
a.Free(); a.Free();
s.Free(); s.Free();