Json.Net Combine 2 Properties on Serialization

Viewed 3043

I'm still fairly new to Json.Net and (de)serializing. I have an object which has a few fields

public class Person {
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
}

var p = new Person() {
    Id = 123,
    FirstName = "John",
    LastName = "Doe",
    Address1 = "456 Main St",
    Address2 = "Apt 2"
}

I need to serialize to the following json

{
    "id": 123
    , "fullName": "John Doe"
    , "street": "456 Main St Apt 2"
}

And deserialize that to

// Person.Id = 123
// Person.Address1 = 456 Main St Apt 2
// Person.Address2 = null

// we are ok not trying to split the 'street' into both 'Address1' and 'Address2'

I'm not sure the best method to do this. Do I need to use a Converter, ExtensionData, Constructor, or something else I haven't found because I don't know how to search for it ;)

Any help and examples would be great.

3 Answers
Related