Home / Function/ guess_json_utf() — requests Function Reference

guess_json_utf() — requests Function Reference

Architecture documentation for the guess_json_utf() function in utils.py from the requests codebase.

Entity Profile

Dependency Diagram

graph TD
  f3de4c68_3dfd_1ac1_297a_0d79b1ff3031["guess_json_utf()"]
  2c39b9da_e317_5e6c_bbac_8362bac2110c["utils.py"]
  f3de4c68_3dfd_1ac1_297a_0d79b1ff3031 -->|defined in| 2c39b9da_e317_5e6c_bbac_8362bac2110c
  49ad2d5b_28fd_baf2_5293_4c3e313ea2e8["json()"]
  49ad2d5b_28fd_baf2_5293_4c3e313ea2e8 -->|calls| f3de4c68_3dfd_1ac1_297a_0d79b1ff3031
  style f3de4c68_3dfd_1ac1_297a_0d79b1ff3031 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/requests/utils.py lines 945–974

def guess_json_utf(data):
    """
    :rtype: str
    """
    # JSON always starts with two ASCII characters, so detection is as
    # easy as counting the nulls and from their location and count
    # determine the encoding. Also detect a BOM, if present.
    sample = data[:4]
    if sample in (codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE):
        return "utf-32"  # BOM included
    if sample[:3] == codecs.BOM_UTF8:
        return "utf-8-sig"  # BOM included, MS style (discouraged)
    if sample[:2] in (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE):
        return "utf-16"  # BOM included
    nullcount = sample.count(_null)
    if nullcount == 0:
        return "utf-8"
    if nullcount == 2:
        if sample[::2] == _null2:  # 1st and 3rd are null
            return "utf-16-be"
        if sample[1::2] == _null2:  # 2nd and 4th are null
            return "utf-16-le"
        # Did not detect 2 valid UTF-16 ascii-range characters
    if nullcount == 3:
        if sample[:3] == _null3:
            return "utf-32-be"
        if sample[1:] == _null3:
            return "utf-32-le"
        # Did not detect a valid UTF-32 ascii-range character
    return None

Domain

Subdomains

Called By

Frequently Asked Questions

What does guess_json_utf() do?
guess_json_utf() is a function in the requests codebase, defined in src/requests/utils.py.
Where is guess_json_utf() defined?
guess_json_utf() is defined in src/requests/utils.py at line 945.
What calls guess_json_utf()?
guess_json_utf() is called by 1 function(s): json.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free