Skip to content

Commit 0343e14

Browse files
jderegclaude
andcommitted
Improve unsafe mode handling for nested JsonReader instances
- Unsafe mode is now enabled for all JsonReader instances when requested - Only root JsonReader manages the lifecycle (disables at end) - Fixed testEnumField to work with opt-in unsafe mode - Partially fixed testDuplicateRef (1 of 2 parameterized tests passing) This improves the situation where nested JsonReader instances created by factories also need unsafe mode enabled for deserializing package-private classes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c806bd9 commit 0343e14

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/main/java/com/cedarsoftware/io/JsonReader.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,13 @@ private boolean isBuiltInPrimitive(Object obj) {
691691
@SuppressWarnings("unchecked")
692692
protected <T> T resolveObjects(JsonObject rootObj, Type rootType) {
693693
// Enable unsafe mode for the entire deserialization if requested
694-
boolean wasUnsafeEnabled = false;
694+
// For root readers, we manage the lifecycle. For nested readers, we just ensure it's on.
695+
boolean shouldManageUnsafe = false;
696+
695697
if (readOptions.isUseUnsafe()) {
696698
ClassUtilities.setUseUnsafe(true);
697-
wasUnsafeEnabled = true;
699+
// Only the root JsonReader should manage (disable) unsafe mode at the end
700+
shouldManageUnsafe = isRoot;
698701
}
699702

700703
try {
@@ -721,7 +724,8 @@ protected <T> T resolveObjects(JsonObject rootObj, Type rootType) {
721724
throw new JsonIoException(getErrorMessage(e.getMessage()), e);
722725
} finally {
723726
// Restore unsafe mode to its original state
724-
if (wasUnsafeEnabled) {
727+
// Only disable if we were the ones who enabled it
728+
if (shouldManageUnsafe) {
725729
ClassUtilities.setUseUnsafe(false);
726730
}
727731

0 commit comments

Comments
 (0)