#1 2025-01-10 22:01:20

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

RecordSaveJson - custom format for TDateTime

Hello,

There are some web services insisting on "yyyy-mm-dd hh:nn:ss" format. Sometimes they even put only php format string "Y-m-d H:i:s" as their requirement.

Is it possible to have mORMot2 to serialize TDateTime values using a custom format?

Thanks & Regards,
Ertan

Offline

#2 2025-01-11 11:17:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,793
Website

Re: RecordSaveJson - custom format for TDateTime

You can overwrite the TDateTime format, but it would be global for the whole process.
IMHO this is not a good idea.

I would just use a RawUtf8 field with manual serialization instead.

Offline

#3 2025-01-18 09:21:01

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

Re: RecordSaveJson - custom format for TDateTime

I am returning back to this specific date time format.
I see it can be de-serialized successfully into TDateTime if the field name is "timestamp" like below

type
  TRec = packed record
    timestamp: TDateTime;
  end;

procedure TForm1.FormCreate(Sender: TObject);
const cData = '{"timestamp":"2024-08-02 21:02:13"}';
var LRec: TRec;
begin
  mormot.core.json.RecordLoadJson(LRec, RawUtf8(cData), TypeInfo(TRec));
end;

I wonder if it is possible to widen this for TDateTime definitions using any name?

Thank you for your consideration.

Regards,
Ertan

Offline

#4 2025-01-18 09:39:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,793
Website

Re: RecordSaveJson - custom format for TDateTime

What do you mean?
Which name are you talking about?
The field name has no impact at all. It uses the TDateTime type definition of the field, that's all.

Offline

#5 2025-01-18 10:21:00

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

Re: RecordSaveJson - custom format for TDateTime

ab wrote:

You can overwrite the TDateTime format, but it would be global for the whole process.

I don't know how I can do that?

Offline

#6 2025-01-18 17:48:19

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,793
Website

Re: RecordSaveJson - custom format for TDateTime

Define your own callbacks for TypeInfo(TDateTime).

Offline

Board footer

Powered by FluxBB