Pages
Introduction
Getting Started
Recipes
Reference
Literals
Arrays
Tuples
Type System
Functions
Modules
Records
Enums
Built-in Types
Constants
Equality
Stores
Routing
Comments
Control Expressions
Components
Properties
Computed Properties
Styling Elements
Connecting Stores
Using Providers
Internal State
Referencing Entities
Global Components
Lifecycle Functions
Directives
JavaScript Interop
Environment Variables
Packages
Encode Expression
Since we can decode objects we need to have a way to encode them into JavaScript objects as well.
The
encode
expression is the way to do that:
encode { name = "Bob" } /* Object */
encode variable /* Object */
The
encode
expression tries to encode a typed object into a JavaScript object
(
Object
type in Mint), in case that it's impossible to do that you will get a nice
error message.
The general rule is that you can
decode
anything that you can
encode
.
Encoding data is useful when you want to convert it to JSON:
(encode { name = "Bob" })
|> Json.stringify()
/* { "name": "Bob"} */