Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting class cast exception for Jackson library #65

Open
vaithu opened this issue Apr 5, 2021 · 1 comment
Open

Getting class cast exception for Jackson library #65

vaithu opened this issue Apr 5, 2021 · 1 comment

Comments

@vaithu
Copy link

vaithu commented Apr 5, 2021

I'm trying to deserialize a json string which contains an Array.

 ListPathProperty<PropertyBox> DASAENTRIES = ListPathProperty.propertyBox("dasaEntries",DasaEntries.PROPERTIES);
    PropertySet<?> PROPERTIES = PropertySet.builderOf(DASAENTRIES)
            .build();
    PathProperty<PropertyBox> VIMSHOTTARIDASA = PathProperty.create("vimshottariDasa", PropertyBox.class)
            .withConfiguration(PropertySet.PROPERTY_CONFIGURATION_ATTRIBUTE,PROPERTIES)

            ;

When trying to get this value from JSON string, I'm getting this exception

Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.holonplatform.core.property.PropertyBox (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.holonplatform.core.property.PropertyBox is in unnamed module of loader 'app')
	at java.base/java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:999)
	at com.example.application.data.test.TestJson.lambda$getDasaEntries$2(TestJson.java:234)
	at java.base/java.util.Optional.ifPresent(Optional.java:176)
	at com.example.application.data.test.TestJson.lambda$getDasaEntries$3(TestJson.java:232)
	at java.base/java.util.Optional.ifPresent(Optional.java:176)
	at com.example.application.data.test.TestJson.getDasaEntries(TestJson.java:230)
	at com.example.application.data.test.TestJson.test(TestJson.java:152)
	at com.example.application.data.test.TestJson.main(TestJson.java:63)

This is where I'm getting the error

responseBox.getValueIfPresent(VimshottariDasa.VIMSHOTTARIDASA)
                .ifPresent(propertyBox -> {
                    propertyBox.getValueIfPresent(VimshottariDasa.DASAENTRIES)
                            .ifPresent(propertyBoxes -> {

                                for (PropertyBox box : propertyBoxes) { // error line
                                    
                                }

Looks there is some problem in casting and the below proves the same.

if (propertyBoxes.get(0) instanceof LinkedHashMap) {
                                    LinkedHashMap<?,?> map = (LinkedHashMap<?, ?>) propertyBoxes.get(0);
                                    map.entrySet()
                                            .stream()
                                            .forEach(entry -> {
                                                write(entry.getKey() +"\t" + entry.getValue());
                                            });
                                }else {
                                    write("not sure ");
                                }

Could you please check this ?

@vaithu
Copy link
Author

vaithu commented Apr 5, 2021

I'm using Jackson library and getting similar error with gson too. Logged an issue for that too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant