apache / dolphinscheduler

Apache DolphinScheduler is the modern data orchestration platform. Agile to create high performance workflow with low-code
https://dolphinscheduler.apache.org/
Apache License 2.0
12.88k stars 4.63k forks source link

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

Closed genuinner closed 3 months ago

genuinner commented 3 months ago

Search before asking

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?

Code of Conduct

SbloodyS commented 3 months ago

Please ask meaningful questions.