You are not logged in.
I have class like this:
type
TMyCalss = class
private
fid: integer;
fqty:TNullableInteger;
fdescr:TNullableUtf8;
published
property id: integer read fid write fid;
property qty: TNullableInteger read fqty write fqty;
property descr: TNullableUtf8 read fdescr write fdescr;
end;
json := ObjectToJson(MyCalss ,[]);
is there an option create json without null values?
[{"id":1,"qty":null,"fdesc":"descr"}] => [{"id":1,"fdesc":"descr"}]
It is nice TTextWriterWriteObjectOption include woReomveNullValues or something like that
Last edited by anouri (2025-01-30 08:49:58)
Offline
Yes it works.
But when the field is of arrayof class type and is not assigned, it will also delete it.
type
TMyCalss = class
private
fid: integer;
fqty:TNullableInteger;
FOtherClass:TArrayOfOtherClass;
published
property id: integer read fid write fid;
property qty: TNullableInteger read fqty write fqty;
property OtherClass: TArrayOfOtherClass read FOtherClass write FOtherClass;
end;
In many cases api report this as error and expect [] for nil array.
Last edited by anouri (2025-01-30 09:21:21)
Offline
I find a way:
var
MyCalss: TMyCalss;
json: string;
MyArray: TMyArray;
begin
SetLength(MyArray, 1);
MyArray[0] := TClass.Create;
MyCalss := TMyCalss.Create;
MyCalss.ID := 1;
MyCalss.ArrayClass := MyArray;
json := ObjectToJson(MyCalss,[woDontStoreVoid]);
json := StringReplace(json,'[{}]','[]',[rfReplaceAll]);
result => {"ID":1,"ArrayClass":[{}]}
then StringReplace replace [{}] with [].
I hope it is safe way!
Thank you for great framewotk
Last edited by anouri (2025-01-30 10:14:44)
Offline
I confused with woDontStoreVoid because of documentation in mormot.core.text:
// - woDontStoreVoid will avoid serializing numeric properties equal to 0 and
// string properties equal to '' (replace both deprecated woDontStore0 and
behaviour is different:
when numeric field has 0 value or string has '' it serialize it to {"num":0,"str":""}. but when it is null doesn't serialize it.
I think documentation need to be correct?
Last edited by anouri (2025-01-30 11:11:15)
Offline