Reply_json_dict: avoid losing distinction between atoms and strings

I have a dict in my server code that looks like this:

_337322{elements:[[name1,alice],[name2,“alice”]]}

After dispatching it off to the client using reply_json_dict, it arrives looking like this:

{
“elements”: [ [“name1”, “alice” ], [“name2”, “alice” ] ]
}

Atoms and strings both end up mapping onto strings, so crucial information has been lost. (name1 was paired with an atom, name2 was paired with a string.)

Is there some way I can preserve the info? I had a look at the options blurb for reply_json_dict/2 but didn’t see anything that would obviously help me.

If the distinction between atom and string is crucial to you you’ll have to map your data structure into something that preserves this. For example, you could map each atom into _{type:atom, text:alice}. As is, JSON has only a single string type and the Prolog mapping tries to be in sync what JSON documents typically mean as closely as possible. If you want to represent pure Prolog data I would serialize it using write_canonical/1

Note that then Pengines library also provides a JSON representation for arbitrary Prolog terms.