Why does TJSONObject.AddPair results Self?
Returning Self
is common coding pattern called fluent interface.
It allows you to continue with calls to the same object, creating chain of methods without the need to reference object variable for every call. That makes code more readable, on the other hand it is harder to debug.
var Obj: TJSONObject;begin Obj := TJSONObject.Create .AddPair('first', 'abc') .AddPair('second', '123') .AddPair('third', 'aaa'); ...end;
would be equivalent of
var Obj: TJSONObject;begin Obj := TJSONObject.Create; Obj.AddPair('first', 'abc'); Obj.AddPair('second', '123'); Obj.AddPair('third', 'aaa'); ...end;
And the generated JSON object will look like:
{ "first": "abc", "second": "123", "third": "aaa"}
That kind of coding style is more prevalent in languages with automatic memory management, because you don't need to introduce intermediate variables.
For instance, if you need JSON string you would use following construct:
var s: string;begin s := TJSONObject.Create .AddPair('first', 'abc') .AddPair('second', '123') .AddPair('third', 'aaa') .Format(2); ...end;
The problem with the above code in Delphi is that it creates memory leak as you don't have ability to release intermediate object. Because of that, it is more common to use fluent interface pattern in combination with reference counted classes where automatic memory management will handle releasing of any intermediary object instances.