0

I am currently working on a solution that Deserializes the list of email from json array, Once I get the list of email objects, I am validating using another class then set into another list.

Below is the code for Deserialize:

static void Main(string[] args)
{
    string jsonTest = "{\"Emails\":[\"[email protected]\",\"[email protected]\"]}";

    Class1 contact = JsonConvert.DeserializeObject<Class1>(jsonTest);

    Console.WriteLine(contact.Emails[0]);
    Console.WriteLine(contact.Emails[1]);
}

And, below is the class definition for Class1 for deserialize:

public class Class1
{
    private readonly List<ValidateEmail> emailsobj;
    public List<string> Emails
    {
        get
        {
            return emailsobj.Select(o => o.EmailAdd).ToList();
        }
        set
        {
            foreach (string email in value)
            {
                emailsobj.Add(new ValidateEmail(email));
            }
        }
    }
}

And, below is the validate class:

public class ValidateEmail
{
    public string EmailAdd { get; set; }
    private bool Valid;

    public ValidateEmail(string value)
    {           
        try
        {
            MailAddress mail = new MailAddress(EmailAdd);
            EmailAdd = value;
            Valid = true;
        }
        catch (FormatException)
        {
            Valid = false;
        }
    }
}

Whenever I deserialize I am getting exception on line :

Console.WriteLine(contact.Emails[0]);

Newtonsoft.Json.JsonSerializationException: Error getting value from 'Emails' on 'JsonGenerator.Class1'. ---> System.ArgumentNullException: Value cannot be null. (Parameter 'source')

It looks like the List<Email> never set. Any help on this, it would be much appreciated.

2
  • Plz let know what error you are getting? This line will fail - JsonConvert.DeserializeObject<Class1>(jsonTest); because the emailsobj is null in class1 and what is MailAddress class? Commented Jun 17, 2020 at 18:34
  • Yes You are correct Mahesh. I have received the null reference error message. MailAddress is in-build class which is part of system.net.email. which is used to validate the email. I just changed to private List<ValidateEmail> emailsobj = new List<ValidateEmail>(). Still failed. Any idea? t would be nice, if you point out my mistake. The below error raised, Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error getting value from 'Emails' on 'JsonGenerator.Class1'. ---> System.ArgumentNullException: Value cannot be null. (Parameter 'source') Commented Jun 18, 2020 at 5:11

1 Answer 1

1

You have to set the below setting(Replace) if you are using Lists - ObjectCreationHandling.

Replace Always create new objects and enables the setter property for the Lists!

So after you initialize the new list in Class1 like below:

private readonly List<ValidateEmail> emailsobj = new List<ValidateEmail>();

Change the code to have setting:

 var settings = new JsonSerializerSettings
      {                
         ObjectCreationHandling = ObjectCreationHandling.Replace           
      };

 Class1 contact = JsonConvert.DeserializeObject<Class1>(jsonTest, settings);

See the detailed explanation here !

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

Comments

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.