safe_json 1.1.1 copy "safe_json: ^1.1.1" to clipboard
safe_json: ^1.1.1 copied to clipboard

Primary purpose of this library is to find out backend error(incomplete data) at the first time.

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 get JSON/Map/Dictionary values without crash.
  • Provide default values when fails.
  • Log Error with description and file location.

Examples #

import 'package:safe_json/safe_json.dart';

class User {
  final String name;
  final int age;
  final String school;
  final List<String> articles;
  final List<Vehicle> vehicles;

  User(
      {required this.name,
        required this.age,
        required this.school,
        required this.articles,
        required this.vehicles});

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      name: json.safeString('name', defaultValue: 'John'),
      age: json.safeInt('age'),
      school: json.safeString('school', defaultValue: 'MIT'),
      articles: json.safeList<String>('articles'),
      vehicles: json.safeList<Vehicle>('vehicles',
          itemParse: (json) => Vehicle.fromJson(json)),
    );
  }
}

class Vehicle {
  final String make;
  final String model;

  Vehicle({required this.make, required this.model});

  factory Vehicle.fromJson(Map<String, dynamic> json) {
    return Vehicle(
      make: json.safeString('make'),
      model: json.safeString('model'),
    );
  }

  @override
  String toString() {
    return '$make $model';
  }
}

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

  User user = User.fromJson(json);

  print(user.name); // John
  print(user.age); // 30
  print(user.school); // MIT
  print(user.articles); // [article1, article2]
  print(user.vehicles); // [Toyota Camry, Honda Accord]
}

//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 #

┌────────────────────────────────────────────────────────────────────────────────────────────────────
│ ⚠️ json['name'] return default value
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
│ value is null
│ package:your-project-name/models/user_model.dart:22:19
│ package:your-project-name/pages/home_page.dart:75:29
└────────────────────────────────────────────────────────────────────────────────────────────────────
1
likes
160
points
94
downloads

Publisher

unverified uploader

Weekly Downloads

Primary purpose of this library is to find out backend error(incomplete data) at the first time.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on safe_json