2020/09/16

Delphi JSON Objects framework 處理效率

 


最近在李維老師的部落格看到關於 JSON 的新文章【RAD Studio的JSON處理效率】,主要在描述 TJSONReader 和 TJSONWriter 物件經過重新設計後所推出的 10.4.1,其效率提升的幅度。

對於專門寫中間層服務的我來說,JSON 也是門必須的功課(另一門是 XML for ClientDataSet),從李老師的文章可以得知使用的框架是【Readers and Writers JSON Framework】,在這邊引用官方新版文件內容:

The readers and writers JSON framework provides classes and method to read and write JSON data to a stream.

Note: RAD Studio has two frameworks to manage JSON data; you can see further details about the two frameworks in JSON.

With the readers and writers JSON framework, you can do the following:

  • Writing JSON: You can use the TJSONObjectBuilder class to create a JSON object. The TJSONObjectBuilder class is a wrapper of the TJsonWriter class that provides the logic to serialize JSON data.
Although you can write JSON objects using the TJSONObjectBuilder or TJsonWriter classes, we recommend that you use the TJSONObjectBuilder class, since it provides a fluent interface for writing JSON objects.
See Writing JSON Objects for further details.
  • Reading JSON: You can use the TJSONIterator class to iterate through the JSON data. The TJSONIterator class is a wrapper of the TJsonReader class that allows you to read data serialized in JSON format.
Although you can read JSON data using the TJSONIterator or TJsonReader classes, we recommend that you use the TJSONIterator class, since it provides additional features to move around the JSON data.
See Reading JSON Objects for further details.
  • Reading and Writing BSON Data: This framework allows you to read and write BSON data with the TBsonReader and TBsonWriter classes.
See BSON for further details.

從文字中可以理解成以下的關係圖:

官方文件說建議使用 TJsonIterator(讀)和 TJsonObjectBuilder(寫),它們都是包裝 TJsonReader 和 TJsonWriter 後的成品,在好寫和好效率的取捨就看個人喜好。

從李老師的文章也可以知道在 10.4.1 版後, Readers and Writers JSON framework 的效率有大幅提升。

以 TJsonAncestor 為基底的 JSON Objects framework 很是建議我們改寫到新框架,這就引發了我的好奇:原本的效率到底是多慢?

詳細的專案程式碼放在 Github 裡,連結在文末,擷取程式碼如下:

    try
      Edit1.Text := IntToStr(LJsonRoot.Size);
      for LRootPos := 0 to LJsonRoot.Size-1 do
      begin
        LJsonObj := LJsonRoot.Get(LRootPos) as TJSONObject;
        LJsonPair := LJsonObj.Get('latitude');
        if Assigned(LJsonPair) then
        begin
          ADest.Add('ObjectNumber : ' + IntToStr(LRootPos));
          ADest.Add(LJsonPair.JsonString.Value + ' = ' + LJsonPair.JsonValue.Value);
        end;
        LJsonPair := LJsonObj.Get('longitude');
        if Assigned(LJsonPair) then
          ADest.Add(LJsonPair.JsonString.Value + ' = ' + LJsonPair.JsonValue.Value);
      end;
    finally
      LJsonRoot.Free;
    end;

程式碼說明

JSON Objects framework 是以物件為核心概念,所以沒有 Readers and Writers JSON framework 的【行數】可以使用,取而代之的是陣列位置,其它內容都參考李老師文章範例的程式架構而來。

結論


 

很意外的,10.4.1 的 JSON Objects framework 核心和舊版相比提高了 34% 的效率,光是這點就值得升級,若是轉換到 Readers and Writers JSON framework 則還能更進一步的提高!

在這麼多次的改版中,終於有和 JSON 效能相關的調整,而且相當有感!以上內容提供讀者參考。

謝謝各位收看,我們下次見!

See also

1 則留言:

  1. 正在學著在WebClinet解析JSON資料。看到你分享的文章,才意識到Delphi JSON 3種Framewok的差異。
    為了理解Readers and Writers JSON framework 找到了一位俄國人寫的分享 很實用也較理解了TJSONIterator class等的作用。謝謝你們的分享。
    https://webdelphi.ru/2019/03/rabota-s-json-v-delphi-10-3-rio-2/#readers_and_writers_json_framework

    回覆刪除