Aus öffentlichen Paketdaten erstellte Orientierung.
Typed_JSON ist eine Dart-Bibliothek, die entwickelt wurde, um die manuelle JSON-Serrialisierung in Projekten jeder Größe zu vereinfachen. Sie bietet eine saubere, typensichere Möglichkeit, Dart-Objekte in JSON und umgekehrt zu konvertieren, ohne auf Reflexion oder Laufzeittypinformationen angewiesen zu sein. Durch die Generierung effizienten Serialisierungs-Codes zur Kompilierzeit wird Leistung sichergestellt und das Risiko von Laufzeitfehlern verringert. Die Bibliothek lässt sich nahtlos in Flutter und andere Dart-Anwendungen integrieren und unterstützt alle wichtigen Plattformen wie Android, iOS, Web, macOS, Windows und Linux.
Anwendungsfälle
Manuelle JSON-Serrialisierung in großskaligen Anwendungen
Typsichere Datenmodellverwaltung
Plattformübergreifender Datenaustausch
Verringern der Abhängigkeit von dynamischen Typen
leistungskritische JSON-Operationen
Benutzerdefinierte Serialisierungslogik
Hauptmerkmale
Codegenerierung zur Kompilierzeit
Keine Laufzeitreflexion
Stark typisiert mit minimalem Boilerplate
Unterstützung für verschachtelte und komplexe Typen
Erweiterbar über benutzerdefinierte Serialisierer
Leicht und schnell
Geeignet für
Entwickler, die volle Kontrolle über die JSON-Serrialisierung suchen
Projekte, die hohe Leistung und Typensicherheit erfordern
Teams, die von Drittanbieter-JSON-Mappern unabhängig bleiben
Anwendungen mit komplexen Datenmodellen
Flutter-Apps, die zuverlässige Datenpersistenz benötigen
Hinweise
Erfordert Schritt zur Codegenerierung
Lernkurve für die Einrichtung eines benutzerdefinierten Serialisators
Beschränkt auf manuell definierte Serialisierungslogik
Nicht ideal für sich schnell ändernde Datenstrukturen
Kann die Build-Zeit bei vielen Modellen leicht erhöhen
Original-README
Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.
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.