Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:convert
dart:convert
description

dart:convert library

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

In addition to converters for common data representations, this libraryprovides support for implementing converters in a way which makes them easyto 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 ofJsonCodec andUtf8Codec, namedjson andutf8, respectively.

JSON

JSON is a simple text format for representing structured objects andcollections.

AJsonCodec encodes JSON objects to strings and decodes strings toJSON objects. Thejson encoder/decoder transforms between strings andobject structures, such as lists and maps, using the JSON format.

Thejson is the default implementation ofJsonCodec.

Examples

var encoded = json.encode([1, 2, { "a": null }]);var decoded = json.decode('["foo", { "bar": 499 }]');

For more information, see alsoJsonEncoder andJsonDecoder.

UTF-8

AUtf8Codec encodes strings to UTF-8 code units (bytes) and decodesUTF-8 code units to strings.

Theutf8 is the default implementation ofUtf8Codec.

Example:

var encoded = utf8.encode('Îñţérñåţîöñåļîžåţîờñ');var decoded = utf8.decode([  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]);

For more information, see alsoUtf8Encoder andUtf8Decoder.

ASCII

AnAsciiCodec encodes strings as ASCII codes stored as bytes and decodesASCII bytes to strings. Not all characters can be represented as ASCII, sonot all strings can be successfully converted.

Theascii is the default implementation ofAsciiCodec.

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]);

For more information, see alsoAsciiEncoder andAsciiDecoder.

Latin-1

ALatin1Codec encodes strings to ISO Latin-1 (aka ISO-8859-1) bytesand decodes Latin-1 bytes to strings. Not all characters can be representedas Latin-1, so not all strings can be successfully converted.

Thelatin1 is the default implementation ofLatin1Codec.

Example:

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

For more information, see alsoLatin1Encoder andLatin1Decoder.

Base64

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

Thebase64 is the default implementation ofBase64Codec.

Example:

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

For more information, see alsoBase64Encoder andBase64Decoder.

Converters

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

const showLineNumbers = true;var lineNumber = 1;var stream = File('quotes.txt').openRead();stream.transform(utf8.decoder)      .transform(const LineSplitter())      .forEach((line) {        if (showLineNumbers) {          stdout.write('${lineNumber++} ');        }        stdout.writeln(line);      });

See the documentation for theCodec andConverter classesfor information about creating your own converters.

HTML Escape

HtmlEscape converter escapes characters with special meaning in HTML.The converter finds characters that are significant in HTML source andreplaces them with corresponding HTML entities.

Custom escape modes can be created using theHtmlEscapeMode.newconstructor.

Example:

const htmlEscapeMode = HtmlEscapeMode(  name: 'custom',  escapeLtGt: true,  escapeQuot: false,  escapeApos: false,  escapeSlash: false, );const HtmlEscape htmlEscape = HtmlEscape(htmlEscapeMode);String unescaped = 'Text & subject';String escaped = htmlEscape.convert(unescaped);print(escaped); // Text &amp; subjectunescaped = '10 > 1 and 1 < 10';escaped = htmlEscape.convert(unescaped);print(escaped); // 10 &gt; 1 and 1 &lt; 10unescaped = "Single-quoted: 'text'";escaped = htmlEscape.convert(unescaped);print(escaped); // Single-quoted: 'text'unescaped = 'Double-quoted: "text"';escaped = htmlEscape.convert(unescaped);print(escaped); // Double-quoted: "text"unescaped = 'Path: /system/';escaped = htmlEscape.convert(unescaped);print(escaped); // Path: /system/

Classes

AsciiCodec
AnAsciiCodec allows encoding strings as ASCII bytesand decoding ASCII bytes to strings.
AsciiDecoder
Converts ASCII bytes to string.
AsciiEncoder
Converts strings of only ASCII characters to bytes.
Base64Codec
Abase64 encoder and decoder.
Base64Decoder
Decoder for base64 encoded data.
Base64Encoder
Base64 and base64url encoding converter.
ByteConversionSink
TheByteConversionSink provides an interface for converters toefficiently transmit byte data.
ChunkedConversionSink<T>
AChunkedConversionSink is used to transmit data more efficiently betweentwo converters during chunked conversions.
ClosableStringSink
AClosableStringSink extends theStringSink interface by adding aclose method.
Codec<S,T>
ACodec encodes and (if supported) decodes data.
Converter<S,T>
AConverter converts data from one representation into another.
Encoding
Open-ended set of encodings.
HtmlEscape
Converter which escapes characters with special meaning in HTML.
HtmlEscapeMode
HTML escape modes.
JsonCodec
AJsonCodec encodes JSON objects to strings and decodes strings toJSON 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
ALatin1Codec encodes strings to ISO Latin-1 (aka ISO-8859-1) bytesand 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
AStreamTransformer that splits aString into individual lines.
StringConversionSink
A sink for converters to efficiently transmit String data.
Utf8Codec
AUtf8Codec encodes strings to utf-8 code units (bytes) and decodesUTF-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 ofunsigned 8-bit integers).

Constants

ascii→ constAsciiCodec
An instance of the default implementation of theAsciiCodec.
base64→ constBase64Codec
Abase64 encoder and decoder.
base64Url→ constBase64Codec
Abase64url encoder and decoder.
htmlEscape→ constHtmlEscape
AString converter that converts characters to HTML entities.
json→ constJsonCodec
An instance of the default implementation of theJsonCodec.
latin1→ constLatin1Codec
An instance of the default implementation of theLatin1Codec.
unicodeBomCharacterRune→ constint
The Unicode Byte Order Marker (BOM) characterU+FEFF.
unicodeReplacementCharacterRune→ constint
The Unicode Replacement characterU+FFFD (�).
utf8→ constUtf8Codec
An instance of the default implementation of theUtf8Codec.

Functions

base64Decode(Stringsource)Uint8List
Decodesbase64 orbase64url encoded bytes.
base64Encode(List<int>bytes)String
Encodesbytes usingbase64 encoding.
base64UrlEncode(List<int>bytes)String
Encodesbytes usingbase64url encoding.
jsonDecode(Stringsource, {Object?reviver(Object?key,Object?value)?})→ dynamic
Parses the string and returns the resulting Json object.
jsonEncode(Object?object, {Object?toEncodable(Object?nonEncodable)?})String
Convertsobject to a JSON string.

Typedefs

ByteConversionSinkBase =ByteConversionSink
This class provides a base-class for converters that need to accept byteinputs.
StringConversionSinkBase =StringConversionSink
This class provides a base-class for converters that need to accept Stringinputs.
StringConversionSinkMixin =StringConversionSink
This class provides a mixin for converters that need to accept Stringinputs.

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.
  1. Dart
  2. dart:convert
DartSDK
  1. Libraries
  2. Core
  3. dart:async
  4. dart:collection
  5. dart:convert
  6. dart:core
  7. dart:developer
  8. dart:math
  9. dart:typed_data
  10. VM
  11. dart:ffi
  12. dart:io
  13. dart:isolate
  14. dart:mirrors
  15. Web
  16. package:webopen_in_new
  17. dart:js_interop
  18. dart:js_interop_unsafe
  19. Web (Legacy)
  20. dart:html
  21. dart:indexed_db
  22. dart:js
  23. dart:js_util
  24. dart:svg
  25. dart:web_audio
  26. dart:web_gl
dart:convert library

[8]ページ先頭

©2009-2025 Movatter.jp