#1 2025-08-30 08:56:53

ertank
Member
Registered: 2016-03-16
Posts: 184

Possibility to skip some values as JSON serialization

Hello,

I have following record definition with Delphi 12.3 and mORMot2.

  TProductForUploadAttribute = packed record
    attributeId: Int64;
    attributeValueId: Variant;
    customAttributeValue: Variant;
  end;
  TProductForUpload = packed record
    attributes: TArray<TProductForUploadAttribute>;
  end;

The "customAttributeValue" is required optional like below by the 3rd party web service I am using.

   "attributes": [
        {
          "attributeId": 1,
          "attributeValueId": 7165
        },
        {
          "attributeId": 3,
          "attributeValueId": null,
          "customAttributeValue": "AÇIK TABA SÜET"
        }
   ]

When I serialize the record "customAttributeValue" is included in all array items. Is there a way to automatically exclude it like above? Maybe when it's value is Unassigned?

Thanks & Regards,
Ertan

Offline

#2 2025-08-30 10:48:07

tbo
Member
Registered: 2015-04-20
Posts: 368

Re: Possibility to skip some values as JSON serialization

  1. Check TTextWriterWriteObjectOption

  2. Permanent

    initialization
      Rtti.ByTypeInfo[TypeInfo(TProductForUploadAttribute )].Props.NameChange('customAttributeValue', '');
  3. With Custom-Serializer in this example.

With best regards
Thomas

Offline

#3 2025-08-31 03:18:56

lfyey121
Member
From: china
Registered: 2022-08-25
Posts: 77

Re: Possibility to skip some values as JSON serialization

TTextWriterWriteObjectOption-->  [woDontStoreVoid]

Offline

Board footer

Powered by FluxBB