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

ReadEnum should use TJSONBool #54

Closed
bm-q opened this issue Aug 20, 2022 · 1 comment
Closed

ReadEnum should use TJSONBool #54

bm-q opened this issue Aug 20, 2022 · 1 comment

Comments

@bm-q
Copy link

bm-q commented Aug 20, 2022

TNeonDeserializerJSON.ReadEnum should use TJSONBool instead of TJSONTrue and TJSONFalse.

type
  TClassWithBool = class
  private
    FBoolProp: Boolean;
  public
    property BoolProp: Boolean read FBoolProp write FBoolProp;
  end;

procedure TestBool;
var
  LJSON: TJSONObject;
  LReader: TNeonDeserializerJSON;
  LClass: TClassWithBool;
begin
  LClass := nil;
  LReader := nil;
  LJSON := TJSONObject.Create;
  try
    LJSON.AddPair('BoolProp', True);
    LReader := TNeonDeserializerJSON.Create(TNeonConfiguration.Default);
    LClass := TClassWithBool.Create;
    LReader.JSONToObject(LClass, LJSON);
    ShowMessage(LReader.Errors.Text); // => Error converting member [BoolProp] of type [TClassWithBool]: Invalid JSON value. Boolean expected
  finally
    LJSON.Free;
    LClass.Free;
    LReader.Free;
  end;
end;

Suggested changes

function TNeonDeserializerJSON.ReadEnum(const AParam: TNeonDeserializerParam): TValue;
var
  LIndex, LOrdinal: Integer;
  LTypeData: PTypeData;
begin
  if AParam.RttiType.Handle = System.TypeInfo(Boolean) then
  begin
    if AParam.JSONValue is TJSONBool then
      Result := (AParam.JSONValue as TJSONBool).AsBoolean
    else
      raise ENeonException.Create('Invalid JSON value. Boolean expected');
  end

[...]
@paolo-rossi
Copy link
Owner

Thanks @bm-q

resolved in the last commit

Ciao,
Paolo.

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

2 participants