0

I've got an XML configuration mapped to a JSON document which has an array of elements, but when there is only one element, the document looks like this:

{
    "name" : "test2"
    "products" : {
        "id" : "prod3"
        "value" : "prod_value3"
    }
}



{
    "name" : "test1"
    "products" : [
        {
            "id" : "prod1"
            "value" : "prod_value1"
        },
        {
            "id" : "prod2"
            "value" : "prod_value2"
        }
    ]
}

Instead of an array of elements, there is only one element "products"

The JSON is inserted into the MongoDB database and I'm trying to map the "products" as an ArrayList but in the first example, the array returns empty. My question is: Is there any way to automatically map this case with Java? Maybe a customMapper?

peter_f
  • 258
  • 1
  • 2
  • 11

1 Answers1

1

This case in Java is known as overloading methods. Object of some class and array are different types. You can't use one typecast to another etc., but you can use different type of parameter in the method accepting the value.

  • This sounds great! I didn't thought of that, but how can I edit the methods that the mongo mapper uses? I've already changed the regular setter and it doesn't work. – peter_f Nov 16 '15 at 16:50