-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path033_json.dart
174 lines (158 loc) · 2.74 KB
/
033_json.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import 'dart:convert';
import 'dart:io';
var jsonString = '''
[
{"score": 40},
{"score": 80}
]
''';
// Json Decode
void foo1() {
var scores = jsonDecode(jsonString);
print("${scores.runtimeType}"); // List<dynamic>
assert(scores is List);
if (scores case [List a]) {
print("result is : $a");
}
var firstScore = scores[0];
assert(firstScore is Map);
assert(firstScore['score'] == 40);
}
// Json Encode
void foo2() {
var scores = [
{'score': 40},
{'score': 80},
{'score': 100, 'overtime': true, 'special_guest': null}
];
var jsonText = jsonEncode(scores);
assert(jsonText ==
'[{"score":40},{"score":80},'
'{"score":100,"overtime":true,'
'"special_guest":null}]');
}
// UTF-8 Encode And Decode
void foo3() {
List<int> utf8Bytes = [
0xc3,
0x8e,
0xc3,
0xb1,
0xc5,
0xa3,
0xc3,
0xa9,
0x72,
0xc3,
0xb1,
0xc3,
0xa5,
0xc5,
0xa3,
0xc3,
0xae,
0xc3,
0xb6,
0xc3,
0xb1,
0xc3,
0xa5,
0xc4,
0xbc,
0xc3,
0xae,
0xc5,
0xbe,
0xc3,
0xa5,
0xc5,
0xa3,
0xc3,
0xae,
0xe1,
0xbb,
0x9d,
0xc3,
0xb1
];
var funnyWord = utf8.decode(utf8Bytes);
assert(funnyWord == 'Îñţérñåţîöñåļîžåţîờñ');
}
void foo4() {
List<int> lst = [
0xc3,
0x8e,
];
String decodedText = utf8.decode(lst);
print(decodedText); // Çıktı: ı
}
void foo5() async {
// Bu kısmı inputStream adlı bir giriş akışı ile doldurmalısınız.
// Örneğin, bir dosyadan okuma yapmak için File sınıfını kullanabilirsiniz:
final file = File('module_ex.dart');
final inputStream = file.openRead();
var lines = utf8.decoder.bind(inputStream).transform(const LineSplitter());
try {
await for (final line in lines) {
print('Got ${line.length} characters from stream');
}
print('Dosya artık kapandı');
} catch (e) {
print(e);
}
}
void foo6() {
String name = "Abdulkerim Akan";
List<int> encodedName = utf8.encode(name);
print(encodedName);
}
void foo7() {
List<int> utf8Bytes = [
0xc3,
0x8e,
0xc3,
0xb1,
0xc5,
0xa3,
0xc3,
0xa9,
0x72,
0xc3,
0xb1,
0xc3,
0xa5,
0xc5,
0xa3,
0xc3,
0xae,
0xc3,
0xb6,
0xc3,
0xb1,
0xc3,
0xa5,
0xc4,
0xbc,
0xc3,
0xae,
0xc5,
0xbe,
0xc3,
0xa5,
0xc5,
0xa3,
0xc3,
0xae,
0xe1,
0xbb,
0x9d,
0xc3,
0xb1
];
List<int> encoded = utf8.encode('Îñţérñåţîöñåļîžåţîờñ');
assert(encoded.length == utf8Bytes.length);
for (int i = 0; i < encoded.length; i++) {
assert(encoded[i] == utf8Bytes[i]);
}
}
void main() => foo7();