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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.