JSON output with emoiji

Currently writing some JSON test cases. And had a look
at test_json.pl. It contains this test case:

?- atom_json_term('"\\ud83d\\udc95"', X, []), X = '\U0001F495'.
X = πŸ’• .

The above test case now even works on the Windows platform.
Tested with SWI-Prolog 9.3.0. But this test case fails:

?- X = '\U0001F495', atom_json_term(Y, X, []).
ERROR: I/O error in write on stream <stream>(00000225565c65e0) (Encoding cannot represent character)

How serialize the emoji as JSON?

Edit 16.02.2024
The test case works in Scryer-Prolog, the Prolog system simply
doesn’t use \uXXXX escape in an emoji output:

?- use_module(library(serialization/json)).
?- json_chars(X, "\"πŸ’•\"", "").
   X = string("πŸ’•")
;  ... .
?- json_chars(string("πŸ’•"), X, "").
   X = "\"πŸ’•\""
;  ... .

Probably a Windows and/or atom_json_term/3 artefact.
SWI-Prolog SWISH doesn’t have any problems with it.

Take this query:

?- atom_codes(X, [128149, 128149, 128149]).

The network viewer shows me this response:


Indeed. Pushed a fix. Full Unicode support on Windows is relatively new. It should work in the core, but JSON string I/O is written in C and the plugin was not aware of surrogate pairs.

1 Like