Matthias Beaupère
Matthias Beaupère

Reputation: 1847

cannot serialize map containg orientdb record id

Just migrated orientdb from v2.1.19 to v3.0.2 and there is an issue with the code I was using.

Basically doing this now throws a cast exception :

Map<String, String> myMap = vertex.getProperty("myMap");
String test = new ObjectMapper().writeValueAsString(myMap);

Where

Full error label : com.fasterxml.jackson.databind.JsonMappingException: com.orientechnologies.orient.core.id.ORecordId cannot be cast to java.lang.String

Why is it that ORecordId can not be cast to String anymore ?

Only solution I see : detect that there is a ORecordId in the map and transform it manually into a String. This doesn't seem like a fit to me.

Upvotes: 1

Views: 170

Answers (1)

Matthias Beaup&#232;re
Matthias Beaup&#232;re

Reputation: 1847

The solution I used (I'm not really fond of it)

I created a method that handle the retrieved property as Map<String, Object>

public static Map<String, String> convertToStringMap(Map<String, Object> map) {
    if (map == null)
        return null;
    return map.entrySet().stream().collect(Collectors.toMap(
            entry -> entry.getKey(), 
            entry -> entry.getValue().toString()));
}

Then I can retrieve my embedded map as a Map<String, String> this way

Map<String, String> myMap = convertToStringMap(vertex.getProperty("myMap"))

// Do something with
// (both throw an exception if no passed through convertToStringMap)
String test = new ObjectMapper().writeValueAsString(myMap); // serialize
System.out.println(my.get("anEntryKey")); // print

Upvotes: 1

Related Questions