PHP 內建的 JSON 函式庫,因為其授權並非標準的「自由/開源軟體授權」,而被 Debian / Fedora 等系統排除在標準預設安裝流程外。

影響

Debian / Fedora 等系統未來安裝 PHP 5.5 時,預設將不包括 php-json 函式庫,也就是無法正常使用 json_encode 及 json_decode 等函式。若欲進行安裝的話,必須要自行編譯,或者使用 php-pecl-jsonc 為其替代方案。

php-pecl-jsonc 為例,在 Fedora 系統下必須先設定好 Remi,再進行下列的指令安裝:

$ yum --enablerepo=remi install php-pecl-jsonc

安裝 php-pecl-jsonc 後,不需要擔心與原本內建 php-json 的相容性,因為 php-pecl-jsonc 一樣支援如 json_encode 及 json_decode 的函式,所以是個透明的替代方案,幾乎沒有額外的成本。

但根據 php-pecl-jsonc 的作者指出,這個替代方案會使得 json 處理的效能變差(loose of performance),但他認為還可以接受(but still acceptable)。

原因

這個 Bug 早在 2012 年 11 月 14 日被提起。

主要原因是 PHP 內建的 JSON 函式庫,使用的是 JSON.org 官網提供的 JSON_checker 程式碼。問題就出在它的授權並非標準的「自由/開源軟體授權」。

截錄 JSON_checker.c 原始碼的一段授權聲明:

Copyright (c) 2005 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

注意最後一句,「The Software shall be used for Good, not Evil」,就是因為這句話使得該授權無法符合「自由軟體」或是「開源軟體」的要求。所以在一些很注重授權的系統中,這個函式庫勢必會被移除。

而替代方案 php-pecl-jsonc 的程式碼授權,採用的是符合「自由軟體」或是「開源軟體」要求的 PHP License 3.01,所以得以解決授權問題。

參考來源:

  1. Why PHP JSON was removed