smart_json_parser 1.0.0 copy "smart_json_parser: ^1.0.0" to clipboard
smart_json_parser: ^1.0.0 copied to clipboard

Smart and safe JSON parser for Dart and Flutter. Parse int, double, bool, and string values safely without runtime crashes. Useful for handling dynamic APIs and inconsistent JSON responses.

Smart JSON Parser #

smart_json_parser is a lightweight JSON parsing helper for Dart and Flutter that helps you safely read values from dynamic JSON without runtime crashes.

Many real‑world APIs return inconsistent types, for example:

{
  "age": "25",
  "price": "12.5",
  "active": "true"
}

age should be an int, price a double, and active a bool, but everything comes back as String.
smart_json_parser provides a small set of helpers that safely convert these values to the types you expect.


Features #

  • Safe JSON parsing
  • Handles null values
  • Supports int, double, bool, and String
  • Lightweight and dependency‑free
  • Works with Flutter and pure Dart

Installation #

Add smart_json_parser to your pubspec.yaml:

dependencies:
  smart_json_parser: ^1.0.0

Then run:

dart pub get

For Flutter:

flutter pub get

Usage #

Import the package:

import 'package:smart_json_parser/smart_json_parser.dart';

Basic usage:

final json = {
  'age': '25',
  'price': '12.5',
  'active': 'true',
};

final age = SmartJson.intValue(json['age']);
final price = SmartJson.doubleValue(json['price']);
final active = SmartJson.boolValue(json['active']);
final name = SmartJson.stringValue(json['name']); // falls back to default ""

Each helper:

  • Accepts dynamic input.
  • Handles null values.
  • Returns a sensible default (0, 0.0, false, or '') when parsing fails, unless you provide a custom defaultValue.

Real World API Example #

Imagine an HTTP API returning this JSON:

{
  "user_id": "42",
  "score": "99.5",
  "is_premium": "yes",
  "display_name": null
}

You can safely parse it like this:

final response = <String, dynamic>{
  'user_id': '42',
  'score': '99.5',
  'is_premium': 'yes',
  'display_name': null,
};

final userId = SmartJson.intValue(response['user_id'], defaultValue: 0);
final score = SmartJson.doubleValue(response['score'], defaultValue: 0.0);
final isPremium = SmartJson.boolValue(response['is_premium'], defaultValue: false);
final displayName = SmartJson.stringValue(response['display_name'], defaultValue: 'Guest');

This code will not throw, even if the backend changes types or sends null.


Supported Types #

SmartJson currently supports:

  • int via SmartJson.intValue(...)
  • double via SmartJson.doubleValue(...)
  • bool via SmartJson.boolValue(...)
  • String via SmartJson.stringValue(...)

Each helper accepts a defaultValue parameter that is returned when parsing fails.


Example Project #

A runnable example is available in example/main.dart:

import 'package:smart_json_parser/smart_json_parser.dart';

void main() {
  final json = <String, dynamic>{
    'age': '30',
    'salary': '55000.75',
    'verified': 'true',
    'name': 'Richa',
  };

  final age = SmartJson.intValue(json['age']);
  final salary = SmartJson.doubleValue(json['salary']);
  final verified = SmartJson.boolValue(json['verified']);
  final name = SmartJson.stringValue(json['name']);

  print('Age: $age');
  print('Salary: $salary');
  print('Verified: $verified');
  print('Name: $name');
}

Run it from the package root:

dart run example/main.dart

Contributing #

Contributions are welcome!
If you have ideas for new helpers, better parsing rules, or documentation improvements:

  • Open an issue in the GitHub issue tracker.
  • Submit a pull request with tests and documentation updates.

Before submitting changes, please run:

dart format .
dart analyze
dart test

License #

This project is licensed under the MIT License.
See the LICENSE file for details.

6
likes
0
points
121
downloads

Publisher

unverified uploader

Weekly Downloads

Smart and safe JSON parser for Dart and Flutter. Parse int, double, bool, and string values safely without runtime crashes. Useful for handling dynamic APIs and inconsistent JSON responses.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on smart_json_parser