simdjson_flutter 0.0.1
simdjson_flutter: ^0.0.1 copied to clipboard
Fast JSON encoding and decoding powered by simdjson (SIMD-accelerated).
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:simdjson_flutter/simdjson_flutter.dart' as simdjson;
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _decodeResult = '';
String _encodeResult = '';
String _roundtripResult = '';
@override
void initState() {
super.initState();
_runExamples();
}
void _runExamples() {
const sampleJson = '''
{
"name": "simdjson",
"version": 4,
"fast": true,
"scores": [9.8, 9.9, 10.0],
"meta": {"author": "Daniel Lemire", "language": null}
}
''';
final decoded = simdjson.jsonDecode(sampleJson);
final name = (decoded as Map)['name'];
final scores = (decoded['scores'] as List).cast<double>();
setState(() {
_decodeResult =
'name=$name version=${decoded['version']} '
'fast=${decoded['fast']} avg=${scores.reduce((a, b) => a + b) / scores.length}';
});
final data = {
'library': 'simdjson_flutter',
'simd': true,
'numbers': [1, 2, 3.14],
'nested': {'key': 'value', 'nothing': null},
};
final encoded = simdjson.jsonEncode(data);
setState(() => _encodeResult = encoded);
final roundtripped = simdjson.jsonDecode(encoded) as Map;
setState(() =>
_roundtripResult = 'library=${roundtripped['library']} '
'simd=${roundtripped['simd']} '
'numbers=${roundtripped['numbers']}');
}
@override
Widget build(BuildContext context) {
const style = TextStyle(fontSize: 14, fontFamily: 'monospace');
const spacer = SizedBox(height: 12);
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('simdjson_flutter example')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Decode result:', style: TextStyle(fontWeight: FontWeight.bold)),
spacer,
Text(_decodeResult, style: style),
const Divider(height: 32),
const Text('Encode result:', style: TextStyle(fontWeight: FontWeight.bold)),
spacer,
Text(_encodeResult, style: style),
const Divider(height: 32),
const Text('Round-trip result:', style: TextStyle(fontWeight: FontWeight.bold)),
spacer,
Text(_roundtripResult, style: style),
],
),
),
),
);
}
}