Fixed malformed-number parsing (#31) and added java.time mapper support (#23)#32
Open
Kitty-Hivens wants to merge 4 commits into
Open
Fixed malformed-number parsing (#31) and added java.time mapper support (#23)#32Kitty-Hivens wants to merge 4 commits into
Kitty-Hivens wants to merge 4 commits into
Conversation
… parser (JavaWebStack#31) parseNumber now validates the candidate token without consuming it and returns null on failure, so the existing error path reports a proper ParseException at the offending position instead of leaking a NumberFormatException. Also fixed exponent-only numbers (e.g. 1e3) being routed to Long.parseLong and failing.
New JavaTimeMapper handles LocalDate, LocalDateTime, LocalTime, Instant, OffsetDateTime, ZonedDateTime, OffsetTime, Year, YearMonth and MonthDay. Values serialize to their canonical ISO-8601 form by default; @DateFormat still applies -- a custom pattern (DateTimeFormatter syntax) or epoch mode (Instant only, otherwise a MapperException). Registered as a sibling of DateMapper, no public API or Java language level change.
There was a problem hiding this comment.
Pull request overview
This PR bundles (1) a fix to JSON number parsing so malformed numeric tokens surface as ParseException rather than leaking NumberFormatException, and (2) new java.time type mapping support integrated into the default mapper set.
Changes:
- Updated
JsonParser.parseNumber()to validate candidate numeric tokens and avoid leakingNumberFormatException, including support for scientific notation. - Added a
JavaTimeMapperregistered inDefaultMappersto map corejava.timetypes with ISO-8601 defaults plus@DateFormatpattern/epoch options (epoch restricted toInstant). - Added JUnit tests for malformed/scientific-number parsing and
java.timemapper round-trips / error cases.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/main/java/org/javawebstack/abstractdata/json/JsonParser.java | Adjusts number token handling to prevent NumberFormatException leaks and support exponent notation. |
| src/main/java/org/javawebstack/abstractdata/mapper/DefaultMappers.java | Registers and implements JavaTimeMapper to support mapping of key java.time types. |
| src/test/java/org/javawebstack/abstractdata/json/JsonParserTest.java | Adds tests for malformed numbers and scientific notation parsing. |
| src/test/java/org/javawebstack/abstractdata/mapper/JavaTimeMapperTest.java | Adds coverage for ISO round-trips, epoch Instant, custom patterns, and malformed-input failures. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…input fromAbstract rebuilt its failure message with element.string(), which itself throws AbstractCoercingException for object/array input and escaped the intended MapperException; a safe representation is now captured before the try and reused in the catch. A custom @DateFormat pattern on an Instant was written through a UTC-defaulted formatter but read back through a zone-less one, so deserializing the value it had just serialized threw; the read side now applies the same UTC default. Add round-trip coverage for Instant custom patterns and for non-primitive input.
parseNumber rejected an invalid token without consuming anything, so parse() flagged the error at the token start ('1' for "1.2.3") rather than at the character that actually broke parsing.
It now consumes the longest valid numeric prefix before returning null, leaving the offending character as the stack head for the position report. The token is still rejected as a whole; the shared parse logic moved into toNumber().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR bundles two independent changes.
JSON parser: malformed numbers (#31)
parseNumbernow validates the candidate token without consuming it and returnsnullon failure, so the existing error path reports a properParseExceptionat the offending position instead of leaking aNumberFormatException. Exponent-only numbers (e.g.1e3), previously routed toLong.parseLongand failing, now parse as doubles.Mapper: java.time support (#23)
A new
JavaTimeMapper(sibling ofDateMapper, registered inDefaultMappers) maps the corejava.timetypes:LocalDate,LocalDateTime,LocalTime,Instant,OffsetDateTime,ZonedDateTime,OffsetTime,Year,YearMonth,MonthDay. Values serialize to their canonical ISO-8601 form by default. The existing@DateFormatannotation still applies: a custom pattern usesDateTimeFormattersyntax, and epoch mode is supported forInstantonly (any other type throws aMapperExceptionrather than silently dropping the zone/offset). No public API or Java language level change (target stays 8).Out of scope, as separate follow-ups:
Duration/Period/ZoneId/ZoneOffset, andjava.timesupport in the BSON converter.Test plan
mvn testgreen.JsonParserTest: added malformed-number cases (--,-,1.2.3,12e,.) assertingParseException, plus scientific notation (1e3,2.5E-2).JavaTimeMapperTest: ISO round-trip for all 10 types, epochInstant(seconds/millis), custom pattern, epoch rejection on a non-Instanttype, and malformed input ->MapperException.