private string getContactJSON(Operation operation)
    {
        string phonePref = Request["phonePref"] != null ? Request["phonePref"].ToString() : "0";
        string[] ptype = getInputType("phone[][type]");
        string[] pnumber = getInputValue("phone[][number]");
        string[] phone_list = new String[ptype.Length];
        if (ptype.Length != pnumber.Length)
        {
            throw new Exception("Number of fields does not match the number of values");
        }
        int phone_int_pref = Convert.ToInt32(phonePref);

        var phone = new List<ContactJSON.Phone>();
        for (int i = 0; i < ptype.Length; i++)
        {
            bool pref = i == phone_int_pref ? true : false;
            phone.Add(new ContactJSON.Phone() { type = ptype[i], number = pnumber[i], preferred = pref });
        }
        var phones = new ContactJSON.Phones();
        phones.phone = phone;

        var empty_phone = new List<ContactJSON.Phone>();
        empty_phone.Add(new ContactJSON.Phone() { type = string.Empty, number = string.Empty, preferred = true });
        var empty_phones = new ContactJSON.Phones();
        empty_phones.phone = empty_phone;

        string Pref = Request["imPref"] != null ? Request["imPref"].ToString() : "0";
        string[] im_type = getInputType("im[][type]");
        string[] im_im = getInputValue("im[][uri]");
        string[] im_list = new String[im_type.Length];
        if (im_type.Length != im_im.Length)
        {
            throw new Exception("Number of fields does not match the number of values");
        }
        int im_int_pref = Convert.ToInt32(Pref);
        var im = new List<ContactJSON.Im>();
        for (int i = 0; i < im_type.Length; i++)
        {
            bool pref = i == im_int_pref ? true : false;
            im.Add(new ContactJSON.Im() { type = im_type[i], imUri = im_im[i], preferred = pref });
        };
        var ims = new ContactJSON.Ims();
        ims.im = im;

        var empty_im = new List<ContactJSON.Im>();
        empty_im.Add(new ContactJSON.Im() { type = string.Empty, imUri = string.Empty, preferred = true });
        var empty_ims = new ContactJSON.Ims();
        empty_ims.im = empty_im;

        string emailPref = Request["emailPref"] != null ? Request["emailPref"].ToString() : "0";
        string[] email_type = getInputType("email[][type]");
        string[] email_email = getInputValue("email[][email_address]");
        string[] email_list = new String[email_type.Length];
        if (email_type.Length != email_email.Length)
        {
            throw new Exception("Number of fields does not match the number of values");
        }
        int email_int_pref = Convert.ToInt32(emailPref);
        var email = new List<ContactJSON.Email>();
        for (int i = 0; i < email_type.Length; i++)
        {
            bool pref = i == email_int_pref ? true : false;
            email.Add(new ContactJSON.Email() { type = email_type[i], emailAddress = email_email[i], preferred = pref });
        }
        var emails = new ContactJSON.Emails();
        emails.email = email;

        var empty_email = new List<ContactJSON.Email>();
        empty_email.Add(new ContactJSON.Email() { type = string.Empty, emailAddress = string.Empty, preferred = true });
        var empty_emails = new ContactJSON.Emails();
        empty_emails.email = email;

        string weburlPref = Request["weburlPref"] != null ? Request["weburlPref"].ToString() : "0";
        string[] weburl_type = getInputType("weburl[][type]");
        string[] weburl_weburl = getInputValue("weburl[][url]");
        string[] weburl_list = new String[weburl_type.Length];
        if (weburl_type.Length != weburl_weburl.Length)
        {
            throw new Exception("Number of fields does not match the number of values");
        }
        int weburl_int_pref = Convert.ToInt32(weburlPref);
        var weburl = new List<ContactJSON.WebUrl>();
        for (int i = 0; i < weburl_type.Length; i++)
        {
            bool pref = i == weburl_int_pref ? true : false;
            weburl.Add(new ContactJSON.WebUrl() { type = weburl_type[i], url = weburl_weburl[i], preferred = pref });
        }
        var weburls = new ContactJSON.Weburls();
        weburls.webUrl = weburl;

        var empty_weburl = new List<ContactJSON.WebUrl>();
        empty_weburl.Add(new ContactJSON.WebUrl() { type = string.Empty, url = string.Empty, preferred = true });
        var empty_weburls = new ContactJSON.Weburls();
        empty_weburls.webUrl = weburl;

        //address
        string addressPref = Request["addressPref"] != null ? Request["addressPref"].ToString() : "0";
        string[] atype = getInputType("address[][type]");
        string[] apoBox = getInputValue("address[][pobox]");
        string[] aaddressLine1 = getInputValue("address[][addressLine1]");
        string[] aaddressLine2 = getInputValue("address[][addressLine2]");
        string[] acity = getInputValue("address[][city]");
        string[] astate = getInputValue("address[][state]");
        string[] azip = getInputValue("address[][zip]");
        string[] acountry = getInputValue("address[][country]");
        int entryCount = atype.Length;
        string[] address_list = new String[entryCount];
        int address_int_pref = Convert.ToInt32(addressPref);
        var address = new List<ContactJSON.Address>();
        for (int i = 0; i < entryCount; i++)
        {
            bool pref = i == address_int_pref ? true : false;
            address.Add(new ContactJSON.Address()
            {
                type = atype[i],
                poBox = apoBox[i],
                preferred = pref,
                addressLine1 = aaddressLine1[i],
                addressLine2 = aaddressLine2[i],
                city = acity[i],
                state = astate[i],
                zipcode = azip[i],
                country = acountry[i]
            });
        }
        var addresses = new ContactJSON.Addresses();
        addresses.address = address;

        var empty_address = new List<ContactJSON.Address>();
        empty_address.Add(new ContactJSON.Address()
        {
            type = string.Empty,
            poBox = string.Empty,
            preferred = true,
            addressLine1 = string.Empty,
            addressLine2 = string.Empty,
            city = string.Empty,
            state = string.Empty,
            zipcode = string.Empty,
            country = string.Empty
        });
        var empty_addresses = new ContactJSON.Addresses();
        empty_addresses.address = address;

        //var contact = new List<ContactJSON.Contact>();
        var contact_obj = new ContactJSON.Contact();
        var photo = new ContactJSON.Photo();
        photo.encoding = "BASE64";

        var empty_photo = new ContactJSON.Photo();
        empty_photo.encoding = "BASE64";
        empty_photo.value = "";
        if (JsonConvert.SerializeObject(phones) != JsonConvert.SerializeObject(empty_phones))
        {
            contact_obj.phones = phones;
        }
        if (JsonConvert.SerializeObject(ims) != JsonConvert.SerializeObject(empty_ims))
        {
            contact_obj.ims = ims;
        }
        if (JsonConvert.SerializeObject(addresses) != JsonConvert.SerializeObject(empty_addresses))
        {
            contact_obj.addresses = addresses;
        }
        if (JsonConvert.SerializeObject(emails) != JsonConvert.SerializeObject(empty_emails))
        {
            contact_obj.emails = emails;
        }
        if (JsonConvert.SerializeObject(weburls) != JsonConvert.SerializeObject(empty_weburls))
        {
            contact_obj.weburls = weburls;
        }
        if (JsonConvert.SerializeObject(photo) != JsonConvert.SerializeObject(empty_photo))
        {
            contact_obj.photo = photo;
        }
        var contactRoot = new ContactJSON.RootObject();
        if (operation == Operation.CreateContactOperation)
        {
            string imgString = getImageto64base(attachPhoto.Value);
            photo.value = imgString;

            if (firstName.Text != "")
            {
                contact_obj.firstName = firstName.Text;
            }
            if (lastName.Text != "")
            {
                contact_obj.lastName = lastName.Text;
            }
            if (prefix.Text != "")
            {
                contact_obj.prefix = prefix.Text;
            }
            if (suffix.Text != "")
            {
                contact_obj.suffix = suffix.Text;
            }
            if (nickname.Text != "")
            {
                contact_obj.nickName = nickname.Text;
            }
            if (organization.Text != "")
            {
                contact_obj.organization = organization.Text;
            }
            if (jobTitle.Text != "")
            {
                contact_obj.jobTitle = jobTitle.Text;
            }
            if (anniversary.Text != "")
            {
                contact_obj.anniversary = anniversary.Text;
            }
            if (gender.Text != "")
            {
                contact_obj.gender = gender.Text;
            }
            if (hobby.Text != "")
            {
                contact_obj.hobby = hobby.Text;
            }
            if (assistant.Text != "")
            {
                contact_obj.assistant = assistant.Text;
            }
            contactRoot.contact = contact_obj;
        }
        else if (operation == Operation.UpdateContactOperation)
        {
            string imgString = getImageto64base(attachPhotoUpd.Value);
            photo.value = imgString;
            if (formattedNameUpd.Text != "")
            {
                contact_obj.formattedName = formattedNameUpd.Text;
            }
            if (firstNameUpd.Text != "")
            {
                contact_obj.firstName = firstNameUpd.Text;
            }
            if (lastNameUpd.Text != "")
            {
                contact_obj.lastName = lastNameUpd.Text;
            }
            if (prefixUpd.Text != "")
            {
                contact_obj.prefix = prefixUpd.Text;
            }
            if (suffixUpd.Text != "")
            {
                contact_obj.suffix = suffixUpd.Text;
            }
            if (nicknameUpd.Text != "")
            {
                contact_obj.nickName = nicknameUpd.Text;
            }
            if (organizationUpd.Text != "")
            {
                contact_obj.organization = organizationUpd.Text;
            }
            if (jobTitleUpd.Text != "")
            {
                contact_obj.jobTitle = jobTitleUpd.Text;
            }
            if (anniversaryUpd.Text != "")
            {
                contact_obj.anniversary = anniversaryUpd.Text;
            }
            if (genderUpd.Text != "")
            {
                contact_obj.gender = genderUpd.Text;
            }
            if (hobbyUpd.Text != "")
            {
                contact_obj.hobby = hobbyUpd.Text;
            }
            if (assistantUpd.Text != "")
            {
                contact_obj.assistant = assistantUpd.Text;
            }
            contactRoot.contact = contact_obj;
        }
        else if (operation == Operation.UpdateMyInfoOperation)
        {
            string imgString = getImageto64base(attachPhotoMyInf.Value);

            photo.value = imgString;

            if (firstNameMyInf.Text != "")
            {
                contact_obj.firstName = firstNameMyInf.Text;
            }
            if (lastNameMyInf.Text != "")
            {
                contact_obj.lastName = lastNameMyInf.Text;
            }
            if (prefixMyInf.Text != "")
            {
                contact_obj.prefix = prefixMyInf.Text;
            }
            if (suffixMyInf.Text != "")
            {
                contact_obj.suffix = suffixMyInf.Text;
            }
            if (nicknameMyInf.Text != "")
            {
                contact_obj.nickName = nicknameMyInf.Text;
            }
            if (organizationMyInf.Text != "")
            {
                contact_obj.organization = organizationMyInf.Text;
            }
            if (jobTitleMyInf.Text != "")
            {
                contact_obj.jobTitle = jobTitleMyInf.Text;
            }
            if (anniversaryMyInf.Text != "")
            {
                contact_obj.anniversary = anniversaryMyInf.Text;
            }
            if (genderMyInf.Text != "")
            {
                contact_obj.gender = genderMyInf.Text;
            }
            if (hobbyMyInf.Text != "")
            {
                contact_obj.hobby = hobbyMyInf.Text;
            }
            if (assistantMyInf.Text != "")
            {
                contact_obj.assistant = assistantMyInf.Text;
            }
            contactRoot.myInfo = contact_obj;
        }
        return JsonConvert.SerializeObject(contactRoot, Formatting.Indented,
                new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
    }
    private string getContactJSON(Operation operation)
    {
        string phonePref = Request["phonePref"] != null ? Request["phonePref"].ToString() : "0";

        string[] ptype      = getInputType("phone[][type]");
        string[] pnumber    = getInputValue("phone[][number]");
        string[] phone_list = new String[ptype.Length];
        if (ptype.Length != pnumber.Length)
        {
            throw new Exception("Number of fields does not match the number of values");
        }
        int phone_int_pref = Convert.ToInt32(phonePref);

        var phone = new List <ContactJSON.Phone>();

        for (int i = 0; i < ptype.Length; i++)
        {
            bool pref = i == phone_int_pref ? true : false;
            phone.Add(new ContactJSON.Phone()
            {
                type = ptype[i], number = pnumber[i], preferred = pref
            });
        }
        var phones = new ContactJSON.Phones();

        phones.phone = phone;

        var empty_phone = new List <ContactJSON.Phone>();

        empty_phone.Add(new ContactJSON.Phone()
        {
            type = string.Empty, number = string.Empty, preferred = true
        });
        var empty_phones = new ContactJSON.Phones();

        empty_phones.phone = empty_phone;

        string Pref = Request["imPref"] != null ? Request["imPref"].ToString() : "0";

        string[] im_type = getInputType("im[][type]");
        string[] im_im   = getInputValue("im[][uri]");
        string[] im_list = new String[im_type.Length];
        if (im_type.Length != im_im.Length)
        {
            throw new Exception("Number of fields does not match the number of values");
        }
        int im_int_pref = Convert.ToInt32(Pref);
        var im          = new List <ContactJSON.Im>();

        for (int i = 0; i < im_type.Length; i++)
        {
            bool pref = i == im_int_pref ? true : false;
            im.Add(new ContactJSON.Im()
            {
                type = im_type[i], imUri = im_im[i], preferred = pref
            });
        }
        ;
        var ims = new ContactJSON.Ims();

        ims.im = im;

        var empty_im = new List <ContactJSON.Im>();

        empty_im.Add(new ContactJSON.Im()
        {
            type = string.Empty, imUri = string.Empty, preferred = true
        });
        var empty_ims = new ContactJSON.Ims();

        empty_ims.im = empty_im;

        string emailPref = Request["emailPref"] != null ? Request["emailPref"].ToString() : "0";

        string[] email_type  = getInputType("email[][type]");
        string[] email_email = getInputValue("email[][email_address]");
        string[] email_list  = new String[email_type.Length];
        if (email_type.Length != email_email.Length)
        {
            throw new Exception("Number of fields does not match the number of values");
        }
        int email_int_pref = Convert.ToInt32(emailPref);
        var email          = new List <ContactJSON.Email>();

        for (int i = 0; i < email_type.Length; i++)
        {
            bool pref = i == email_int_pref ? true : false;
            email.Add(new ContactJSON.Email()
            {
                type = email_type[i], emailAddress = email_email[i], preferred = pref
            });
        }
        var emails = new ContactJSON.Emails();

        emails.email = email;

        var empty_email = new List <ContactJSON.Email>();

        empty_email.Add(new ContactJSON.Email()
        {
            type = string.Empty, emailAddress = string.Empty, preferred = true
        });
        var empty_emails = new ContactJSON.Emails();

        empty_emails.email = email;

        string weburlPref = Request["weburlPref"] != null ? Request["weburlPref"].ToString() : "0";

        string[] weburl_type   = getInputType("weburl[][type]");
        string[] weburl_weburl = getInputValue("weburl[][url]");
        string[] weburl_list   = new String[weburl_type.Length];
        if (weburl_type.Length != weburl_weburl.Length)
        {
            throw new Exception("Number of fields does not match the number of values");
        }
        int weburl_int_pref = Convert.ToInt32(weburlPref);
        var weburl          = new List <ContactJSON.WebUrl>();

        for (int i = 0; i < weburl_type.Length; i++)
        {
            bool pref = i == weburl_int_pref ? true : false;
            weburl.Add(new ContactJSON.WebUrl()
            {
                type = weburl_type[i], url = weburl_weburl[i], preferred = pref
            });
        }
        var weburls = new ContactJSON.Weburls();

        weburls.webUrl = weburl;

        var empty_weburl = new List <ContactJSON.WebUrl>();

        empty_weburl.Add(new ContactJSON.WebUrl()
        {
            type = string.Empty, url = string.Empty, preferred = true
        });
        var empty_weburls = new ContactJSON.Weburls();

        empty_weburls.webUrl = weburl;

        //address
        string addressPref = Request["addressPref"] != null ? Request["addressPref"].ToString() : "0";

        string[] atype         = getInputType("address[][type]");
        string[] apoBox        = getInputValue("address[][pobox]");
        string[] aaddressLine1 = getInputValue("address[][addressLine1]");
        string[] aaddressLine2 = getInputValue("address[][addressLine2]");
        string[] acity         = getInputValue("address[][city]");
        string[] astate        = getInputValue("address[][state]");
        string[] azip          = getInputValue("address[][zip]");
        string[] acountry      = getInputValue("address[][country]");
        int      entryCount    = atype.Length;

        string[] address_list     = new String[entryCount];
        int      address_int_pref = Convert.ToInt32(addressPref);
        var      address          = new List <ContactJSON.Address>();

        for (int i = 0; i < entryCount; i++)
        {
            bool pref = i == address_int_pref ? true : false;
            address.Add(new ContactJSON.Address()
            {
                type         = atype[i],
                poBox        = apoBox[i],
                preferred    = pref,
                addressLine1 = aaddressLine1[i],
                addressLine2 = aaddressLine2[i],
                city         = acity[i],
                state        = astate[i],
                zipcode      = azip[i],
                country      = acountry[i]
            });
        }
        var addresses = new ContactJSON.Addresses();

        addresses.address = address;

        var empty_address = new List <ContactJSON.Address>();

        empty_address.Add(new ContactJSON.Address()
        {
            type         = string.Empty,
            poBox        = string.Empty,
            preferred    = true,
            addressLine1 = string.Empty,
            addressLine2 = string.Empty,
            city         = string.Empty,
            state        = string.Empty,
            zipcode      = string.Empty,
            country      = string.Empty
        });
        var empty_addresses = new ContactJSON.Addresses();

        empty_addresses.address = address;

        //var contact = new List<ContactJSON.Contact>();
        var contact_obj = new ContactJSON.Contact();
        var photo       = new ContactJSON.Photo();

        photo.encoding = "BASE64";

        var empty_photo = new ContactJSON.Photo();

        empty_photo.encoding = "BASE64";
        empty_photo.value    = "";
        if (JsonConvert.SerializeObject(phones) != JsonConvert.SerializeObject(empty_phones))
        {
            contact_obj.phones = phones;
        }
        if (JsonConvert.SerializeObject(ims) != JsonConvert.SerializeObject(empty_ims))
        {
            contact_obj.ims = ims;
        }
        if (JsonConvert.SerializeObject(addresses) != JsonConvert.SerializeObject(empty_addresses))
        {
            contact_obj.addresses = addresses;
        }
        if (JsonConvert.SerializeObject(emails) != JsonConvert.SerializeObject(empty_emails))
        {
            contact_obj.emails = emails;
        }
        if (JsonConvert.SerializeObject(weburls) != JsonConvert.SerializeObject(empty_weburls))
        {
            contact_obj.weburls = weburls;
        }
        if (JsonConvert.SerializeObject(photo) != JsonConvert.SerializeObject(empty_photo))
        {
            contact_obj.photo = photo;
        }
        var contactRoot = new ContactJSON.RootObject();

        if (operation == Operation.CreateContactOperation)
        {
            string imgString = getImageto64base(attachPhoto.Value);
            photo.value = imgString;

            if (firstName.Text != "")
            {
                contact_obj.firstName = firstName.Text;
            }
            if (lastName.Text != "")
            {
                contact_obj.lastName = lastName.Text;
            }
            if (prefix.Text != "")
            {
                contact_obj.prefix = prefix.Text;
            }
            if (suffix.Text != "")
            {
                contact_obj.suffix = suffix.Text;
            }
            if (nickname.Text != "")
            {
                contact_obj.nickName = nickname.Text;
            }
            if (organization.Text != "")
            {
                contact_obj.organization = organization.Text;
            }
            if (jobTitle.Text != "")
            {
                contact_obj.jobTitle = jobTitle.Text;
            }
            if (anniversary.Text != "")
            {
                contact_obj.anniversary = anniversary.Text;
            }
            if (gender.Text != "")
            {
                contact_obj.gender = gender.Text;
            }
            if (hobby.Text != "")
            {
                contact_obj.hobby = hobby.Text;
            }
            if (assistant.Text != "")
            {
                contact_obj.assistant = assistant.Text;
            }
            contactRoot.contact = contact_obj;
        }
        else if (operation == Operation.UpdateContactOperation)
        {
            string imgString = getImageto64base(attachPhotoUpd.Value);
            photo.value = imgString;
            if (formattedNameUpd.Text != "")
            {
                contact_obj.formattedName = formattedNameUpd.Text;
            }
            if (firstNameUpd.Text != "")
            {
                contact_obj.firstName = firstNameUpd.Text;
            }
            if (lastNameUpd.Text != "")
            {
                contact_obj.lastName = lastNameUpd.Text;
            }
            if (prefixUpd.Text != "")
            {
                contact_obj.prefix = prefixUpd.Text;
            }
            if (suffixUpd.Text != "")
            {
                contact_obj.suffix = suffixUpd.Text;
            }
            if (nicknameUpd.Text != "")
            {
                contact_obj.nickName = nicknameUpd.Text;
            }
            if (organizationUpd.Text != "")
            {
                contact_obj.organization = organizationUpd.Text;
            }
            if (jobTitleUpd.Text != "")
            {
                contact_obj.jobTitle = jobTitleUpd.Text;
            }
            if (anniversaryUpd.Text != "")
            {
                contact_obj.anniversary = anniversaryUpd.Text;
            }
            if (genderUpd.Text != "")
            {
                contact_obj.gender = genderUpd.Text;
            }
            if (hobbyUpd.Text != "")
            {
                contact_obj.hobby = hobbyUpd.Text;
            }
            if (assistantUpd.Text != "")
            {
                contact_obj.assistant = assistantUpd.Text;
            }
            contactRoot.contact = contact_obj;
        }
        else if (operation == Operation.UpdateMyInfoOperation)
        {
            string imgString = getImageto64base(attachPhotoMyInf.Value);

            photo.value = imgString;

            if (firstNameMyInf.Text != "")
            {
                contact_obj.firstName = firstNameMyInf.Text;
            }
            if (lastNameMyInf.Text != "")
            {
                contact_obj.lastName = lastNameMyInf.Text;
            }
            if (prefixMyInf.Text != "")
            {
                contact_obj.prefix = prefixMyInf.Text;
            }
            if (suffixMyInf.Text != "")
            {
                contact_obj.suffix = suffixMyInf.Text;
            }
            if (nicknameMyInf.Text != "")
            {
                contact_obj.nickName = nicknameMyInf.Text;
            }
            if (organizationMyInf.Text != "")
            {
                contact_obj.organization = organizationMyInf.Text;
            }
            if (jobTitleMyInf.Text != "")
            {
                contact_obj.jobTitle = jobTitleMyInf.Text;
            }
            if (anniversaryMyInf.Text != "")
            {
                contact_obj.anniversary = anniversaryMyInf.Text;
            }
            if (genderMyInf.Text != "")
            {
                contact_obj.gender = genderMyInf.Text;
            }
            if (hobbyMyInf.Text != "")
            {
                contact_obj.hobby = hobbyMyInf.Text;
            }
            if (assistantMyInf.Text != "")
            {
                contact_obj.assistant = assistantMyInf.Text;
            }
            contactRoot.myInfo = contact_obj;
        }
        return(JsonConvert.SerializeObject(contactRoot, Formatting.Indented,
                                           new JsonSerializerSettings {
            NullValueHandling = NullValueHandling.Ignore
        }));
    }