You are not logged in.
Pages: 1
Hello ab,
I have this interface to comunication between client and server.
IClient = interface(IInvokable)
['{3E095FAD-5D65-4A27-82D5-6492F197F402}']
procedure SomeMethod (AClientDTO: TClientDTO);
end;
In the server, I have this version of TClientDTO:
TClientDTO = class(TSQLRecord)
private
FCode: integer;
FName: RawUTF8;
published
property Code: Integer read FCode write FCode;
property Name: RawUTF8 read FName write FName;
end;
and the client has this version of TClientDTO (a new property):
TClientDTO = class(TSQLRecord)
private
FCode: integer;
FName: RawUTF8;
FCelfone: RawUTF8;
published
property Code: Integer read FCode write FCode;
property Name: RawUTF8 read FName write FName;
property Celfone: RawUTF8 read FCelfone write FCelfone;
end;
when I execute the method SomeMethod in the client side, I receive the error 400 – bad request.
May be you ask me if this situation is common to me. Yes, the client can be the last version and the server can be a old version...
Well, my question is: Can I force the server ignore the property Celfone?
Offline
I found this... if I change the lines bellow, my problem is resolved.
function JSONToObject(var ObjectInstance; From: PUTF8Char; var Valid: boolean;
TObjectListItemClass: TClass=nil): PUTF8Char;
...
P := ClassFieldPropWithParentsFromUTF8(PPointer(Value)^,PropName);
if P=nil then
begin
Valid := true; // return it until is true...
PropValue := GetJSONField(From,From,@wasString,@EndOfObject); // get value and ignore de value
continue; //exit; // unknwown property
end;
...
What can I do ?
Offline
You can help Arnaud?
Offline
I've added a new TJSONToObjectOptions optional parameter in JSONToObject() / ObjectToJSON() functions and associated WriteObject() method.
It should add the behavior you were missing.
Offline
Thanks AB.
Let's test it.
Offline
Ensure you retrieve the latest revision - c826016e40 may be broken, but http://synopse.info/fossil/info/6fc099a149 should be fine (and including all new variant-based features).
BTW, the new variant types with late-binding are a very good candidate for dynamic DTO classes: no type definition to write, just use properties as you need them.
Offline
Hello AB,
Thank you. Sorry for not replying earlier ... I was on vacation :-).
Offline
Hello AB,
on this subject, I have a question:
on an http client, how should I set the new attribute of my DTO class?
I saw that in the method ...
function TServiceMethod.InternalExecute(...
... call the function without any parameters:
Par: JSONToObject = (Objects [IndexVar] Pair valid);
So I changed as below so that the server does not generate an error as DTO customer has with a new attribute.
Par: JSONToObject = (Objects [IndexVar] Pair valid, nil, [j2oIgnoreUnknownProperty]);
Offline
Hello,
Using a custom reader method bellow:
class function TServerClass.MyClassCustomReader(const aValue: TObject; aFrom: PUTF8Char; var aValid: Boolean; aOptions: TJSONToObjectOptions): PUTF8Char;
var
oDTOMyClass: TDTOMyClass;
begin
aOptions := aOptions + [j2oIgnoreUnknownProperty];
result := aFrom;
oDTOMyClass := TDTOMyClass(aValue);
JSONToObject(oDTOMyClass, aFrom, aValid, nil, aOptions);
oDTOMyClass := nil;
end;
...and registering the method with the code bellow:
TJSONSerializer.RegisterCustomSerializer(TDTOMyClass, MyClassCustomReader, nil);
...my code enter in a loop (of course).
JSONToObject->MyClassCustomReader->JSONToObject->MyClassCustomReader....
I need just include the j2oIgnoreUnknownProperty option. What the best way to do it without recreate another JSONToObject ?
Thanks.
Dorival
Offline
well I changed de method (mormot.pas)
function JSONToObject(var ObjectInstance; From: PUTF8Char; var Valid: boolean; TObjectListItemClass: TClass=nil;
Options: TJSONToObjectOptions=[]): PUTF8Char;
...to...
function JSONToObject(var ObjectInstance; From: PUTF8Char; var Valid: boolean; TObjectListItemClass: TClass=nil; Options:
TJSONToObjectOptions=[j2oIgnoreUnknownProperty]): PUTF8Char;
(Options default j2oIgnoreUnknownProperty ).
It´s work for me. Someone knows if can it do some problem?
Dorival
Last edited by dorival (2014-06-10 20:53:16)
Offline
Pages: 1