Ainsworth
Ainsworth

Reputation: 79

Newtonsoft.Json.JsonConvert(object) returns null object

I have a 'complex' object that I want to serialize with JSon.Convert. As 'complex' objects go it is rather simple: Here are the objects:

The main object:

public class CustomerContactRequest
{
    private RequestHeaderArea header;
    private RequestPayloadArea payload;

    public CustomerContactRequest(string headerMessage, string npsGroup, string npsSection)
    {
        this.header = new RequestHeaderArea(headerMessage);
        this.payload = new RequestPayloadArea(npsGroup, npsSection);
    }
}

The 'header' Object:

public class RequestHeaderArea
{
    private string headerMessage;
    public string HeaderMessage { get { return headerMessage; } }
   
    public RequestHeaderArea(string headerMessage)
    {
        this.headerMessage = headerMessage;
    }
}

The Payload Area:

public class RequestPayloadArea
{
    private string npsGroup;
    private string npsSection;

    public string NPSGroup { get { return npsGroup; } }
    public string NPSSection { get { return npsSection; } }
    public RequestPayloadArea(string npsGroup, string npsSection)
    {
        this.npsGroup = npsGroup;
        this.npsSection = npsSection;
    }
}

And Finally, the main process:

static void Main(string[] args)
    {
        CustomerContactRequest ccRequest = new CustomerContactRequest(
            headerMessage: "test", 
            npsGroup: "1234567", 
            npsSection: "0000");
        retrieveContactInfo(ccRequest);
        
    }
    static void retrieveContactInfo(CustomerContactRequest ccRequest)
    {
        string jsonRequest = JsonConvert.SerializeObject(ccRequest);
        // code to call service
    }

jsonRequest returns {} even though ccRequest contains the expected values. What am I missing?

I am expecting something like this (sans formatting):

{
"headerArea": {
    "messageId": "test"
},
"payloadArea": {
    "group": {
        "Number": "1234567",
        "Suffix": "0000"
        }
    }
}

Implementing Chris's answer my classes now look like below (main program did not change except that I added Formatted.Indented to the SerializeObject call to make it pretty):

CustomerContactRequest:

public class CustomerContactRequest
{
    public  RequestHeaderArea headerArea;
    public  RequestPayloadArea payloadArea;

    public CustomerContactRequest(string headerMessage, string npsGroup, string npsSection)
    {
        this.headerArea = new RequestHeaderArea(headerMessage);
        this.payloadArea = new RequestPayloadArea(npsGroup, npsSection);
    }
   
}

RequestHeaderArea:

 public class RequestHeaderArea
{
    private string messageId;
    public string MessageId { get { return messageId; } }
   
    public RequestHeaderArea(string headerMessage)
    {
        this.messageId = headerMessage;
    }
   
}

RequestPayloadArea:

  public class RequestPayloadArea
{
    public Group group;

   
    public RequestPayloadArea(string npsGroup, string npsSection)
    {
        this.group = new Group(npsGroup, npsSection);
    }

    
}

And a new class: Group:

public class Group
{
    public string Number;
    public string Suffix;

    public Group(string npsGroup, string npsSection)
    {
        Number = npsGroup;
        Suffix = npsSection;
    }
}

Now my Json looks exactly as expected (see green text above)

Upvotes: 0

Views: 525

Answers (1)

Chris Hinton
Chris Hinton

Reputation: 866

SerializeObject ignores private members by default. You can either make them public, or by adding the SerializableAttribute to your CustomerContractRequest class.

Upvotes: 2

Related Questions