I am migrating some projects from Delphi 10.4 to Delphi 11.3.
I am facing some problems with the JSON library:
See the following codes:
First case:
function TestCreate;
var
LValue: Variant;
LJSON: TJSONNumber;
begin
LValue := 100;
LJSON := TJSONNumber.Create(LValue);
end;
Compiling this, it results in:
[dcc64 Error] _Unit.pas(74): E2251 Ambiguous overloaded call to 'Create'
System.JSON.pas(435): Related method: constructor TJSONNumber.Create(const string);
System.JSON.pas(439): Related method: constructor TJSONNumber.Create(const Double);
Second case:
function TestAddPair;
var
LValue: Variant;
LJSON: TJSONObject;
begin
LValue := 100;
LJSON := TJSONObject.Create();
LJSON.AddPair('Test', LValue);
end;
Compiling this, it results in:
[dcc64 Error] _Unit.pas(77): E2251 Ambiguous overloaded call to 'AddPair'
System.JSON.pas(661): Related method: function TJSONObject.AddPair(const string; const Boolean): TJSONObject;
System.JSON.pas(651): Related method: function TJSONObject.AddPair(const string; const Double): TJSONObject;
System.JSON.pas(636): Related method: function TJSONObject.AddPair(const string; const string): TJSONObject;
Both used to work properly on Delphi 10.4, but on Delphi 11.3 they do not compile.
Is there a way to let them compile? I’d prefer not to modify each command that uses a Variant to create/add a JSON.
2
Answers
A Variant can implicitly convert to both string or Double type without any preference for one of them.
In R104 the constructor taking a string parameter is protected, while in 11.3 it is public.
As Toon mentioned, you may not be able to avoid changing all the calls.
Update: Alternatively you can create class helpers for
TJSONNumber
andTJSONObject
with overrides for Variant. F.i. like this:Another alternative and different approach could be using mORMot, furthermore it’s very optimized to use as little memory and very fast JSON serialization / un-serialization.