hkrutzer

hkrutzer

term_to_json - Convert (m)any terms to JSON

TermToJson - Convert Elixir Terms to Human-Readable JSON

Hi everyone! I’d like to share a small library that helps serialize Elixir terms to JSON while preserving their structure and types.

Do not use with untrusted input!

What does it do?

TermToJson converts Elixir data types like structs, tuples, and atoms to JSON and back. It’s particularly useful when you need to:

  • Store Elixir terms in databases or log files while keeping them human-readable
  • Make your serialized data queryable by external tools
  • Preserve type information when serializing complex structures

Quick Examples

# Structs work out of the box
uri = URI.parse("https://elixir-lang.org/")
json = TermToJson.encode(uri)
TermToJson.decode(json)  # Returns the original URI struct

# Tuples are preserved
TermToJson.encode({:ok, ~U[2022-01-11 12:00:12Z]})
# => ["__tuple__",":ok","2022-01-11T12:00:12Z"]

# Regex
TermToJson.encode(~r/foo/uiU)
# => ["__regex__","foo",[":ungreedy",":caseless",":unicode",":ucp"]]

Key Features

  • Built on top of Erlang’s native :json module
  • Handles structs, atoms, DateTime and other types
  • Readable JSON output
  • No dependencies

Links

Most Liked

hkrutzer

hkrutzer

Interesting! You should be able to use my library, or (assuming you are using the LangChain library) add a Jason.Encoder impl for %LangChain{}. Either can be stored in the Postgres JSON type.

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
blatyo
https://www.conduitframework.com/ The best overview for how things are tied together is this presentation. Modules and functions are pre...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

Sub Categories:

We're in Beta

About us Mission Statement