javajsonserializationmorphia

Morphia : Accepting single value as array, for a field in morphia entity


Lets say I have class

@Entity
class MyCollectionEntity {

    @Property("sample_field")
    private List<String> sampleField;
}

Here morphia is able to save the json documents where the sampleField key has value like

{
 "sample_field":["field1", "field2"]
}

But I need this entity to map the values which are not in list and just a string too like

{
"sample_field": "field1"
}

in the same sampleField class as List. How can I achieve this when the key may take value as an array of objects as well as sometimes just the object ? This will be similar to the feature provided in Jackson DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY I am using org.mongodb.morphia v1.3.2


Solution

  • Morphia does not support that kind of serialization. 1.3.2 is super old so i'm trying to remember the details. I think it can read such values (but that's not a promise) but will write out a List as an array in the documents.