简体   繁体   中英

Call the constructor when Deserializing json to object

I have the LoginModels :

public class LoginModels
{
    public LoginModels(string userEmail, string userPassword)
    {
        email = userEmail;
        password = userPassword;
        errorMessage = GetLoginError();
    }

    public string email;
    public string password;
    public string errorMessage;

    public string GetLoginError()
    {
        if (string.IsNullOrEmpty(email)) return "email is empty";
        else return "good";
    }
}

I sent a json to a function of a controller..

In the controller, I wrote:

LoginModels user = JsonConvert.DeserializeObject<LoginModels>(userDetails);
string relevantEmail = user.email;

BUT the constructor of LoginModels gets email and password as null.

That's why errorMessage is email is empty .

But relevantEmail is the email that came from the ajax (and it's ok).

I realy don't know why the constructor doesn't get the parameters that were send by the ajax call.

Any help appreciated!

Serialization/deserialization can call only default constructor - imagine you'll have multiple constructors with various parameters - how can the framework guess which one to call/which parameters ? Additionally the serializable fields should be properties. So your object should look like:

public class LoginModels
{
    private string _errorMessage;

    // default ctor for serialization
    public LoginModels() 
    {
    }

    public LoginModels(string userEmail, string userPassword)
    {
        email = userEmail;
        password = userPassword;
    }

    public string email { get; set; }
    public string password { get; set; }
    public string errorMessage 
    { 
        get 
        { 
            if (string.IsNullOrEmpty(_errorMessage))
            {
                _errorMessage = GetLoginError();
            }
            return _errorMessage;
        } 
        set { _errorMessage = value; }
    }

    public string GetLoginError()
    {
        if (string.IsNullOrEmpty(email))
        {
            return "email is empty";
        }
        // also no need for "else" here
        return "good";
    }
}

Use the JsonConstructor attribute so that your JsonConvert knows which constructor to use:

using using Newtonsoft.Json;;    

public class LoginModels
{
  [JsonConstructor]
  public LoginModels(string userEmail, string userPassword)
  {
      email = userEmail;
      password = userPassword;
      errorMessage = GetLoginError();
  }

  public string email;
  public string password;
  public string errorMessage;

  public string GetLoginError()
  {
      if (string.IsNullOrEmpty(email)) return "email is empty";
      else return "good";
  }
}

Here is the source: https://www.newtonsoft.com/json/help/html/JsonConstructorAttribute.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM