Created
November 24, 2020 06:48
-
-
Save pasindud/cf96b8b2117643c9e6e2b3a6b51cbe9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:typed_data'; | |
import 'dart:convert'; | |
void main() { | |
final j = {'k' : 'ජෝන්', 'non_unicode': 'abc'}; | |
/// Existing Version | |
final oldencoded = json.encode(j); | |
final oldutfencoded = Uint8List.fromList(oldencoded.codeUnits); | |
final oldutfdecoded = String.fromCharCodes(oldutfencoded); | |
final olddecoded = json.decode(oldutfdecoded); | |
print(j['non_unicode'] == olddecoded['non_unicode']); // True | |
print(j['k'] == olddecoded['k']); // False | |
print(j.toString() == olddecoded.toString()); // False | |
/// New Version | |
final encoded = json.encode(j); | |
final utfencoded = utf8.encoder.convert(encoded); | |
final utfdecoded = utf8.decoder.convert(utfencoded); | |
final decoded = json.decode(utfdecoded); | |
print(j['non_unicode'] == decoded['non_unicode']); // True | |
print(j['k'] == decoded['k']); // True | |
print(j.toString() == decoded.toString()); // True | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment