Skip to content

JsonNode.deepCopy() ClassCastException`s #1829

@D3v01dZA

Description

@D3v01dZA

The current signature of JsonNode#deepCopy looks like this:

public abstract <T extends JsonNode> T deepCopy();

I wondered about the decision to do this as you can quite easily create a ClassCastException using this as follows:

TextNode text = TextNode.valueOf("SomeText");
ObjectNode object = text.deepCopy();

This code is obviously incorrect just by looking at it however this will always compile and throw an exception at runtime. I know this is considered an API change but a signature that prevents this might be:

JsonNode.java
public abstract JsonNode deepCopy();

And then be overridden on each subclass with the correct type

ValueNode.java
public abstract ValueNode deepCopy();

After looking at this again, the signatures of ObjectNode and ArrayNode are correct but you can still trick it by upcasting to JsonNode and then using deepCopy()

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.0Issue planned for initial 3.0 release

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions