#Rust πŸ¦€ – Serializing and Deserializing data structures to JSON with Serde


While I was creating a sample on how to use OpenAI APIs in Rust, I realized that I needed to serialize and deserialize some data in JSON format. Lucky me, Serde JSON works great!

Serde JSON

Serde JSON is a library for serializing and deserializing Rust data structures efficiently and generically12. It provides ways to convert between different representations of JSON data, such as:

  • As a strongly typed Rust data structure. When you expect all or most of your data to conform to a particular structure and want to get real work done without JSON’s loosey-goosey nature tripping you up3.
  • As an untyped JSON value. When you want to work with data that does not have a predefined structure3.
  • As a stream of JSON events. When you need to read or write large messages with minimal memory overhead3.

Tool to convert JSON to Rust Structs

My next step was to analyze one of the JSON response samples from OpenAI APIs, and manually create the data struct. I’m lazy, so I search and find this amazing website: https://transform.tools/json-to-rust-serde

2 seconds later, I have my structures to work with OpenAI APIs !

Next week I’ll share more details about the repo with samples on how to use OpenAI APIs in Rust.

Happy coding!


El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: