Changeset 704 for trunk/lib


Ignore:
Timestamp:
Oct 10, 2019 3:24:03 AM (5 years ago)
Author:
anonymous
Message:

Add jsonDecodeFile() function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Utilities.inc.php

    r701 r704  
    14931493    return preg_match('/^[23]\d\d$/', $http_code);
    14941494}
     1495
     1496/*
     1497* Load JSON data from a file and return it as an array (as specified by the json_decode options passed below.)
     1498*
     1499* @access   public
     1500* @param    string  $filename   Name of the file to load. Just exist in the include path.
     1501* @param    bool    $assoc      When TRUE, returned objects will be converted into associative arrays.
     1502* @param    int     $depth      Recursion depth.
     1503* @param    const   $options    Bitmask of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR.
     1504* @return   array               Array of data from the file, or null if there was a problem.
     1505* @author   Quinn Comendant <quinn@strangecode.com>
     1506* @since    09 Oct 2019 21:32:47
     1507*/
     1508function jsonDecodeFile($filename, $assoc=true, $depth=512, $options=0)
     1509{
     1510    $app =& App::getInstance();
     1511
     1512    if (false === $resolved_filename = stream_resolve_include_path($filename)) {
     1513        $app->logMsg(sprintf('JSON file not found: %s', $resolved_filename), LOG_ERR, __FILE__, __LINE__);
     1514        return null;
     1515    }
     1516
     1517    if (!is_readable($resolved_filename)) {
     1518        $app->logMsg(sprintf('JSON file is unreadable: %s', $resolved_filename), LOG_ERR, __FILE__, __LINE__);
     1519        return null;
     1520    }
     1521
     1522    if (null === $data = json_decode(file_get_contents($resolved_filename), $assoc, $depth, $options)) {
     1523        $app->logMsg(sprintf('JSON is unparsable: %s', $resolved_filename), LOG_ERR, __FILE__, __LINE__);
     1524        return null;
     1525    }
     1526
     1527    return $data;
     1528}
Note: See TracChangeset for help on using the changeset viewer.