Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#7

AW: JSONPair mit Extended oder Integer

  Alt 18. Jan 2016, 16:54
Also mit ein bisserl gutem Willen und Spucke (class helper) bekommt man folgendes hin:
Delphi-Quellcode:
procedure Test1;
var
  lJson: TJsonObject;
begin
  lJson := TJsonObject.Create;
  lJson.AddPair( TJsonPair.Create( 'int', 42 ) );
  lJson.AddPair( 'double', 42.01 );
  lJson.AddPair( 'bool-a', false );
  lJson.AddPair( 'bool-b', true );

  lJson['foo'] := 'bar';

  WriteLn(lJson.ToJSON);
end;
Code:
{"int":42,"double":42.01,"bool-a":false,"bool-b":true,"str":"str","foo":"bar"}
der Class Helper dazu sieht dann so aus:
Delphi-Quellcode:
type
  TJsonValueRecord = record
  private
    FBuilder: TFunc<TJsonValue>;
  public
    class operator implicit( const i: Integer ): TJsonValueRecord;
    class operator implicit( const i: Int64 ): TJsonValueRecord;
    class operator implicit( const d: double ): TJsonValueRecord;
    class operator implicit( const b: Boolean ): TJsonValueRecord;
    class operator implicit( const s: string ): TJsonValueRecord;
    function BuildJsonValue: TJsonValue;
  end;

  HelperForJsonPair = class helper for TJsonPair
    constructor Create( const Str: string; const Value: TJsonValueRecord ); overload;
  end;

  HelperForJsonObject = class helper for TJsonObject
  private
    procedure SetValue( const AName:string; const Value:TJsonValueRecord );
  public
    procedure AddPair( const Str: string; const Value: TJsonValueRecord ); overload;
    property Values[const AName:string] : TJsonValueRecord write SetValue; default;
  end;

implementation

{ HelperForJsonPair }

constructor HelperForJsonPair.Create( const Str: string; const Value: TJsonValueRecord );
begin
  Create( Str, Value.BuildJsonValue );
end;

{ TJsonValueRecord }

function TJsonValueRecord.BuildJsonValue: TJsonValue;
begin
  Result := FBuilder( );
end;

class operator TJsonValueRecord.implicit( const i: Integer ): TJsonValueRecord;
begin
  Result.FBuilder :=
    function( ): TJsonValue
    begin
      Result := TJSONNumber.Create( i );
    end;
end;

class operator TJsonValueRecord.implicit( const i: Int64 ): TJsonValueRecord;
begin
  Result.FBuilder :=
    function( ): TJsonValue
    begin
      Result := TJSONNumber.Create( i );
    end;
end;

class operator TJsonValueRecord.implicit( const d: double ): TJsonValueRecord;
begin
  Result.FBuilder :=
    function( ): TJsonValue
    begin
      Result := TJSONNumber.Create( d );
    end;
end;

class operator TJsonValueRecord.implicit( const b: Boolean ): TJsonValueRecord;
begin
  Result.FBuilder :=
    function( ): TJsonValue
    begin
      if b
      then
        Result := TJSONTrue.Create
      else
        Result := TJsonFalse.Create;
    end;
end;

class operator TJsonValueRecord.implicit(const s: string): TJsonValueRecord;
begin
  Result.FBuilder :=
    function( ): TJsonValue
    begin
      Result := TJSONString.Create( s );
    end;
end;

{ HelperForJsonObject }

procedure HelperForJsonObject.AddPair( const Str: string;
  const Value: TJsonValueRecord );
begin
  AddPair( TJsonPair.Create( Str, Value ) );
end;

procedure HelperForJsonObject.SetValue(const AName: string;
  const Value: TJsonValueRecord);
begin
  AddPair( AName, Value );
end;
P.S. Ja, ich bin auch der Meinung, die JSON-Bibliothek hätte das so oder anders gleich schon von Haus aus mitbringen sollen
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo (18. Jan 2016 um 17:19 Uhr)
  Mit Zitat antworten Zitat