#1 2016-06-10 08:50:56

oz
Member
Registered: 2015-09-02
Posts: 95

Change request: Convert "procedure ObjectToJSONFile()" into a function

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

#2 2016-06-10 11:10:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Change request: Convert "procedure ObjectToJSONFile()" into a function

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

#3 2016-06-10 13:32:56

oz
Member
Registered: 2015-09-02
Posts: 95

Re: Change request: Convert "procedure ObjectToJSONFile()" into a function

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

#4 2016-06-10 13:37:43

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Change request: Convert "procedure ObjectToJSONFile()" into a function

Does make sense!
See http://synopse.info/fossil/info/47a6abc1e2

Thanks for the feedback.

Offline

#5 2016-06-10 21:09:27

oz
Member
Registered: 2015-09-02
Posts: 95

Re: Change request: Convert "procedure ObjectToJSONFile()" into a function

Thanks for quick implementation in trunk!

Offline

Board footer

Powered by FluxBB