Skip to content

[Bug] [COMMON] JSONUtils.toMap method cannot cast json to the provided type #16430

@genuinner

Description

@genuinner

Search before asking

  • I had searched in the issues and found no similar issues.

What happened

When i try to parse json to complex class(like BaseHeartBeat.class), there will be a ClassCastException.
That is because the JSONUtils.toMap(String json, Class classK, Class classV) uses TypeReference to parse json which cannot deal generic type correctly, we can use MapType instead.

public void toGenericMap() {
        String json = "{\"beatHeart\":{\"processId\":18,\"host\":\"192.168.1.1\"}}";

        Map<String, BaseHeartBeat> models = JSONUtils.toMap(json, String.class, BaseHeartBeat.class);
        Assertions.assertEquals(18, models.get("beatHeart").getProcessId());
}

What you expected to happen

no exceptin should be throw

How to reproduce

just parse the json to any custom class

Anything else

No response

Version

dev

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions