C# deserializar Enum that does not exist

Asked

Viewed 44 times

-1

I have a class that has as attribute an Enum, for example the class below:

public enum Model { Volvo = 1, Ferrari = 2, Gurgel = 3 }

public class Car
{
    public Model model { get; private set; }

    public Car(Model model)
    {
        if (Enum.IsDefined(typeof(Model), model))
            throw new Exception("Quero que arrebente esta exception");

        this.model = model;
    }

}

If I pass the right Json to a controller, an instance of the CAR class will be mounted normally, but if I pass the wrong Json, the CAR class object will be null and won’t even enter the constructor. It would not be right to enter the constructor regardless of whether the value is right or wrong?

Json certain

{
  "model" : "Volvo"
}

Wrong json

{
  "model" : "Arno"
}

Controller method

    public async Task<IActionResult> AddCar([FromBody]Car car)
    {
        if (car == null) //CAI AQUI QUANDO O VALOR DO ENUM NÃO EXISTE.    
    }

1 answer

1

You can read What good is a builder?. It shows that the constructor serves to create the object in a valid state. If there is no data to be used then the object cannot be created. If you were to create an object anyway then you wouldn’t need a builder. And entered the constructor, only you made an exception and it was not built (you can have debug to see step by step).

But even in this hypothesis, what do you expect to do with an invalid object? Just cause trouble. So the best that can happen is the object get null and you only use it after testing that it is ok, which seems to be what it did.

Of course you might want to create the object anyway, but then in its constructor you have to establish the proper criterion to create and achieve a state that is sensitive to the case, it can even leave in an invalid state, it’s wrong, but it’s technically possible, If you make it clear you want this, take it. In case I don’t even see how to make it invalid. If you see one Arno What value should the object have then? Some value has to have if you don’t put it will take 0. What will you do with a model 0?

Anyway, was thrown an exception (wrong way, but it was), in some must have captured and accepted the object anyway, this is wrong, if had an exception can not continue using the object. If you want to use it anyway the first thing you have to do is take the exception (I’m not saying it’s right to do it, but if you want the object in invalid state would be a little more correct).

  • You’re being very critical of who you’re trying to help you with. A from the comment seems that the question is unclear then. If you’ve done something wrong, do you want the person to say it’s right? Then you don’t want help. It is not possible to do the right thing with a wrong problem statement, you can’t say you want to add two things and talk to not consider that there is a sum. I answered over what you wrote, if you wanted to write something else you should have written that other thing.

  • Boy...you’re being very critical of someone who didn’t understand the question. 1. All that’s up there are examples, that is, don’t take into account whether I’m lifting an Exception or not in a constructor. (I know very well what the constructor is for, so your recommendation is bad) 2. The question of the Enum VALUE being wrong in JSON is precisely the x of the question, since the invalid value does not let me even get to the car EXAMPLE constructor, probably this is at the time of the json deserialization in the controller’s Middle.

  • the CAR class object will be null and will not enter the constructor. Wouldn’t it be right to enter the constructor regardless of whether the value is right or wrong? This question is very objective and your answer has nothing to do with this question...!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.