diff options
author | Urban Müller | 2021-12-02 19:25:39 +0100 |
---|---|---|
committer | Urban Müller | 2021-12-02 19:25:39 +0100 |
commit | 7c61258ec289cec41b5b153b4baf4b8c44a5e8ec (patch) | |
tree | 2ecf47fe5323331263d513aad805f79044760262 | |
parent | 8d10ff3fd25c50b4cece4fd0942f20765322352b (diff) | |
download | itools-7c61258ec289cec41b5b153b4baf4b8c44a5e8ec.tar.gz itools-7c61258ec289cec41b5b153b4baf4b8c44a5e8ec.tar.bz2 itools-7c61258ec289cec41b5b153b4baf4b8c44a5e8ec.zip |
allow reading/writing of json in one go
-rw-r--r-- | it.class | 6 |
1 files changed, 6 insertions, 0 deletions
@@ -1111,6 +1111,7 @@ static function mail($p) * @param $filename name of file to read or - for stdin * @param $p['keyval'] each line of the file is one tab-separated key/value pair, return assoc array * @param $p['lines'] return file as array of lines without newline + * @param $p['json'] return data json-decoded, set to "assoc" to get objects as assoc arrays */ static function file_get($filename, $p = array()) { @@ -1126,6 +1127,8 @@ static function file_get($filename, $p = array()) } else if ($p['lines']) $result = explode("\n", rtrim($data, "\n")); + else if ($p['json']) + $result = it::json_decode($data, ['assoc' => $p['json'] == "assoc", 'it_error' => ['title' => "bad json in $filename"]]); else $result = $data; } @@ -1139,6 +1142,7 @@ static function file_get($filename, $p = array()) * @param $data data to write, string by default * @param $p['keyval'] $data must be an assoc array and is written as tab-separated lines * @param $p['lines'] write array of lines, appending newline + * @param $p['json'] write array as json * @param $p['mkdir'] create parent directory (one level) * @param $p['cdist'] distribute saved file */ @@ -1150,6 +1154,8 @@ static function file_put($filename, $data, $p = array()) $data = implode("", it::map('"$k\t$v\n"', $data)); else if ($p['lines']) $data = count((array)$data) ? implode("\n", (array)$data) ."\n" : ""; + else if ($p['json']) + $data = it::json_encode($data); if ($p['mkdir'] && $filename != "-") @mkdir(dirname($filename)); |