Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting to Delphi & Path find with dot #19

Open
hafedh-trimeche opened this issue Aug 1, 2020 · 0 comments
Open

Porting to Delphi & Path find with dot #19

hafedh-trimeche opened this issue Aug 1, 2020 · 0 comments

Comments

@hafedh-trimeche
Copy link

Trying to compile JsonTools under Delphi 10.3 some warnings and hints are issued:

The TEnumConverter is used to handle enum types.

{$IF FPC}
{$mode delphi}
{$ENDIF}
[dcc32 Warning] jsontools.pas(326): W1050 WideChar reduced to byte char in set expressions.  Consider using 'CharInSet' function in 'SysUtils' unit.

[dcc32 Hint] jsontools.pas(944): H2443 Inline function 'TList.Remove' has not been expanded because unit 'System.Types' is not specified in USES list

[dcc32 Warning] jsontools.pas(978): W1035 Return value of function 'TJsonNode.Child' might be undefined

The Find function would be replaced by PathToObject to meet path access using (.) delimiter:

  TEnumConverter = class
  public
    class function ToStr<T>(AValue:T):string;
    class function FromStr<T>(AValue:string):T;
  end;

  TJsonData       = TJsonNode;
  TJsonDataHelper = class helper for TJsonData
  private
    function  GetString(const Path:string):string;
    procedure SetString(const Path:string;const AValue:string);
  public
    function  PathToObject(const PathOrIndex:Variant;const NodeKing:TJsonNodeKind=nkNull):TJsonData;
    procedure E_<T>(AName:string;AValue:T);overload;
    procedure _E<T>(AName:string;out AValue:T);overload;
 end;

class function TEnumConverter.ToStr<T>(AValue:T): string;
var
  IEnum : Integer;
begin
  IEnum := 0;
  Move(AValue,IEnum,SizeOf(T));
  Result := GetEnumName(TypeInfo(T),IEnum);
end;

class function TEnumConverter.FromStr<T>(AValue:string):T;
var
  IEnum : Integer;
begin
  IEnum := GetEnumValue(TypeInfo(T),AValue);
  Move(IEnum,Result,SizeOf(T));
end;

function TJsonDataHelper.PathToObject(const PathOrIndex:Variant;const NodeKing:TJsonNodeKind):TJsonData;
var
  sPath   : string;
  i       : Integer;
  Strings : TStringDynArray;
  LObject : TJsonData;
procedure AdjustPath(var Path:string);
begin
  Path := Trim(Path);
  if (Path<>'') and (Path[Length(Path)]='.') then SetLength(Path,Length(Path)-1);
end;
begin
  if VarType(PathOrIndex)=vtInteger then
  begin
    Result := Child(Integer(PathOrIndex)-1);
    Exit;
  end;
  Result := Self;
  sPath  := string(PathOrIndex);
  AdjustPath(sPath);
  if sPath='' then Exit;
  Strings := SplitString(sPath,'.');
  for i:=0 to Length(Strings)-1 do
  begin
    LObject := Result.Child(Strings[i]);
    if (LObject=nil) and (NodeKing<>nkNull) then LObject := Result.Add(Strings[i],NodeKing);
    Result := LObject;
    if Result=nil then Break;
  end;
end;

function TJsonDataHelper.GetString(const Path:string):string;
var
  JsonData : TJsonData;
begin
  Lock;
  JsonData := PathToObject(Path,nkNull);
  if Assigned(JsonData) then Result := JsonData.AsString
                        else Result := '';
  Unlock;
end;

procedure TJsonDataHelper.SetString(const Path:string;const AValue:string);
var
  JsonData : TJsonData;
begin
  Lock;
  JsonData := PathToObject(Path,nkString);
  if Assigned(JsonData) then JsonData.AsString := AValue;
  Unlock;
end;

procedure TJsonDataHelper.E_<T>(AName:string;AValue:T);
var
  sEnum :string;
begin
  sEnum := TEnumConverter.ToStr(AValue);
  SetString(AName,sEnum);
end;

procedure TJsonDataHelper._E<T>(AName:string;out AValue:T);
begin
  AValue := TEnumConverter.FromStr<T>(GetString(AName));
end;

@hafedh-trimeche hafedh-trimeche changed the title Poting to Delphi & Path find with dot Porting to Delphi & Path find with dot Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant