pub fn json<S: Serialize>(s: S) -> Result<String>
Expand description
Serialize to JSON (requires json
feature)
The generated string does not contain ampersands &
, chevrons < >
, or apostrophes '
.
To use it in a <script>
you can combine it with the safe filter:
<script>
var data = {{data|json|safe}};
</script>
To use it in HTML attributes, you can either use it in quotation marks "{{data|json}}"
as is,
or in apostrophes with the (optional) safe filter '{{data|json|safe}}'
.
In HTML texts the output of e.g. <pre>{{data|json|safe}}</pre>
is safe, too.