Skip to content

Instantly share code, notes, and snippets.

@pasindud
Created November 24, 2020 06:48
Show Gist options
  • Save pasindud/cf96b8b2117643c9e6e2b3a6b51cbe9e to your computer and use it in GitHub Desktop.
Save pasindud/cf96b8b2117643c9e6e2b3a6b51cbe9e to your computer and use it in GitHub Desktop.
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