type
TRecordEnum = (reOne, reTwo, reLast);
type // define some record types, used as properties below
TTestCustomJSONArraySimpleArray = record
F: string;
G: array of string;
H: record
H1: integer;
H2: string;
H3: record
H3a: boolean;
H3b: String;
end;
end;
I: TDateTime;
J: array of record
J1: Integer;
J2: String;
J3: TRecordEnum;
end;
end;
type
/// which kind of document the TJSONVariantData contains
TJSONVariantKind = (jvUndefined, jvObject, jvArray);
/// convert a text or integer enumeration representation into its ordinal value
function VariantToEnum(const Value: variant; const TextValues: array of string): integer;
var str: string;
begin
// {$ifdef ISSMS}
if TVariant.IsNumber(Value) then
// {$else}
// if VarIsOrdinal(Value) then // Value is integer
// {$endif}
result := Value else begin
str := Value;
if str<>'' then
for result := 0 to high(TextValues) do
if str=TextValues[result] then
exit;
result := 0; // return first item by default
end;
end;
function VariantType(const Value: variant): TJSONVariantKind;
begin
asm
if (@Value === null) return 0;
if (typeof(@Value) !== "object") return 0;
if (Object.prototype.toString.call(@Value) === "[object Array]") return 2;
return 1;
end;
end;
function Variant2TRecordEnum(const _variant: variant): TRecordEnum;
begin
asm return @VariantToEnum(@_variant,@TRecordEnum); end;
end;
function Variant2TTestCustomJSONArraySimpleArray(const Value: variant): TTestCustomJSONArraySimpleArray;
begin
result.F := Value.F;
if VariantType(Value.G)=jvArray then
for var i := 0 to integer(Value.G.length)-1 do
result.G.Add(string(Value.G[i]));
result.H.H1 := Value.H.H1;
result.H.H2 := Value.H.H2;
result.H.H3.H3a := Value.H.H3.H3a;
result.H.H3.H3b := (Value.H.H3.H3b);
result.I := Iso8601ToDateTime(Value.I);
if VariantType(Value.J)=jvArray then begin
var tmp: TTestCustomJSONArraySimpleArray;
tmp.J.SetLength(1);
for var n := 0 to integer(Value.J.length)-1 do begin
var source := Value.J[n];
var dest := tmp.J[0];
dest.J1 := source.J1;
dest.J2 := (source.J2);
dest.J3 := Variant2TRecordEnum(source.J3);
result.J.Add(dest);
end;
end;
end;
procedure TForm1.W3Button4Click(Sender: TObject);
var
abc : TTestCustomJSONArraySimpleArray;
begin
abc.F := 'warleyalex';
abc.G := ['Delphi','Java','C++'];
abc.H.H1 := 123;
abc.H.H2 := 'Jesus Cristo';
abc.H.H3.H3a := True;
abc.H.H3.H3b := 'Smart Mobile Studio';
abc.I := now();
WriteLn(abc);
end;
|