danieleteti / delphimvcframework

DMVCFramework (for short) is a popular and powerful framework for WEB API in Delphi. Supports RESTful and JSON-RPC WEB APIs development.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSONRPC Nullable* wrong deserialization

sf-spb opened this issue · comments

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