safe_json

pub package CI Code size License

The primary goal of this library is to find out bugs in the early stages of development when get JSON values.

Features

  • Safely parse JSON values to various data types.
  • Provide default values when parsing fails.
  • Error logging with file location.

Examples

Map<String, dynamic> json = {
  'age': '30.0',
  'articles': [
    'article1',
    'article2',
  ],
  'vehicles': [
    {'make': 'Toyota', 'model': 'Camry'},
    {'make': 'Honda', 'model': 'Accord'},
  ],
};

final name = json.safeString('name', defaultValue: 'John');

final age = json.safeInt('age');

final articles = json.safeList<String>('articles');

final vehicles = json.safeList<Vehicle>('vehicles', itemParse: (json) => Vehicle.fromJson(json));

//set to true if want to disable error logging (usually in production)
SafeJsonLibrary.skipLog = true; 

//set the stacktrace count showing in console
SafeJsonLibrary.stackTraceCount = 1; 

Console Logs

Libraries

safe_json
Null Safety JSON decoding library for Dart and Flutter