You are not logged in.
Hi Arnaud,
imho the method "ObjectToJSONFile" should be a function, not procedure. The current implementation is:
procedure ObjectToJSONFile(Value: TObject; const JSONFile: TFileName;
Options: TTextWriterWriteObjectOptions);
var humanread: boolean;
json: RawUTF8;
begin
humanread := woHumanReadable in Options;
Exclude(Options,woHumanReadable);
json := ObjectToJSON(Value,Options);
if humanread then
// woHumanReadable not working with custom JSON serializers, e.g. T*ObjArray
JSONBufferReformatToFile(pointer(json),JSONFile) else
FileFromString(json,JSONFile);
end;
"JSONBufferReformatToFile()" and "FileFromString()" do return valueable output, so that should be passed as result.
Changing that procedure into a function should have no impacts on existing code.
What do you thing, could you include following change to mORMot.pas:
function ObjectToJSONFile(Value: TObject; const JSONFile: TFileName;
Options: TTextWriterWriteObjectOptions): boolean;
var humanread: boolean;
json: RawUTF8;
begin
humanread := woHumanReadable in Options;
Exclude(Options,woHumanReadable);
json := ObjectToJSON(Value,Options);
if humanread then
// woHumanReadable not working with custom JSON serializers, e.g. T*ObjArray
result:=JSONBufferReformatToFile(pointer(json),JSONFile) else
result:=FileFromString(json,JSONFile);
end;
Offline
I wonder how ObjectToJSON() would return invalid JSON...
Its json output should be always valid.
This is why this was defined as a procedure, not a function.
Did you ever see "JSONBufferReformatToFile()" and "FileFromString()" return anything else than "true" in this case?
Offline
FileFromString could fail because of wrong FileName parameter, or because write access to FileName is not allowed.
More generally: an operation could always fail if file i/o is involved.
Last edited by oz (2016-06-10 13:38:20)
Offline
Does make sense!
See http://synopse.info/fossil/info/47a6abc1e2
Thanks for the feedback.
Offline
Thanks for quick implementation in trunk!
Offline