#1 2021-10-31 08:00:30

srp
Member
Registered: 2014-09-02
Posts: 11

How to do Custom Serialization in Mormot 2 ?

I am having custom Record 

const
  __TRECApplicationSetting= 'Port:integer;RootURI:RawUTF8;AutoStart:Boolean;AllowedFileExtension:RawUTF8;Token:Rawutf8';

In Mormot 1 ,  this is working.

TTextWriter.RegisterCustomJSONSerializerFromText(typeinfo(TRECApplicationSetting),__TRECApplicationSetting).Options:= [soReadIgnoreUnknownFields, soWriteHumanReadable];

Please tell me how to convert the following in Mormot2 ?

Thank you

Offline

#2 2021-10-31 11:39:09

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

Re: How to do Custom Serialization in Mormot 2 ?

You can just call TRttiJson.RegisterFromText() or Rtti.RegisterFromText() instead.

Online

#3 2021-10-31 13:29:35

srp
Member
Registered: 2014-09-02
Posts: 11

Re: How to do Custom Serialization in Mormot 2 ?

Thank You @ab. 

I tried, but I got the error ERttiException "Text definition covers 17 bytes but RTTI defined 25.

I see that if I don't define TRttiJson.RegisterFromText( ) then it is working as expected without any problem. So the problem is resolved.

Last edited by srp (2021-10-31 13:31:28)

Offline

#4 2021-10-31 14:34:44

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

Re: How to do Custom Serialization in Mormot 2 ?

Your text definition (__TRECApplicationSetting) does not match the record definition type.
Some fields are missing (may be two pointers/integers or a double/Int64).

If you don't register it, on Delphi 2010 and later, the framework will use the RTTI so it is not mandatory to call Rtti.RegisterFromText().
On FPC or older Delphi, there is no such extended RTTI for record, and RegisterFromText() is needed.

Online

#5 2021-11-13 03:18:54

srp
Member
Registered: 2014-09-02
Posts: 11

Re: How to do Custom Serialization in Mormot 2 ?

In Mormot 1 this  is working  as expected  and is raising the exception when the port is consumed by any other service.
  Try
      HTTPServer := TSQLHttpServer.create(777),[RestSrvr]);
  Except
    on e:ECrtSocket do
       ShowMessage('Port is already in use. Please check  if any other program  is using this port and restart this Service.');
   End;


In Mormot 2 this is not working as expected and is not raising the exception when the port is consumed by any other service.
  try
       HTTPServer :=TRestHttpServer.create(777,[FRestSrvr]);
   Except
        on e: ECrtSocket do
           ShowMessage(e.message);
      End;
   
Can you please guide me if I am doing it wrong?  Is there some way to see if the RestHttpserver has started?

Thank You

Last edited by srp (2021-11-13 11:26:12)

Offline

#6 2021-11-13 12:25:20

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

Re: How to do Custom Serialization in Mormot 2 ?

On Windows with http.sys, I have a ENetSock with the expected 'Another process may be currently listening to this port! TCrtSocket.OpenBind(0.0.0.0:777) [Fatal Error - #6]' message.

But with the new async server (e.g. on Linux or on Windows if http.sys URI has not been registered) then there was a waiting loop with no exception.
I tried to fix it with my latest commit.
Note that for socket-based server (i.e. non http.sys - also in mORMot 1 IIRC) the binding is done in the server background thread so the exception may not be reflected in the main thread directly.

Online

#7 2021-11-13 15:53:38

srp
Member
Registered: 2014-09-02
Posts: 11

Re: How to do Custom Serialization in Mormot 2 ?

Thank You for looking into it @ab

I updated the new changes you made. However, the problem still exists. 
In the debug mode,  the exception is raised and handled by the Debugger exception.  However, when I try running in Administrator mode (http.sys) or the normal mode, the exception is not being raised still.

Offline

#8 2021-11-13 20:46:29

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

Re: How to do Custom Serialization in Mormot 2 ?

I have modified WaitStarted to raise an exception if it failed to start.

Online

#9 2021-11-14 05:56:02

srp
Member
Registered: 2014-09-02
Posts: 11

Re: How to do Custom Serialization in Mormot 2 ?

Perfect !!!!  It is working as expected.

Thanks, @ab for your support

Does it make sense in having a  read-only property for the RestHttpServer that exposes the current state (ExecuteState  (THttpServerExecuteState)) of the HTTP Server?


Also, in general, Is it better that I start a new thread when I have a specific issue?

Last edited by srp (2021-11-14 06:06:44)

Offline

#10 2021-11-14 12:47:53

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

Re: How to do Custom Serialization in Mormot 2 ?

There is already the ExecuteState property at THttpServerSocketGeneric level, AFAIK.

About the forum, you are right, it is always better to stard a new thread with a dedicated title, for each issue.
It would be easier to follow and find.

Online

Board footer

Powered by FluxBB