Skip to content

Added example: Using serde to populate structures from an API#926

Open
noobosapien wants to merge 4 commits intocloudflare:mainfrom
noobosapien:serde-structs
Open

Added example: Using serde to populate structures from an API#926
noobosapien wants to merge 4 commits intocloudflare:mainfrom
noobosapien:serde-structs

Conversation

@noobosapien
Copy link

This is a demo of using serde Serialize and Deserialize to populate structs from JSON from an API and return a response from that struct serialised as JSON. It also uses http:Request instead of worker::Request.
It is useful to get values tied to Rust variables from an API.
This fills a gap in the current examples, where using external APIs to get values and manipulate them.


[dependencies]
worker = { version = "0.7", features = ['http'] }
worker-macros = { version = "0.7", features = ['http'] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unused dependency. 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove it soon!

worker-macros = { version = "0.7", features = ['http'] }
http = "1.3"
serde = "1.0.228"
serde_json = "1.0.149"
Copy link
Contributor

@jakubadamw jakubadamw Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unused dependency. This is important in so far as using serde_json in code compiled to Wasm and deployed to Cloudflare Workers is normally discouraged in favour of serde-wasm-bindgen and using the native JSON.*() functions (through, say, js-sys) as that reduces final code bloat. But anyway, your example doesn't actually use serde_json in any way, so this can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for letting me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants