# Serializing terms with the foreign language interface

**URL:** <https://swi-prolog.discourse.group/t/serializing-terms-with-the-foreign-language-interface/8751>\
**Category:** PlDoc\
**Created:** [January 24, 2025, 12:35pm UTC](https://swi-prolog.discourse.group/t/serializing-terms-with-the-foreign-language-interface/8751 "2025-01-24T12:35:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [January 24, 2025, 12:35pm UTC](https://swi-prolog.discourse.group/t/serializing-terms-with-the-foreign-language-interface/8751/1 "2025-01-24T12:35:50Z")

</div>

In the [Foreign Include File section](https://www.swi-prolog.org/pldoc/man?section=foreigninclude), there is subsection [12.4.9, Serializing and deserializing Prolog terms](https://www.swi-prolog.org/pldoc/man?section=foreign-serialize). If I am reading this correctly, it only discusses de-serialization through [`PL_put_term_from_chars()`](https://www.swi-prolog.org/pldoc/doc_for?object=c(%27PL_put_term_from_chars%27)).

Is the serialization meant to be done with [`PL_get_chars()`](https://www.swi-prolog.org/pldoc/doc_for?object=c(%27PL_get_chars%27)) and [`PL_get_nchars()`](https://www.swi-prolog.org/pldoc/doc_for?object=c(%27PL_get_nchars%27)), using the flag `CVT_WRITE_CANONICAL`?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 24, 2025, 12:56pm UTC](https://swi-prolog.discourse.group/t/serializing-terms-with-the-foreign-language-interface/8751/2 "2025-01-24T12:56:40Z")

</div>

Yes. There are some more flags than `CVT_WRITE_CANONICAL`. If you want full control you either have to play tricks with PL\_write\_term() and I/O streams or, much easier, write a Prolog predicate that does the serialization to a string as you want it and call that.
