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
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.
1
Popular in Announcing
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM.
...
New
Yes, yet another parser combinator library!
Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
New
I’d like to announce a small library called boundaries.
This is an experimental project which explores the idea of enforcing boundaries ...
New
Hello :waving_hand:
Allow me to introduce you to Tz, an alternative time zone database support to Tzdata.
Why another library?
First a...
New
WebAuthnLiveComponent WebAuthnComponents
See this post about renaming the package.
Passwordless authentication for Phoenix LiveView app...
New
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
Lexical
Lexical is a next-generation language server for the Elixir programming language.
Features
Context aware code completion
As-you...
New
Other popular topics
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
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









