Package selection guidance compiled from public package information, community data, and project usage.
Typed_JSON is a Dart library designed to simplify manual JSON serialization across projects of any scale. It provides a clean, type-safe way to convert Dart objects to and from JSON without relying on reflection or runtime type information. By generating efficient serialization code at compile time, it ensures performance and reduces the risk of runtime errors. The library integrates seamlessly into Flutter and other Dart applications, supporting all major platforms including Android, iOS, Web, macOS, Windows, and Linux.
Use cases
Manual JSON serialization in large-scale apps
Type-safe data model handling
Cross-platform data exchange
Reducing reliance on dynamic types
Performance-critical JSON operations
Custom serialization logic
Key features
Compile-time code generation
No runtime reflection
Strong typing with minimal boilerplate
Support for nested and complex types
Extensible via custom serializers
Lightweight and fast
Suitable for
Developers seeking full control over JSON serialization
Projects requiring high performance and type safety
Teams avoiding dependency on third-party JSON mappers
Applications with complex data models
Flutter apps needing reliable data persistence
Considerations
Requires code generation step
Learning curve for custom serializer setup
Limited to manually defined serialization logic
Not ideal for rapidly changing data structures
May increase build time slightly with many models
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
Typed JSON
A library intended to bring manual JSON serialization to the Dart projects of all scales.
Features:
Human readable and maintainable API
Strict compile-time and runtime types checks
Nested objects parsing
Lists of objects parsing
Exceptions that include a JSON key path with an error
try {
int? intVal = json["year"].intValue;
String? stringVal = json["title"].stringValue;
double? doubleVal = json["price"].doubleValue;
num? numValue = json["price"].numValue;
bool? boolVal = json["inStock"].boolValue;
} onJsonValueExceptioncatch (e) {
print(e.value) // The value of the faulty fieldprint(e) // "Unable to parse the value at the/path/to/field"...
}
In this case, the JsonValueException will be thrown if the type of the value is incorrect. The null value is permitted.
Required values
If the value must not be null, you can use the *OrException properties:
try {
int intVal = json["year"].intOrException;
String stringVal = json["title"].stringOrException;
double doubleVal = json["price"].doubleOrException;
num numValue = json["price"].numOrException;
bool boolVal = json["inStock"].boolOrException;
} onJsonValueExceptioncatch (e) {
print(e.value) // The value of the faulty fieldprint(e) // "Unable to parse the required value at the/path/to/field"...
}
In this case, the JsonValueException will be thrown if the type of the value is incorrect or the value is null.
In the example above the keys "root" and "nested" must not be null, otherwise the JsonException will be thrown.
If you need to parse the nested JSON with keys that can be null, you should check it explicitly.
final value = j["inexisted"].optional["nested"].stringValue; // null
In the example, we marked the path "inexisted" as optional. The JSON parser will know that the "inexisted" can be null. And the "nested" and all the keys to the right side of "inexisted" can be null as well.
Null checks
You can check if the JSON value is not null:
final json = Json.empty(); // json with nullprint(json != null) // trueprint(json.isExist) // false
You can check if the JSON object contains the element with the giving name:
final json = Json.object(); // json with {}print(json.isExist) // trueprint(json["key"].isExist) // false
Now you can work with JSON values of the custom type using the get and set methods.
final valueJson = Json.empty();
valueJson.set(Status.active, StatusAdapter());
Status? valueStatus = valueJson.get(StatusAdapter());
final objectJson = Json.object();
objectJson["key"].set(Status.active, StatusAdapter());
Status? ojectStatus = objectJson["key"].get(StatusAdapter());
If the value must not be null, you can use the getRequired method.
final valueJson = Json.empty();
valueJson.set(Status.active, StatusAdapter());
Status valueStatus = valueJson.getRequired(StatusAdapter());
If the custom adapter returns null, the getRequired method will throw a JsonValueException.
try {
Json("incorrect").getRequired(StatusAdapter());
} onJsonValueExceptioncatch (e) {
print(e.value) // The value of the faulty fieldprint(e) // "Unable to parse the required value at the/path/to/field"...
}
Authors
Evgeniy Safronov (evsafronov.personal@gmail.com)
Alexander Smetannikov (alexsmetdev@gmail.com)
License
Typed JSON is available under the MIT license. See the LICENSE file for more info.