rj_safe_parser 0.4.0
rj_safe_parser: ^0.4.0 copied to clipboard
RJ Safe Parser: Annotation-based Dart generator for JSON parsing, mapping, and validation with zero boilerplate.
0.4.0 #
Phase 2a — enum support and Map<String, V> support
New: enum field support #
Dart enum fields are now fully supported — annotate with @RjEnum() or
leave it bare for by-name matching, or use @RjEnum(byIndex: true) for
ordinal matching.
enum Status { active, inactive, pending }
enum Priority { low, medium, high }
@RjSafeParsable()
class Task {
@RjEnum() // 'active' → Status.active
final Status status;
@RjEnum(byIndex: true) // 0 → Priority.low, 2 → Priority.high
final Priority priority;
}
- By name (default): JSON value must be the
.namestring of an enum constant. - By index: JSON value must be an
int(or numeric string) matching the constant's position invalues. - Nullable
Status?fields: absent key or explicitnull→null. toMap()serialises back to.nameor.indexautomatically.- Runtime: new
rjCoerceEnum/rjCoerceEnumNullablehelpers inrj_converters.dart. - Schema: new
RjEnumSchemainrj_schema.dartcarries thevalueslist,byIndexflag,jsonKey, andisNullable.
New: Map<String, V> field support #
Fields typed as Map<String, V> are now generated and parsed correctly.
The map's values are individually coerced using the same smart-coercion
engine as all other fields.
@RjSafeParsable()
class Config {
final Map<String, String> labels; // {'env': 'prod'}
final Map<String, int> counts; // {'retry': '3'} → {'retry': 3}
final Map<String, bool> flags; // {'darkMode': 1} → {'darkMode': true}
final Map<String, double>? rates; // nullable, absent → null
}
- Only
Map<String, V>is supported (JSON has string-only keys); using any other key type raises a code-generation error. - Values are coerced using the same rules as their scalar counterparts.
toMap()serialises map values (handlesDateTime,Uri, nested models).- Error paths include the map entry key:
scores.alicefor a bad entry. - Runtime: new
rjCoerceMap/rjCoerceMapNullablehelpers. - Schema: new
RjMapSchemainrj_schema.dart.
Other changes #
- Generator now accepts a
FieldElement?parameter in_schemaExpressionso enum and map schemas can read field-level annotations. _jsonKeyOfin parser handles all five schema types cleanly.- Full test coverage for both features — runtime tests only, no
build_runnerrequired.
0.2.0 #
Phase 1 — Foundation fixes
-
Fix: type extraction no longer uses
toString()parsing.RjTypeSchemanow carries explicittypeName: StringandisNullable: boolfields set by the code generator at build time. The runtime reads these directly — no string parsing, no fragile generic reflection. -
Fix: required
Listfields now throw on missing key. Previously a missing list key silently returned[]regardless of nullability. Now only nullable lists (List<T>?) accept a missing key; required lists throwRjParseExceptionjust like any other required field. -
Fix: key-present vs key-absent distinction in the parser. The parser now uses
Map.containsKey()to distinguish a missing key from an explicitly null value, enabling correct error messages for both cases. -
Fix: removed Flutter dependency.
flutter_testandflutter_lintsremoved fromdev_dependencies. Replaced withpackage:test(already present) andpackage:lints.rj_safe_parseris a pure Dart package and never required Flutter. -
Fix:
analysis_options.yamlnow includespackage:lints/recommended.yamlinstead ofpackage:flutter_lints/flutter.yaml. -
Updated:
RjListSchemaandRjObjectSchemacarryisNullablefield. The runtime uses this to distinguish nullable-absent (return null/empty) from required-absent (throw). -
Updated: generator emits
isNullableandtypeNamein all schema literals. Regenerate.g.dartfiles withdart run build_runner build --delete-conflicting-outputsafter upgrading. -
Updated: test suite migrated from
flutter_testtopackage:test. All tests pass withdart test— no Flutter toolchain required.
0.1.0 #
- Initial release of
rj_safe_parser @RjSafeParsable()— single class-level annotation, zero per-field boilerplate- Smart type coercion for
String,int,double,bool,DateTime,Uri- Numeric strings (
'7') →int/double - Integer
1/0→bool - Unix timestamps (seconds and milliseconds) →
DateTime - ISO-8601 strings →
DateTime
- Numeric strings (
- Nullable fields (
T?) — absent key yieldsnull, no exception List<T>andList<NestedModel>— each element individually coerced- Nested
@RjSafeParsable()models — auto-detected, schema built inline - Strict mode (
strict: true) — rejects unknown keys withRjParseException - Lenient mode (default) — unknown keys produce warnings in
RjParseResult.warnings - Dot-path error reporting —
RjParseExceptionincludes full field path (e.g.address.zip) build_runnercode generator viaRjSafeParserGenerator- Comprehensive runtime tests — no
build_runnerrequired to run the test suite