#1 2018-12-11 12:40:11

magnushc
Member
Registered: 2018-12-11
Posts: 2

TDocVariant JSON parsing, Invalid variant type conversion in Delphi 6

I have a customer who is using Delphi 6 and needs to use JSON parsing.
(Yes it is very old and I don't know why he have to use this version)

The following code will display a message dialog showing "ABC" if run on Delphi 7.
If run in Delphi 6 I will only get "Invalid variant type conversion" exception.


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SynCommons, StdCtrls;

...

procedure TForm1.Button1Click(Sender: TObject);
var
  Test: variant;
begin
  TDocVariant.New(Test);
  try
    Test.param := 'ABC';

    // If breaking and inspecting Test here it will be '{"PARAM":"ABC"}' in Delphi 6 and '{"param":"ABC"}' in Delphi 7

    ShowMessage(Test.param);   // Exception here
  except
    on E: Exception do ShowMessage(E.Message);
  end;
end;

Offline

#2 2018-12-11 15:48:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,240
Website

Re: TDocVariant JSON parsing, Invalid variant type conversion in Delphi 6

This is a Delphi 6 bug... they put an UpperCase() in the RTL!

So if you want to properly handle it, write:

 _Safe(Test)^.U['param'] := 'ABC';

Online

#3 2018-12-11 16:44:31

magnushc
Member
Registered: 2018-12-11
Posts: 2

Re: TDocVariant JSON parsing, Invalid variant type conversion in Delphi 6

Thanks, it works great!

But maybe I'll try to convince them to use a newer Delphi version instead :-)

Offline

#4 2018-12-11 17:04:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,240
Website

Re: TDocVariant JSON parsing, Invalid variant type conversion in Delphi 6

At least switch to Delphi 7 could be pretty painless. wink

Online

Board footer

Powered by FluxBB