5

Everyone got this question a lot, and I tried almost everything but none of it works for me.

So I am developing in Xamarin.Forms and is about to send my data to the server. I have this class:

public class Customer
{

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string BirthDate { get; set; }
    public string Password { get; set; }
    public string EmailAddress { get; set; }
    public string ContactNumber { get; set; }

}

Then I used Newtonsoft's SerializeObject method:

Customer customer = new Customer
        {
            FirstName = FirstName.Text,
            LastName = LastName.Text,
            BirthDate = BirthDate.Date.ToString(),
            EmailAddress = Email.Text,
            Password = Password.Text,
            ContactNumber = Mobile.Text
        };


        var item = JsonConvert.SerializeObject(customer);

But variable item results in a string containing an empty JSON object {}. Is something wrong with my implementation?

EDIT: Also, I noticed that although my Customer class and its members are public, the debugger still counts them as "non-public" members. Please see "screenshot of debugger":

Screenshot of debugger

Lala
  • 378
  • 3
  • 15
  • 1
    get rid of the [Serializable] attribute – Jason Feb 21 '18 at 20:10
  • 1
    I doubt SerializeObject returns null if passing in "an object". It should still return "some JSON" (or *a string*), even if it doesn't serialize as expected. – user2864740 Feb 21 '18 at 20:10
  • 1
    *Also, I noticed that although my Customer class is public, its members are non-public* -- if that is true, then they will not be serialized. See [Why are some members missing when trying to print an object by serializing to JSON?](https://stackoverflow.com/q/48156976/3744182) for an explanation and options to fix. However, in your question, the properties *are* public. So, which is it? – dbc Feb 21 '18 at 20:15
  • @dbc Please see the "screenshot of debugger" although i've set every member to public, the debugger counts them as non-public. – Lala Feb 21 '18 at 20:18
  • @Lala - well that's a bit mysterious. What if you try some of the workarounds from [here](https://stackoverflow.com/q/48156976/3744182) such as marking your public properties with `[JsonProperty]`? – dbc Feb 21 '18 at 20:20
  • `FirstName.Text`, `LastName.Text`, etc. are not null? – Bruno Peres Feb 21 '18 at 20:29
  • @BrunoPeres they are not. – Lala Feb 21 '18 at 20:31
  • @dbc did that, to no avail – Lala Feb 21 '18 at 20:31
  • @Jason got rid off, still no luck – Lala Feb 21 '18 at 20:33
  • 2
    Then maybe related or duplicate: [JsonConvert.SerializeObject always return {} in XamarinForms](https://stackoverflow.com/q/48041823/3744182). – dbc Feb 21 '18 at 20:35
  • 1
    @dbc that's it!! I plugged my device via USB (instead of Xamarin Live Player) and now it works! – Lala Feb 21 '18 at 21:16

2 Answers2

3

Your question is a little misleading since you show your class properties as public.

Also, I noticed that although my Customer class is public, its members are non-public:

WIth that being said these are your options:

  1. If your properties can be public, make them public
  2. If the properties need to remain private, add the [JsonProperty] attribute to them
ertdiddy
  • 407
  • 3
  • 11
  • Apologies for that, what I meant was - I've set them all to public. But the debugger still counts them as "non-public". Please see "screenshot of debugger" – Lala Feb 21 '18 at 20:26
  • 1
    @Lala your backing fields are non publics, but your properties are public. – Bruno Peres Feb 21 '18 at 20:28
3

It appears that Xamarin Live Player has some issues with the serializer. I tried plugging my phone via USB and it works!

Lala
  • 378
  • 3
  • 15