json_helpers 0.1.0 json_helpers: ^0.1.0 copied to clipboard
The `json_helpers` contains functions that make it easier decoding JSON objects directly from strings, lists and maps.
json_helpers #
The json_helpers
contains functions that make it easier decoding JSON objects directly from strings, lists and maps.
Version 0.0.1
Allows you to simplify decoding JSON objects directly from String
, List
and Map
values.
Examples (better examples comming soon):
import 'dart:convert';
import 'package:json_helper/json_helper.dart';
import 'package:test/test.dart';
void main() {
final data1 = Data(name: 'test1');
final data2 = Data(name: 'test1');
final dataList = [data1, data2];
final dataMapList = [data1.toJson(), data2.toJson()];
final data1AsString = jsonEncode(data1);
Map data1AsMap = data1.toJson();
final dataListAsString = jsonEncode(dataList);
test('String.decodeJson', () {
final result = data1AsString.decodeJson((e) => Data.fromJson(e));
expect(result, data1);
});
test('String.decodeJsonList', () {
final result = dataListAsString.decodeJsonList((e) => Data.fromJson(e));
expect(result, dataList);
});
test('Map.fromJson', () {
final result = data1AsMap.fromJson((e) => Data.fromJson(e));
expect(result, data1);
});
test('List.fromJson', () {
final result = dataMapList.fromJson((e) => Data.fromJson(e));
expect(result, dataList);
});
}
class Data {
final String name;
Data({required this.name});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(name: (json['name'] as String?) ?? '');
}
@override
bool operator ==(other) {
if (other is Data) {
return name == other.name;
}
return false;
}
Map<String, dynamic> toJson() => {'name': name};
}
New features will be added soon...