You are not logged in.
Pages: 1
I use interface based service. The methods has parameters in the form of an array of records, some of the record fields are an enumerated type. When the client communicates with the server, the call parameters are converted to JSON, and the enumerated type are converted/casted to Int32.
Is it possible to set the server or service so that the parameters of the enumerated types are converted to a text string?
So e.g. instead of ftString => 1 to be ftString => 'ftString'
Offline
I believe converting and transferring the enum values in their string presentations would significantly reduce the performance.
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
I suppose that something (depending on your expectations) can be done with TEnumType (mORMot unit). There is a batch of pretty functions for working with string values of enumerated types.
As a sample of using you can look at TSQLTable.ExpandAsString function f.ex.
I'm using ExpandAsString in my projects and I didn't see any additional visible reduce of performance with enumerated types, even with big data tables. I admit that working with integer values is much faster, but I think that for most everyday tasks such an economy doesn't worth efforts.
Besides, jaclas asked the question "Is it possible ... ?", and not about performance
Offline
@Vitaly,
Thanks for your comments.
It's good to know about the useful `TEnumType` type and your real-life experience with it.
But If I understand the OP correctly, he wants the parameters of type enum of an interface-based service to be transfer by the framework in the format of strings instead of integer values, as far as I know it's not possible?
I just checked the "Service Methods Parameters” chapter of the document, it states that all enum values are being transferred as JSON numbers, not sure if there is an option to configure/change that.
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
I don't know details of the task - that's why I wrote: "depending on your expectations".
As well as don't know, why working with strings can be better than enumerated for some task But situations may be different and unexpected.
If jaclas wants string parameter instead of enumerated in some method, I don't see any problem to use it. For example, converting with TEnumType to string on some side (client or server, depending on needs) seems to me to be a rather simple and fastly implemented decision. How do you think?
I'm asking because I'm interested in everything concerning enumerated types in mORMot (or Delphi/FPC) - there are more and more of them in my projects, lately. And I want to use all options
Offline
Thanks for replies...
I don't think that the transfer of parameters of the enumerated type is noticeably slower, although it is to be tested.
But I personally see three advantages of this enumeration type encoding:
- makes debugging services easier
- makes it easier to connect foreign clients to the server (in other languages, e.g. JS)
- eliminates the problem of extending the type in the future by new values (then e.g. ftTable = 5 soon becomes ftTable = 10 and all client code needs to be corrected)
@Vitaly for enumerated types, see Spring4D and TEnum type :-)
Offline
- eliminates the problem of extending the type in the future by new values (then e.g. ftTable = 5 soon becomes ftTable = 10 and all client code needs to be corrected)
You can use directly pointed values, so it will stay fixed until you change it. Like:
TRegionLevel = (rlCountry = 0, rlCounty = 1, rlMunicipality = 2, rlBSU = 3);
@Vitaly for enumerated types, see Spring4D and TEnum type :-)
I will, thanks!
Offline
I try, like you suggest, enumerated type with assigned integer value:
TTest = (ttOne = 1, ttTen = 10, ttHund = 100);
then I set two field to ttTen and ttHund value, now I get strange values in transport JSON (coded by mORMot):
{"Test":"0A00000000000000"},{"Test":"6400000000000000"}
So this way is blind :-)
Offline
There is no RTTI generated by the compiler for such uncontiguous enumerated types, so they are not properly serialized.
About enums serialization as string, it is not possible yet for method parameter - but it is for record serialization IIRC.
Note that if you add some items in the middle of the enumerate, it will break ORM persistence - so as a general advice, new items are to appended at the end, not in the middle of the enumerate.
Offline
Maybe it happens because of the order of values, so it becomes not traditional enumeration. Try standard numeration, beginning with 0:
TTest = (ttOne = 0, ttTen = 1, ttHund = 2);
So it will be the same as
TTest = (ttOne, ttTen, ttHund);
My idea from the above was just to avoid the problem "ftTable = 5 soon becomes ftTable = 10" through coding, but not to change the enumeration type It will be not easy to break the enumeration while adding some new member if all old members already have their directly set numbers. Of course, you can do it simpler, just remember to add values only to the end of the enumeration.
Although, maybe there is some another approach, and maybe somebody will share it here
Offline
About enums serialization as string, it is not possible yet for method parameter - but it is for record serialization IIRC.
@ab, yes, for record serialization enums can be serialized as string, but please consider adding options to the server/service configuration so that the parameters of the enumerated type (including complex types such as records or arrays) are serialized to a string
Offline
Pages: 1