* Applied patch from Luiz Americo to make set functions return previous value (bug ID 25795)

git-svn-id: trunk@26940 -
This commit is contained in:
michael 2014-03-03 14:13:00 +00:00
parent 54e926fdb5
commit 2ae138d95c
4 changed files with 23 additions and 18 deletions

View File

@ -69,7 +69,7 @@ Type
TJSONData = class(TObject) TJSONData = class(TObject)
protected protected
Class Procedure DoError(Const Msg : String); Class Procedure DoError(Const Msg : String);
Class Procedure DoError(Const Fmt : String; Args : Array of const); Class Procedure DoError(Const Fmt : String; const Args : Array of const);
Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; virtual; Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; virtual;
function GetAsBoolean: Boolean; virtual; abstract; function GetAsBoolean: Boolean; virtual; abstract;
function GetAsFloat: TJSONFloat; virtual; abstract; function GetAsFloat: TJSONFloat; virtual; abstract;
@ -480,7 +480,7 @@ Type
TJSONParserHandler = Procedure(AStream : TStream; Const AUseUTF8 : Boolean; Out Data : TJSONData); TJSONParserHandler = Procedure(AStream : TStream; Const AUseUTF8 : Boolean; Out Data : TJSONData);
Procedure SetJSONInstanceType(AType : TJSONInstanceType; AClass : TJSONDataClass); Function SetJSONInstanceType(AType : TJSONInstanceType; AClass : TJSONDataClass) : TJSONDataClass;
Function GetJSONInstanceType(AType : TJSONInstanceType) : TJSONDataClass; Function GetJSONInstanceType(AType : TJSONInstanceType) : TJSONDataClass;
Function StringToJSONString(const S : TJSONStringType) : TJSONStringType; Function StringToJSONString(const S : TJSONStringType) : TJSONStringType;
@ -501,7 +501,7 @@ Function CreateJSONObject(Data : Array of const) : TJSONObject;
// When the jsonparser unit is included in the project, the callback is automatically set. // When the jsonparser unit is included in the project, the callback is automatically set.
Function GetJSON(Const JSON : TJSONStringType; Const UseUTF8 : Boolean = True) : TJSONData; Function GetJSON(Const JSON : TJSONStringType; Const UseUTF8 : Boolean = True) : TJSONData;
Function GetJSON(Const JSON : TStream; Const UseUTF8 : Boolean = True) : TJSONData; Function GetJSON(Const JSON : TStream; Const UseUTF8 : Boolean = True) : TJSONData;
Procedure SetJSONParserHandler(AHandler : TJSONParserHandler); Function SetJSONParserHandler(AHandler : TJSONParserHandler) : TJSONParserHandler;
Function GetJSONParserHandler : TJSONParserHandler; Function GetJSONParserHandler : TJSONParserHandler;
implementation implementation
@ -541,12 +541,13 @@ Const
TJSONInt64Number,TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray, TJSONInt64Number,TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray,
TJSONObject); TJSONObject);
procedure SetJSONInstanceType(AType: TJSONInstanceType; AClass: TJSONDataClass); function SetJSONInstanceType(AType: TJSONInstanceType; AClass: TJSONDataClass): TJSONDataClass;
begin begin
if AClass=Nil then if AClass=Nil then
TJSONData.DoError(SErrWrongInstanceClass,['Nil',MinJSONINstanceTypes[AType].ClassName]); TJSONData.DoError(SErrWrongInstanceClass,['Nil',MinJSONInstanceTypes[AType].ClassName]);
if Not AClass.InheritsFrom(MinJSONINstanceTypes[AType]) then if Not AClass.InheritsFrom(MinJSONINstanceTypes[AType]) then
TJSONData.DoError(SErrWrongInstanceClass,[AClass.ClassName,MinJSONINstanceTypes[AType].ClassName]); TJSONData.DoError(SErrWrongInstanceClass,[AClass.ClassName,MinJSONInstanceTypes[AType].ClassName]);
Result:=DefaultJSONInstanceTypes[AType];
DefaultJSONINstanceTypes[AType]:=AClass; DefaultJSONINstanceTypes[AType]:=AClass;
end; end;
@ -706,8 +707,9 @@ begin
JPH(JSON,UseUTF8,Result); JPH(JSON,UseUTF8,Result);
end; end;
procedure SetJSONParserHandler(AHandler: TJSONParserHandler); function SetJSONParserHandler(AHandler: TJSONParserHandler): TJSONParserHandler;
begin begin
Result:=JPH;
JPH:=AHandler; JPH:=AHandler;
end; end;
@ -837,7 +839,7 @@ begin
Raise EJSON.Create(Msg); Raise EJSON.Create(Msg);
end; end;
class procedure TJSONData.DoError(const Fmt: String; Args: array of const); class procedure TJSONData.DoError(const Fmt: String; Const Args: array of const);
begin begin
Raise EJSON.CreateFmt(Fmt,Args); Raise EJSON.CreateFmt(Fmt,Args);
end; end;

View File

@ -1,4 +1,4 @@
<?xml version="1.0"?> <?xml version="1.0" encoding="UTF-8"?>
<CONFIG> <CONFIG>
<ProjectOptions> <ProjectOptions>
<Version Value="9"/> <Version Value="9"/>

View File

@ -989,13 +989,20 @@ end;
{ TTestJSON } { TTestJSON }
procedure TTestJSON.SetDefaultInstanceTypes;
Const Const
DefJSONInstanceTypes : DefJSONInstanceTypes :
Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TJSONIntegerNumber, Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TJSONIntegerNumber,
TJSONInt64Number,TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray, TJSONInt64Number,TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray,
TJSONObject); TJSONObject);
Const
MyJSONInstanceTypes :
Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TMyInteger,
TMyInt64,TMyFloat, TMyString, TMyBoolean, TMyNull, TMyArray,
TMyObject);
procedure TTestJSON.SetDefaultInstanceTypes;
Var Var
Ti : TJSONInstanceType; Ti : TJSONInstanceType;
@ -1005,17 +1012,13 @@ begin
end; end;
procedure TTestJSON.SetMyInstanceTypes; procedure TTestJSON.SetMyInstanceTypes;
Const
MyJSONInstanceTypes :
Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TMyInteger,
TMyInt64,TMyFloat, TMyString, TMyBoolean, TMyNull, TMyArray,
TMyObject);
Var Var
Ti : TJSONInstanceType; Ti : TJSONInstanceType;
begin begin
For ti:=Low(TJSONInstanceType) to High(TJSONInstanceType) do For ti:=Low(TJSONInstanceType) to High(TJSONInstanceType) do
SetJSONInstanceType(Ti,MyJSONInstanceTypes[ti]); AssertEquals('Previous value is returned by SetJSONInstanceType',DefJSONInstanceTypes[ti],SetJSONInstanceType(Ti,MyJSONInstanceTypes[ti]));
end; end;
procedure TTestJSON.SetUp; procedure TTestJSON.SetUp;

View File

@ -406,7 +406,7 @@ Var
begin begin
H:=GetJSONParserHandler; H:=GetJSONParserHandler;
try try
SetJSONParserHandler(Nil); AssertSame('SetJSONParserHandler returns previous handler',H,SetJSONParserHandler(Nil));
AssertException('No handler raises exception',EJSON,@CallNoHandler); AssertException('No handler raises exception',EJSON,@CallNoHandler);
AssertException('No handler raises exception',EJSON,@CallNoHandlerStream); AssertException('No handler raises exception',EJSON,@CallNoHandlerStream);
finally finally