Parse file extension.

    - *.json: uncompressed, utf-8 encode json file
    - *.gz: compressed, utf-8 encode json file
180998
def is_json_file(abspath):
    
    abspath = abspath.lower()
    fname, ext = os.path.splitext(abspath)
    if ext in [".json", ".js"]:
        is_json = True
    elif ext == ".gz":
        is_json = False
    elif ext == ".tmp":
        return is_json_file(fname)
    else:
        raise JsonExtError(
            "'%s' is not a valid json file. "
            "extension has to be '.json' for uncompressed, '.gz' "
            "for compressed." % abspath)
    return is_json
1353671
def _init_read_gz(self):
        
        self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
        self.dbuf = b""

        # taken from gzip.GzipFile with some alterations
        if self.__read(2) != b"\037\213":
            raise ReadError("not a gzip file")
        if self.__read(1) != b"\010":
            raise CompressionError("unsupported compression method")

        flag = ord(self.__read(1))
        self.__read(6)

        if flag & 4:
            xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
            self.read(xlen)
        if flag & 8:
            while True:
                s = self.__read(1)
                if not s or s == NUL:
                    break
        if flag & 16:
            while True:
                s = self.__read(1)
                if not s or s == NUL:
                    break
        if flag & 2:
            self.__read(2)
897611
def __unpack_tgz(self, filename):
        """"""
        if isinstance(filename, string_types) and self.__isValidTGZ(filename) and tarfile.is_tarfile(filename):
            with tarfile.open(filename, mode='r:gz') as t:
                for name in t.getnames():
                    t.extract(name, self.plugin_abspath)
        else:
            raise TarError("Invalid Plugin Compressed File")
789814
def parse(self, data, mimetype):
        
        encoding = mimetype.params.get('charset') or 'utf-8'

        return json.loads(data.decode(encoding))
742547
def _expand_json(self, j):
        
        decompressed_json = copy.copy(j)
        decompressed_json.pop('blob', None)  # Remove BLOB portion of JSON

        # Decode and decompress JSON
        compressed_data = base64.b64decode(j['blob'])
        original_json = zlib.decompress(compressed_data).decode('utf-8')

        decompressed_json['users'] = json.loads(original_json)  # Insert users

        return decompressed_json
1025549
function encodeFileToJson(file) {
    var filePath = file.getPath();
    if (!filePath) {
        return undefined;
    }

    return {
        path: filePath,
        mtime: file.getMTime(),
        type: file.getType()
    };
}
361702
def gzip_encode(data):
    
    if not gzip:
        raise NotImplementedError
    f = StringIO.StringIO()
    gzf = gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1)
    gzf.write(data)
    gzf.close()
    encoded = f.getvalue()
    f.close()
    return encoded
582792
def fromJSON(value):
        """"""
        if isinstance(value, str):
            value = json.loads(value)
        elif isinstance(value, dict): pass
        else:
            raise AttributeError("Invalid input")
        return Extension(typeName=value['typeName'],
                         capabilities=value['capabilities'],
                         enabled=value['enabled'] == "true",
                         maxUploadFileSize=value['maxUploadFileSize'],
                         allowedUploadFileType=value['allowedUploadFileTypes'],
                         properties=value['properties'])
179759
def gunzip(content):
  
  gzip_magic_numbers = [ 0x1f, 0x8b ]
  first_two_bytes = [ byte for byte in bytearray(content)[:2] ]
  if first_two_bytes != gzip_magic_numbers:
    raise DecompressionError('File is not in gzip format. Magic numbers {}, {} did not match {}, {}.'.format(
      hex(first_two_bytes[0]), hex(first_two_bytes[1])), hex(gzip_magic_numbers[0]), hex(gzip_magic_numbers[1]))

  stringio = BytesIO(content)
  with gzip.GzipFile(mode='rb', fileobj=stringio) as gfile:
    return gfile.read()
1180978
function parseFile(filePath, encoding, callback) {
	return fs.readFile(filePath, encoding, function(err, fileString) {
		if (err) {
			return callback(err);
		}

		fileString = expandIncludes(fileString, path.dirname(filePath));

		return parse(fileString, callback);
	});
}
396488
function read(fp, opts, fn) {
  opts = opts || {};
  opts.encoding = opts.encoding || 'utf8';

  if (opts.read) {
    return opts.read(fp, opts);
  } else if (fn) {
    return fn(fp, opts);
  }

  if (endsWith(fp, '.js')) {
    return tryRequire(fp);
  } else {
    var str = tryCatch(fs.readFileSync, fp, opts);
    if (endsWith(fp, '.json')) {
      return tryCatch(JSON.parse, str);
    }
    return str;
  }
}
657558
def _read_file(self, filename):
        

        if self.__compression:
            f = gzip.GzipFile(filename, "rb")
        else:
            f = open(filename, "rb")
        res = pickle.load(f)
        f.close()
        return res
1325388
def to_json(data, compress=False):
    
    return json.compress(data) if compress else json.dumps(data)
300350
def parse_json(json_file, include_date=False):
    
    if json_file[-2:] == 'gz':
        fh = gzip.open(json_file, 'rt')
    else:
        fh = io.open(json_file, mode='rt', encoding='utf8')
    for line in fh:
        try:
            jj = json.loads(line)
            if type(jj) is not list:
                jj = [jj]
            for j in jj:
                if include_date:
                    yield (j['user']['screen_name'].lower(), j['text'], j['created_at'])
                else:
                    if 'full_text' in j: # get untruncated text if available.
                        yield (j['user']['screen_name'].lower(), j['full_text'])
                    else:
                        yield (j['user']['screen_name'].lower(), j['text'])

        except Exception as e:
            sys.stderr.write('skipping json error: %s\n' % e)
292135
def gzip_uncompress(data, truncated=False):
    '''
    '''
    decompressor = SimpleGzipDecompressor()
    inflated_data = decompressor.decompress(data)

    if not truncated:
        inflated_data += decompressor.flush()

    return inflated_data
843264
function parse(context, file) {
  var message

  if (stats(file).fatal) {
    return
  }

  if (context.treeIn) {
    debug('Not parsing already parsed document')

    try {
      context.tree = json(file.toString())
    } catch (error) {
      message = file.message(
        new Error('Cannot read file as JSON\n' + error.message)
      )
      message.fatal = true
    }

    /* Add the preferred extension to ensure the file, when compiled, is
     * correctly recognized. Only add it if there’s a path — not if the
     * file is for example stdin. */
    if (file.path) {
      file.extname = context.extensions[0]
    }

    file.contents = ''

    return
  }

  debug('Parsing `%s`', file.path)

  context.tree = context.processor.parse(file)

  debug('Parsed document')
}
1263315
def load_json(filename, **kwargs):
    

    with open(filename, 'r', encoding='utf-8') as f:
        return json.load(f, **kwargs)