Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store Array of Record in JSON

How can an array of record be stored in JSON via SuperObject library. For example..

type
  TData = record
    str: string;
    int: Integer;
    bool: Boolean;
    flt: Double;
  end;

var
DataArray: Array[0..100] of TData;
like image 872
user299323 Avatar asked Jan 18 '26 07:01

user299323


1 Answers

Just use the superobject Marshalling TSuperRTTIContext

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  superobject,
  System.SysUtils;

type
  TData = record
    str : string;
    int : Integer;
    bool : Boolean;
    flt : Double;
  end;

  TDataArray = Array [0 .. 100] of TData;

procedure Test;
var
  DataArray : TDataArray;
  so :        ISuperObject;
  ctx :       TSuperRttiContext;
begin
  ctx := TSuperRttiContext.Create;
  try
    so := ctx.AsJson<TDataArray>( DataArray );
  finally
    ctx.Free;
  end;
  Writeln( so.AsJson );
end;

begin
  try
    Test;
  except
    on E : Exception do
      Writeln( E.ClassName, ': ', E.Message );
  end;

  ReadLn;

end.
like image 144
Sir Rufo Avatar answered Jan 21 '26 02:01

Sir Rufo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!