Skip to content

[BUG]: write_json / read_json raise UnicodeEncodeError on Windows for non-cp1252 characters #5665

Description

@LukeTheoJohnson

Description

pio.write_json raises UnicodeEncodeError on Windows when a figure's text contains characters outside the cp1252 default codec. pio.read_json has the matching bug — it reads without specifying an encoding, so a UTF-8 JSON file is decoded with cp1252, mangling non-ASCII text or raising UnicodeDecodeError.

This works on macOS/Linux only because their default encoding is already UTF-8.

Steps to reproduce

import plotly.graph_objects as go
import plotly.io as pio

fig = go.Figure(layout_title_text="μ 中文")   # Greek mu + CJK
pio.write_json(fig, "fig.json")
# UnicodeEncodeError: 'charmap' codec can't encode character 'μ' in position ...: character maps to <undefined>

Environment: Windows, locale.getpreferredencoding(False)cp1252, orjson installed.

Notes

Two conditions both have to hold:

  1. The figure text contains a character outside cp1252. Common Latin-1 characters (é, ö, °) are inside cp1252 and do not raise — it takes Greek (μ), CJK, or emoji to trigger it.
  2. orjson is installed (the default engine when available). orjson emits real UTF-8, so non-ASCII characters reach write_text unchanged. The pure-Python json engine uses ensure_ascii=True, escaping everything to \uXXXX ASCII, so it never trips the codec.

write_html already handles this correctly with encoding="utf-8" (see #3898). The JSON I/O path was missed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions