Dart Core Libraries 'dart:convert'

Dart Core Libraries 'dart:convert'

The dart:convert library (API reference) has converters for JSON and UTF-8, as well as support for creating additional converters.

This library provides a set of encoders and decoders that allow Dart applications to seamlessly convert data between different representations.

JSON

JSON is a simple text format for representing structured objects and collections.

A JsonCodec encodes JSON objects to strings and decodes strings to JSON objects. The json encoder/decoder transforms between strings and object structures, such as lists and maps, using the JSON format.

The json is the default implementation of JsonCodec.

Example - JSON Encoding and Decoding:

import 'dart:convert';

void main() {
  // Encode a Dart map into a JSON string
  Map<String, dynamic> person = {
    'name': 'Jinali',
    'age': 21,
    'email': 'Jinali@example.com'
  };
  String jsonEncoded = jsonEncode(person);
  print('Encoded JSON: $jsonEncoded');

  // Decode a JSON string back into a Dart map
  String json = '{"name":"Reet","age":14}';
  Map<String, dynamic> decodedMap = jsonDecode(json);
  print('Decoded Map: $decodedMap');
}

Output:

Encoded JSON: {"name":"Jinali","age":21,"email":"Jinali@example.com"}
Decoded Map: {name: Reet, age: 14}

UTF-8

A Utf8Codec encodes strings to UTF-8 code units (bytes) and decodes UTF-8 code units to strings.

The utf8 is the default implementation of Utf8Codec.

import 'dart:convert';

void main() {
  // Encode a string into UTF-8 bytes
  var encoded = utf8.encode('Îñţérñåţîöñåļîžåţîờñ');

  // Decode UTF-8 bytes back into a string
  var bytes = [
    195, 142, 195, 177, 197, 163, 195, 169, 114, 195, 177, 195, 165, 197,
    163, 195, 174, 195, 182, 195, 177, 195, 165, 196, 188, 195, 174, 197,
    190, 195, 165, 197, 163, 195, 174, 225, 187, 157, 195, 177
  ];
  var decoded = utf8.decode(bytes);

  print('Encoded bytes: $encoded');
  print('Decoded string: $decoded');
}

Output:

Encoded bytes: [195, 142, 195, 177, 197, 163, 195, 169, 114, 195, 177, 195, 165, 197, 163, 195, 174, 195, 182, 195, 177, 195, 165, 196, 188, 195, 174, 197, 190, 195, 165, 197, 163, 195, 174, 225, 187, 157, 195, 177]
Decoded string: Îñţérñåţîöñåļîžåţîờñ

ASCII

An AsciiCodec encodes strings as ASCII codes stored as bytes and decodes ASCII bytes to strings. Not all characters can be represented as ASCII, so not all strings can be successfully converted.

The ascii is the default implementation of AsciiCodec.

Example:

var encoded = ascii.encode('This is ASCII!');
var decoded = ascii.decode([0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
                            0x20, 0x41, 0x53, 0x43, 0x49, 0x49, 0x21]

Latin-1

A Latin1Codec encodes strings to ISO Latin-1 (aka ISO-8859-1) bytes and decodes Latin-1 bytes to strings. Not all characters can be represented as Latin-1, so not all strings can be successfully converted.

The latin1 is the default implementation of Latin1Codec.

Example:

var encoded = latin1.encode('blåbærgrød');
var decoded = latin1.decode([0x62, 0x6c, 0xe5, 0x62, 0xe6,
                             0x72, 0x67, 0x72, 0xf8, 0x64]);

Base64

A Base64Codec encodes bytes using the default base64 alphabet, decodes using both the base64 and base64url alphabets, does not allow invalid characters and requires padding.

The base64 is the default implementation of Base64Codec.

Example:

var encoded = base64.encode([0x62, 0x6c, 0xc3, 0xa5, 0x62, 0xc3, 0xa6,
                             0x72, 0x67, 0x72, 0xc3, 0xb8, 0x64]);
var decoded = base64.decode('YmzDpWLDpnJncsO4ZAo=');