The fromJson(Class) Javadoc claimed that only classes "explicitly supported
and registered within the JSONObject context" can be deserialized, and
declared an IllegalArgumentException for classes outside that set. No such
registry exists: fromJson instantiates any class with a no-arg constructor and
writes every non-static declared field whose name matches a JSON key, widening
access with setAccessible(true). Private and transient fields are populated,
and JSONPropertyIgnore does not apply because it only suppresses the bean to
JSONObject direction.
The binding rules are already released, so changing them here would break
existing users. Instead document what actually happens, point out that binding
is driven by whatever keys the JSON contains, and replace the non-existent
IllegalArgumentException with the JSONException the method really throws.
Adds JSONObjectTest#jsonObjectParseFromJson_10 to pin the current binding
behavior so the documentation cannot drift from the implementation again. The
test bean is named FromJsonKeyBindingExample and its fields spell out how they
are declared (privateString, privateBoolean, transientString,
ignoredGetterBoolean), so the test reads as a binding example rather than as a
scenario.
Fixes#1076
XML.mustEscape() is shared by XML.escape() (serialization) and
XMLTokener.unescapeEntity() (parsing), but the two callers need opposite
answers for #x9/#xA/#xD. #1070 added those codepoints to the valid-range
clause to fix unescape(" "), which also stopped XML.toString() and
JSONML.toString() from escaping literal TAB, LF and CR. Literal
occurrences of these characters are subject to whitespace normalization
by conforming XML parsers, so a value like "Lettuce\nTomato" loses its
line breaks when the generated XML is read back by any standard parser.
This regressed #1092.
Split the two concerns: mustEscape() returns true for every ISO control
character again (serialization), and the new isValidXmlCharacter() keeps
the previous numeric-reference validation for the parsing path, so 	,
and are still accepted (#1059/#1070 behaviour unchanged).
isValidXmlCharacter() stays as strict as before for the C1 control
characters (#x7F-#x9F): they fall inside [#x20-#xD7FF] and are legal XML
1.0 characters, but this library has always rejected references to them,
and widening the range here would silently change what XML.toJSONObject
accepts. Only the serialization of TAB/LF/CR changes.
Fixes#1092
Replace the four repeated "childNodes" literals with a private
static final constant.
Address SonarQube rule java:S1192 without changing parsing behavior.
JSONML object-form parsing uses "childNodes" to store child elements,
but the reserved-attribute check incorrectly used "childNode".
This rejected ordinary "childNode" attributes while accepting
"childNodes" attributes, whose values could then be overwritten
by the element's children.
Correct the reserved-attribute check to use "childNodes" and add
JUnit 4 regression tests covering:
- Rejection of reserved "childNodes" attributes in object-form parsing
- Preservation of ordinary "childNode" attributes and child content
- Continued rejection of reserved "tagName" attributes
- Preservation of both attribute names in array-form parsing
Verify each scenario with keepStrings enabled and disabled.
Validation:
- All 40 JSONML tests pass
- Full test suite: 807 passed, 6 skipped on Java 11 with Gradle 8.13
- Both bug regression tests fail against the original implementation
Fixes#1080
toURIFragment() URL-encoded each reference token but skipped the ~0/~1
escaping from RFC 6901. Tokens are stored as literal key names, so a key
containing / or ~ produced a fragment that parsed back to a different
pointer. Apply escape() before encoding, as toString() already does.
Fixes#1082
Per review on #1072: reject only < > & " ' / in element names to
close the CWE-91 injection vector, and drop the full XML 1.0 Name
validation to preserve backwards compatibility for callers that
emit non-well-formed but non-injecting tag names.
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01MTGnYg5v1QxaqqfDKHKTVr
isXmlNameStart's alternating &&/|| chain scored cognitive complexity 28.
Extracting inRange(cp, lo, hi) collapses it to a flat || sequence and
keeps the range list 1:1 with the XML 1.0 NameStartChar production.
isXmlNameChar updated the same way. No behaviour change.
Co-Authored-By: Claude <[email protected]>
XML.toString emitted JSONObject keys verbatim as tag names, so a key
containing '<', '>' or '/' broke out of its element and injected
arbitrary sibling structure into the output. Per #294/#123 the agreed
approach is to throw on invalid input rather than mangle it.
- Add mustBeXmlName / isXmlNameStart / isXmlNameChar implementing the
XML 1.0 (5th ed.) Name production, code-point aware.
- Validate tagName at method entry and each key at the top of the key
loop (skipping the cDataTagName sentinel).
- Rewrite XMLTest.shouldHandleIllegalJSONNodeNames and
XMLConfigurationTest.shouldHandleIllegalJSONNodeNames (previously
documenting the pass-through behaviour) to assert the throw.
- Add XMLTest.toStringRejectsElementInjectionInKey covering the #1071
payload and an invalid caller-supplied tagName.
- Add XMLTest.toStringAcceptsValidXmlNames covering hyphen/dot/
underscore/colon, Latin-1 letters, and the cDataTagName sentinel.
Fixes#1071. Also resolves the long-standing well-formedness question
in #166 / #294 / #308.
Co-Authored-By: Claude <[email protected]>
XML.mustEscape() is shared by both XML.escape() (serialization) and
XMLTokener.unescapeEntity() (deserialization). While the method's Javadoc
and comment quote the W3C XML 1.0 valid-character range
(#x9 | #xA | #xD | [#x20-#xD7FF] | ...), the implementation only checked
[#x20-#xD7FF] in its range clause, omitting #x9/#xA/#xD.
Although the ISO-control clause excluded those three codepoints, the
negated range clause still marked them as 'must escape', so unescapeEntity()
threw JSONException for the XML-allowed control characters TAB, LF and CR.
This broke XML.unescape(" ") and XML.toJSONObject("<a> </a>"),
both of which worked in v20251224 and regressed after #1045 (v20260522).
Align the range clause with the W3C spec by explicitly allowing #x9, #xA
and #xD, matching the comment that was already documented.
Fixes#1059
Per review on #1067:
- objectToBigInteger(val, dflt, JSONParserConfiguration) uses
cfg.getMaxNumberLength() for the digit-count guard; -1 disables it.
Existing 2-arg form delegates with a default config.
- New public overloads on JSONObject and JSONArray:
getBigInteger(key, cfg) / optBigInteger(key, dflt, cfg).
Existing methods delegate with a default config.
- objectToBigDecimal left unchanged (no expansion path; agreed on PR).
- Tests cover default (1000), raised (2000), lowered (5), disabled (-1),
null config, and JSONArray overloads.
Co-Authored-By: Claude <[email protected]>
Completes the CVE-2026-59171 fix started in ab92bb9 / #1065. The
1000-char length guard in stringToValue admits short exponent-notation
literals (e.g. 1e100000000, 11 chars) which are stored compactly as
BigDecimal and only expand when getBigInteger/optBigInteger calls
BigDecimal.toBigInteger(), materialising ~10^8 digits and stalling the
thread or throwing OOM.
Guard both toBigInteger() sites in objectToBigInteger by rejecting any
BigDecimal whose integer part would exceed
ParserConfiguration.DEFAULT_MAX_NUMBER_LENGTH decimal digits
(precision() - scale(), both O(1) reads). Returns defaultValue on
overflow, matching the method's existing behaviour for non-finite and
unparseable values.
Covers JSONObject.getBigInteger/optBigInteger and
JSONArray.getBigInteger/optBigInteger (all delegate to this helper).
Adds JSONObjectTest.getBigIntegerHugeExponentReturnsDefault with a 5s
timeout so a regression fails fast rather than hanging CI.
Co-Authored-By: Claude <[email protected]>