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

JSONRPC Nullable* wrong deserialization #648

Closed
sf-spb opened this issue Mar 12, 2023 · 0 comments
Closed

JSONRPC Nullable* wrong deserialization #648

sf-spb opened this issue Mar 12, 2023 · 0 comments
Assignees
Labels
accepted Issue has been accepted and inserted in a future milestone

Comments

@sf-spb
Copy link

sf-spb commented Mar 12, 2023

Hi, Daniele,

If you try to deserialize JSON like this:

"jsonrpc": "2.0",
"id": 14,
"result": [
    {
        "id": 155,
        "added": "2011-08-05T17:48:41.000+04:00",
        "name": "Jhon",
        "expirationDate": "2021-12-31",
        "maxUpdateDate": null,
        "appVersion": null,
        "activated": null
    }]

into

TCustomer = record
  Id: NullableInt32;
  Added: TDateTime;
  Name: NullableString;
  ExpirationDate: NullableTDate;
  MaxUpdateDate: NullableTDate;
  AppVersion: NullableString;
  Activated: NullableTDateTime;
end;

you will get an exception: Project raised exception class EJsonCastException with message 'Cannot cast Integer into Object'.
Solution: modify function TMVCJsonDataObjectsSerializer.JSONObjectToRecord in MVCFramework.Serializer.JsonDataObjects.pas.

Before:

lKeyName := TMVCSerializerHelper.GetKeyName(lField, lRTTIType);
if lField.FieldType.IsRecord then
begin
  JSONObjectToNestedRecordField(JSONObject.O[lKeyName], lField, 0, lBuffer);
end

after

lKeyName := TMVCSerializerHelper.GetKeyName(lField, lRTTIType);
if lField.FieldType.IsRecord and not lField.FieldType.Handle.NameFld.ToString.StartsWith('Nullable') then
begin
  JSONObjectToNestedRecordField(JSONObject.O[lKeyName], lField, 0, lBuffer);
end
@danieleteti danieleteti added the accepted Issue has been accepted and inserted in a future milestone label Mar 13, 2023
@danieleteti danieleteti self-assigned this Mar 13, 2023
@danieleteti danieleteti added this to the 3.3.0-fluorine milestone Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Issue has been accepted and inserted in a future milestone
Projects
None yet
Development

No branches or pull requests

2 participants