View Issue Details

IDProjectCategoryView StatusLast Update
0022273FPCFCLpublic2014-03-06 10:23
ReporterDenis Kozlov Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionno change required 
Product Version2.6.0 
Summary0022273: Control whether TJSONObject.GetElements raises Exception when element not found
DescriptionIn the latest mainstream version FPC 2.6.0 the JSON functionality has been modified in such a way that breaks existing applications.

TJSONObject.GetElements method is used for retrieving TJSONData by name. Prior to FPC 2.6.0, if the element was not found by the specified name the method would return NIL. In FPC 2.6.0, however, the method raises an Exception when this occurs.

Can I suggest adding an option (property) which will control this new behavior and will allow developers to switch off these exceptions. I understand that we can use IndexOfName method prior to getting the element, but this will require modification of very line of code where JSON elements are being used.

FPC 2.4.2:
----------------------------------------
function TJSONObject.GetElements(AName: string): TJSONData;
begin
  Result:=TJSONData(FHash.Find(AName));
end;
----------------------------------------

FPC 2.6.0:
----------------------------------------
function TJSONObject.GetElements(Const AName: string): TJSONData;
begin
  Result:=TJSONData(FHash.Find(AName));
  If (Result=Nil) then
    Raise EJSON.CreateFmt(SErrNonexistentElement,[AName]);
end;
----------------------------------------

Previously working code breaks in 2.6.0:
----------------------------------------
var
  J: TJSONObject;
  D: TJSONData;
begin
  J := TJSONObject.Create;
  D := J['dummy'];
  if D = nil then
    Write('Dummy does not exist...');
  else
    Write('Found the dummy...');
----------------------------------------
TagsException, GetElements, JSON, TJSONObject
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

Luiz Americo

2012-06-17 01:16

developer   ~0060591

In svn version you can use the Find method that does exactly what you want

Ludo Brands

2012-06-17 16:25

developer   ~0060600

2154 function TJSONObject.Find(const AName: String): TJSONData;
 2155
 2156 Var
 2157 I : Integer;
 2158
 2159 begin
 2160 I:=IndexOfName(AName);
 2161 If (I=-1) then <--- interesting !!!
 2162 Result:=Items[i]
 2163 else
 2164 Result:=Nil;
 2165 end;

Should be
If (I>-1) then

Makes also fail functions like TJSONObject.GetString, etc.

Isn't there a test for this functionality?

Marco van de Voort

2012-06-17 19:45

manager   ~0060601

Ludo: changed that to <>-1

Michael Van Canneyt

2012-07-11 18:55

administrator   ~0060983

The behaviour was changed intentionally.

In all other classes in the RTL/FCL, a Find function exists that returns Nil.
The Get function uses the Find function, and raises an exception when the result is Nil.

Issue History

Date Modified Username Field Change
2012-06-15 18:12 Denis Kozlov New Issue
2012-06-15 18:12 Denis Kozlov Tag Attached: Exception
2012-06-15 18:12 Denis Kozlov Tag Attached: GetElements
2012-06-15 18:12 Denis Kozlov Tag Attached: JSON
2012-06-15 18:12 Denis Kozlov Tag Attached: TJSONObject
2012-06-17 01:16 Luiz Americo Note Added: 0060591
2012-06-17 16:25 Ludo Brands Note Added: 0060600
2012-06-17 19:45 Marco van de Voort Note Added: 0060601
2012-07-11 18:55 Michael Van Canneyt Status new => resolved
2012-07-11 18:55 Michael Van Canneyt Resolution open => no change required
2012-07-11 18:55 Michael Van Canneyt Assigned To => Michael Van Canneyt
2012-07-11 18:55 Michael Van Canneyt Note Added: 0060983
2014-03-06 10:23 Michael Van Canneyt Status resolved => closed