Ejemplo n.º 1
0
def loads(src: _Union[str, bytes],
          cls: _Type[FREDDecoder] = None,
          **kwargs) -> FRED:
    """
    Load FRED data from string.

    Args:
        src:
            A string with FRED formatted text.
        cls:
            A FREDEncoder subclass.
        object_hook:
            Used to construct objects.
    """
    if isinstance(src, str):
        if src.startswith('\ufeff'):
            msg = "Unexpected UTF-8 BOM (decode using utf-8-sig)"
            raise FREDDecodeError(msg, 0, 0)
    else:
        if not isinstance(src, (bytes, bytearray)):
            cls_name = type(src).__name__
            msg = f'the JSON object must be str, bytes or bytearray, not {cls_name}'
            raise TypeError(msg)
        src = src.decode(detect_encoding(src), 'surrogatepass')

    dec = (cls or FREDDecoder)(**kwargs)
    return dec.decode(src)
Ejemplo n.º 2
0
def loads(s: str, **kwargs) -> JsonObj:
    """ Convert a json_str into a JsonObj

    :param s: a str instance containing a JSON document
    :param kwargs: arguments see: json.load for details
    :return: JsonObj representing the json string
    """
    if isinstance(s, (bytes, bytearray)):
        s = s.decode(json.detect_encoding(s), 'surrogatepass')
    return json.loads(s, object_hook=lambda pairs: JsonObj(**pairs), **kwargs)
Ejemplo n.º 3
0
def loads(s: typing.Union[str, dict, bytes, bytearray]):
    """Deserialize ``s`` (a ``str``, ``dict``, ``bytes`` or ``bytearray`` instance
    containing a JSON document) to a Python module."""
    if not isinstance(s, (dict, str, bytes, bytearray)):
        raise TypeError(f'the module dict object must be dict, str, bytes or bytearray, '
                        f'not {s.__class__.__name__}')
    if isinstance(s, (bytes, bytearray)):
        s = s.decode(json.detect_encoding(s), 'surrogatepass')
    if isinstance(s, str):
        s = json.loads(s)
    check_compatibility(s)
    return jsonimporter(s)
Ejemplo n.º 4
0
def _auto_decode(fp: IO, nbytes: Optional[int] = None) -> str:
    # We have a file opened in binary mode and haven't formally established a decoder.
    # We may need as many as four bytes to determine the encoding.
    # TODO: should the BOM (if present) count in nbytes?
    if not fp.decoder:
        if nbytes and nbytes < 4:
            # Nothing we can do if they're asking for less than 4 bytes
            data = fp.native_reader(nbytes)
            return data.decode() if data else data
        # Use the BOM to figure out what is toing on.  If no BOM, we always go to UTF-8
        bom = fp.native_reader(4)
        if len(bom) < 4:
            return bom.decode(fp.decoder if fp.decoder else 'utf-8')
        fp.decoder = json.detect_encoding(bom)
        if nbytes is None:
            return (bom + fp.native_reader()).decode(fp.decoder)
        else:
            return (bom + fp.native_reader(nbytes - 4)).decode(fp.decoder)
    else:
        return fp.native_reader(nbytes).decode(fp.decoder)
Ejemplo n.º 5
0
    async def load(self,
                   fp: t.Optional[t.Union[io.TextIOBase,
                                          io.BufferedIOBase]] = None,
                   *,
                   mode='rb'):
        """
        Coroutine to load sample to json formation.

        :param fp:
            File object.
        :param mode:
            File mode. 'w', 'r', 'rb', 'wb'
        """
        if fp is None:
            fp = get_file_io(self.target_path, mode)  # type: ignore

        if not hasattr(fp, 'close'):
            raise AttributeError("fp must have close method")

        if asyncio.iscoroutine(fp):
            fp = await fp.open()
            read = await fp.read()
        elif isinstance(fp, (AsyncBufferedIOBase, AsyncTextIOWrapper)):
            read = await fp.read()
        else:
            read = fp.read()

        if isinstance(read, str):
            if read.startswith('\ufeff'):
                raise JSONDecodeError(
                    "Unexpected UTF-8 BOM (decode using utf-8-sig)", read, 0)
        else:
            if not isinstance(read, (bytes, bytearray)):
                raise TypeError(
                    f'the JSON object must be str, bytes or bytearray, '
                    f'not {read.__class__.__name__}')
            read = read.decode(detect_encoding(read), 'surrogatepass')
        json_data = self._default_decoder.decode(read)
        return json_data
Ejemplo n.º 6
0
def detect_encoding(value):
    try:
        return json.detect_encoding(value)
    except AttributeError:
        return "utf-8"
Ejemplo n.º 7
0
def detect_encoding(b):
    return json.detect_encoding(b)
Ejemplo n.º 8
0
def _decode(data: bytes) -> str:
    """Returns string representation of data."""
    try:
        return data.decode(json.detect_encoding(data), "surrogatepass")
    except (TypeError, UnicodeDecodeError):
        return "Can not decode"