Encoders and decoders for converting between different data representations, including JSON and UTF-8.

In addition to converters for common data representations, this library provides support for implementing converters in a way which makes them easy to chain and to use with streams.

To use this library in your code:

import 'dart:convert';

Two commonly used converters are the top-level instances of JsonCodec and Utf8Codec, named json and utf8, respectively.

JSON is a simple text format for representing structured objects and collections. The JSON encoder/decoder transforms between strings and object structures, such as lists and maps, using the JSON format.

UTF-8 is a common variable-width encoding that can represent every character in the Unicode character set. The UTF-8 encoder/decoder transforms between Strings and bytes.

Converters are often used with streams to transform the data that comes through the stream as it becomes available. The following code uses two converters. The first is a UTF-8 decoder, which converts the data from bytes to UTF-8 as it's read from a file, The second is an instance of LineSplitter, which splits the data on newline boundaries.

int lineNumber = 1;
Stream<List<int>> stream = new File('quotes.txt').openRead();

stream.transform(utf8.decoder)
      .transform(const LineSplitter())
      .listen((line) {
        if (showLineNumbers) {
          stdout.write('${lineNumber++} ');
        }
        stdout.writeln(line);
      });

See the documentation for the Codec and Converter classes for information about creating your own converters.

Classes

AsciiCodec
An AsciiCodec allows encoding strings as ASCII bytes and decoding ASCII bytes to strings.
AsciiDecoder
AsciiEncoder
This class converts strings of only ASCII characters to bytes.
Base64Codec
A base64 encoder and decoder. [...]
Base64Decoder
Decoder for base64 encoded data. [...]
Base64Encoder
Base64 and base64url encoding converter. [...]
ByteConversionSink
The ByteConversionSink provides an interface for converters to efficiently transmit byte data. [...]
ByteConversionSinkBase
This class provides a base-class for converters that need to accept byte inputs.
ChunkedConversionSink<T>
A ChunkedConversionSink is used to transmit data more efficiently between two converters during chunked conversions. [...]
ClosableStringSink
A ClosableStringSink extends the StringSink interface by adding a close method.
Codec<S, T>
A Codec encodes and (if supported) decodes data. [...]
Converter<S, T>
A Converter converts data from one representation into another. [...]
Encoding
Open-ended Encoding enum.
HtmlEscape
Converter which escapes characters with special meaning in HTML. [...]
HtmlEscapeMode
HTML escape modes. [...]
JsonCodec
A JsonCodec encodes JSON objects to strings and decodes strings to JSON objects. [...]
JsonDecoder
This class parses JSON strings and builds the corresponding objects.
JsonEncoder
This class converts JSON objects to strings.
JsonUtf8Encoder
Encoder that encodes a single object as a UTF-8 encoded JSON string. [...]
Latin1Codec
A Latin1Codec encodes strings to ISO Latin-1 (aka ISO-8859-1) bytes and decodes Latin-1 bytes to strings.
Latin1Decoder
This class converts Latin-1 bytes (lists of unsigned 8-bit integers) to a string.
Latin1Encoder
This class converts strings of only ISO Latin-1 characters to bytes.
LineSplitter
A StreamTransformer that splits a String into individual lines. [...]
StringConversionSink
This class provides an interface for converters to efficiently transmit String data. [...]
StringConversionSinkBase
This class provides a base-class for converters that need to accept String inputs.
StringConversionSinkMixin
This class provides a mixin for converters that need to accept String inputs.
Utf8Codec
A Utf8Codec encodes strings to utf-8 code units (bytes) and decodes UTF-8 code units to strings.
Utf8Decoder
This class converts UTF-8 code units (lists of unsigned 8-bit integers) to a string.
Utf8Encoder
This class converts strings to their UTF-8 code units (a list of unsigned 8-bit integers).

Constants

ASCII AsciiCodec
Deprecated, use ascii instead.
ascii
ascii AsciiCodec
An instance of the default implementation of the AsciiCodec. [...]
const AsciiCodec()
BASE64 Base64Codec
Deprecated, use base64 instead.
base64
base64 Base64Codec
A base64 encoder and decoder. [...]
const Base64Codec()
BASE64URL Base64Codec
Deprecated, use base64Url instead.
base64Url
base64Url Base64Codec
A base64url encoder and decoder. [...]
const Base64Codec.urlSafe()
HTML_ESCAPE HtmlEscape
Deprecated, use htmlEscape instead.
htmlEscape
htmlEscape HtmlEscape
A String converter that converts characters to HTML entities. [...]
const HtmlEscape()
JSON JsonCodec
Deprecated, use json instead.
json
json JsonCodec
An instance of the default implementation of the JsonCodec. [...]
const JsonCodec()
LATIN1 Latin1Codec
Deprecated, use latin1 instead.
latin1
latin1 Latin1Codec
An instance of the default implementation of the Latin1Codec. [...]
const Latin1Codec()
UNICODE_BOM_CHARACTER_RUNE int
Deprecated, use unicodeBomCharacterRune instead.
unicodeBomCharacterRune
UNICODE_REPLACEMENT_CHARACTER_RUNE int
Deprecated, use unicodeReplacementCharacterRune instead.
unicodeReplacementCharacterRune
unicodeBomCharacterRune int
The Unicode Byte Order Marker (BOM) character U+FEFF.
0xFEFF
unicodeReplacementCharacterRune int
The Unicode Replacement character U+FFFD (�).
0xFFFD
UTF8 Utf8Codec
Deprecated, use tf8Codec instead.
utf8
utf8 Utf8Codec
An instance of the default implementation of the Utf8Codec. [...]
const Utf8Codec()

Exceptions / Errors

JsonCyclicError
Reports that an object could not be stringified due to cyclic references. [...]
JsonUnsupportedObjectError
Error thrown by JSON serialization if an object cannot be serialized. [...]