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

A Dart package for mapping JSON data to strongly typed models with minimal boilerplate.

static_mapper #

A simple and flexible library for JSON mapping in Dart using static typed property access.

✨ Features #

  • Static-typed model mapping from JSON
  • Fallback values for missing or invalid fields
  • Nested object and list support
  • Simple and composable design

🚀 Getting Started #

Add the dependency in your pubspec.yaml:

dependencies:
  static_mapper: latest

🔧 Define Models #

import 'package:static_mapper/static_mapper.dart';

class User extends BaseJsonModel {
  User(super.json);

  JsonProperty<String> get id => prop('id');
  JsonProperty<String> get name => prop('name');
  JsonProperty<int> get age => prop('age', fallback: 0);
  JsonProperty<bool> get isActive => prop('isActive', fallback: false);

  @override
  Map<String, dynamic> toJson() => json;
}

✅ Usage Example #

final userJson = {'id': '001', 'name': 'Bob'};
final user = User(userJson);

print(user.name.value);     // Bob
print(user.age.value);      // 0 (fallback)
user.age.value = 42;

print(user.toJson());       // {id: 001, name: Bob, age: 42, isActive: false}

🔄 Nested Models #

class Post extends BaseJsonModel {
  Post(super.json);

  JsonProperty<String> get title => prop('title');
  JsonProperty<User?> get author => obj('author', fromJson: (json) => User(json));

  @override
  Map<String, dynamic> toJson() => json;
}

📄 License #

MIT


Made with ❤️ using Dart

5
likes
0
points
35
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart package for mapping JSON data to strongly typed models with minimal boilerplate.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on static_mapper