Support for RoName attribute on TRoComplexType properties for JSON


(Sindre) #1

In addition to using TRoComplexType against RoSDK based servers, we use it to model structures against different JSON based WEB services. That works fine using the TRoJSONMessage (with som trixing), but we run into problems with reserved words in structures. For exampe field names like class and type.

It would been great if the JSON serializer would respect the RoName attribute. Ie.
[RoDocumentation(‘DGClass’)]
[RoName(‘Class’)]
property Class_: integer read FClass write FClass;


(EvgenyK) #2

Hi,

try to use soapname attribute:

    [ROCustom('soapname', 'class')]
    property class_: ROAnsiString read fclass_ write fclass_;

(Sindre) #3

Thanks,

It looks like only the XML serializer is using the soapname attribute.


(RemObjects) #4

Thanks, logged as bugs://81999


(RemObjects) #5

bugs://81999 got closed with status fixed.


(EvgenyK) #6

update uRORTTISupport.pas as

  • add
function TRORTTISerializerHelper.GetSoapName(p: TRttiProperty): string;
var
  la: ROCustomAttribute;
begin
  Result := p.Name;
  for la in p.ROGetAttributes<ROCustomAttribute> do
    if SameText(la.Name,'soapname') then begin
      result := la.Value;
      Exit;
    end;
end;
  • replace p.Name with GetSoapName(p) in ReadRTTIObject/WriteRTTIObject methods of this helper

after this, this code

  TMyStruct = class(TROComplexType)
  private
    fclass_: string;
  published
    [ROCustom('soapname', 'class')]
    property class_: string read fclass_ write fclass_;
  end;

  [ROService(__ServiceName)]
  TNewService = class(TRORemoteDataModule)
  private
  public
    [ROServiceMethod]
    function Echo(a: TMyStruct): TMyStruct;
  end;

will be read/written correctly:

{"version":"1.1","result":{"class":"test"}}

Note: this change was included into beta branch only


(Sindre) #7

Thank you. This works fine.
BTW how do you paste syntax highlighted source code in here?


(EvgenyK) #8

put your code into ```, like