WebTech
WebTech

Reputation: 9

JSON.NET Array Serialization in C#.NET

I am trying to create a class for serializing and deserializing arrays. The class I have created, appears to be working for deserializing, but when I try to serialize the array, I am having issues. I am a fairly new C# developer and I am sure I have left an important piece out of my code, I just am not sure what.

Below is a copy of the class I created:

namespace PinnacleCartFormAPI
{
    class clsGetCustomersResponse
    {
        public Customer Customer = new Customer();//{ get; set; }
    }

    public class Customer
    {
        public Int32 UserId;
        public string UserName;
        public CustBilling Billing = new CustBilling();
        public AddressBook[] AddressBook;// AddressBook = new AddressBook();
    }

    public class CustBilling
    {
        public string FullName, FirstName, LastName, Email, Company, Phone;
        public CustAddress Address = new CustAddress();
    }

    public class CustAddress
    {
        public string Name, Street1, Street2, City, State, Zip, Country;
    }

    public class AddressBook
    {
        public string Name, Street1, Street2, City, State, Zip, Country;
    }
}

As you can see, the AddressBook class needs to be an Array. I believe my issue is related to the fact that I am not initiating the AddressBook class properly as an array.

Below, is a copy of the calling code that adds values to the different elements of the class:

clsGetCustomersResponse GetCustomersResp = new clsGetCustomersResponse();
GetCustomersResp.Customer.UserId = 123456;
GetCustomersResp.Customer.UserName = "Username";
GetCustomersResp.Customer.Billing.FullName = "Full Name";
GetCustomersResp.Customer.Billing.FirstName = "First Name";
GetCustomersResp.Customer.Billing.LastName = "Last Name";    
GetCustomersResp.Customer.Billing.Email = "[email protected]";
GetCustomersResp.Customer.Billing.Phone = "7778889999";
GetCustomersResp.Customer.Billing.Address.Name = "Address Name";
GetCustomersResp.Customer.Billing.Address.Street1 = "Address Street 1";
GetCustomersResp.Customer.Billing.Address.Street2 = "";
GetCustomersResp.Customer.Billing.Address.City = "Address City";
GetCustomersResp.Customer.Billing.Address.State = "Address State";
GetCustomersResp.Customer.Billing.Address.Zip = "Address Zip";
GetCustomersResp.Customer.Billing.Address.Country = "Address Country";

GetCustomersResp.Customer.AddressBook[0].Name = "Address Name";

GetCustomersResp.Customer.AddressBook[0].Street1 = "Address Street 1";
GetCustomersResp.Customer.AddressBook[0].Street2 = "";
GetCustomersResp.Customer.AddressBook[0].City = "Address City";
GetCustomersResp.Customer.AddressBook[0].State = "Address State";
GetCustomersResp.Customer.AddressBook[0].Zip = "Address Zip";
GetCustomersResp.Customer.AddressBook[0].Country = "Address Country";

As soon as I hit the bolded lines, I receive the following error:

"Object reference not set to an instance of an object"

Again, I believe this is a result of me not properly initializing the AddressBook portion of the code. However, I am not certain how to do that with the array.

Can you please provide me with some direction on this?

Thanks,

Zach

Upvotes: 0

Views: 917

Answers (3)

Steve
Steve

Reputation: 31652

Agreed with Jon's answer - I didn't spend a ton of time on it, but maybe you'll find this refactored set of classes useful:

namespace Pinnacle.Cart.Customers
{
    public class RetrieveResponse
    {
        public RetrieveResponse() { }

        public RetrieveResponse(Customer customer) { 
            Customer = customer;
        }

        public Customer Customer { get; set; }
    }

    public class Customer
    {
        public Customer() { 
            Billing = new BillingInfo(); 
            AddressBook = new List<AddressBookEntry>(); 
        }

        public Int UserId  { get; set; }
        public String UserName  { get; set; }
        public BillingInfo Billing  { get; set; }
        public List<AddressBookEntry> AddressBook { get; set; }

        public class BillingInfo
        {
            public BillingInfo() { Address = new Address(); }

            public String FullName { get; set; }
            public String FirstName { get; set; }
            public String LastName { get; set; }
            public String Email { get; set; }
            public String Company { get; set; }
            public String Phone { get; set; }
            public Address Address { get; set; }
        }
    }

    public class Address
    {
        public String Street1 { get; set; }
        public String Street2 { get; set; }
        public String City { get; set; }
        public String State { get; set; }
        public String Zip { get; set; }
        public String Country { get; set; }
    }

    public class AddressBookEntry : Address
    {
        public string Name { get; set; }
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503130

(Just to be clear, this doesn't really have anything to do with JSON.)

Yes, you have to initialize an array before you start putting values in it. Unfortunately arrays are of a fixed size - you can't change the size (e.g. by adding an element) after they've been created. I would suggest using a List<AddressBook> instead of an array. Then you can use:

// This initialization could be in the type itself
GetCustomersResp.Customer.AddressBook = new List<AddressBook>();

AddressBook address = new AddressBook();
address.Name = "Address Name";
address.Street1 = "Address Street 1";
// etc

GetCustomersResp.Customer.AddressBook.Add(address);

I'd also be tempted to rename the AddressBook type - it's just a single address, not a whole address book. The property within Customer could still be called AddressBook, as an address book is a collection of addresses.

Upvotes: 2

flipchart
flipchart

Reputation: 6578

Try this (for 10 address books):

GetCustomersResp.Customer.AddressBook = new AddressBook[10];

You need to instantiate your array before you try to assign elements in it

Upvotes: 1

Related Questions