Last active
June 17, 2018 09:52
-
-
Save Kaidanov/f9ad0d79238494432f32b8407942c606 to your computer and use it in GitHub Desktop.
Removing circular entity rendering, handling Camel convension, No need for additional attributes on properties, Serializer per entity type , Including children of children in response, Removing backslash from JSON by returning Content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Contact | |
{ | |
public int Id { get; set; } | |
public string UserName { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public string Email { get; set; } | |
... | |
//[JsonIgnore] | |
//[JsonProperty("notificationContact")] | |
public List<NotificationContact> NotificationContact{get; set;} | |
} | |
public class NotificationContact | |
{ | |
public int Id { get; set; } | |
public int ContactId { get; set; } | |
//[JsonIgnore] | |
public Contact Contact { get; set; } | |
public int NotificationId { get; set; } | |
//[JsonProperty("notification")] | |
public Notification Notification {get;set;} | |
} | |
public static class ContactSerialize | |
{ | |
public static string ToJson(this Contact self) => JsonConvert.SerializeObject(self,Converter.Settings); | |
} | |
public class Converter | |
{ | |
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
{ | |
MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
DateParseHandling = DateParseHandling.None, | |
Formatting = Formatting.Indented, | |
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | |
ContractResolver = new CamelCasePropertyNamesContractResolver() | |
}; | |
} | |
[HttpGet("{id}")] | |
public async Task<IActionResult> GetContact([FromRoute] int id) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var contact = await _context.Contact | |
.Include("NotificationContact.Notification") | |
.Include("UserContactTypeContact.ContactType") | |
.Include("Address") | |
.SingleOrDefaultAsync(m => m.Id == id); | |
if (contact == null) | |
{ | |
return NotFound(); | |
} | |
return Content(contact.ToJson()); | |
//return contact; | |
} | |
///Output | |
{ | |
"id": 3031, | |
"userName": null, | |
"firstName": "..", | |
"lastName": "..", | |
"email": "..", | |
"irs": "..", | |
"portCode": null, | |
"selfFilerCode": "..", | |
"achCode": "..", | |
"picPath": null, | |
"address": { | |
"id": 4028, | |
"street": "77 Turner St Middlesex County", | |
"addressPart1": null, | |
"addressPart2": null, | |
"city": "Woodbridge Township", | |
"state": "New Jersey", | |
"zip": "07064", | |
"country": "United States" | |
}, | |
"notificationContact": [ | |
{ | |
"id": .., | |
"contactId": .., | |
"notificationId": 3, | |
"notification": { | |
"id": 3, | |
"description": "SMS" | |
} | |
} | |
], | |
"userContactTypeContact": [ | |
{ | |
"id": .., | |
"userId": 4, | |
"user": null, | |
"contactId": 3031, | |
"contactTypeId": 8, | |
"contactType": { | |
"id": 8, | |
"description": "Chat" | |
} | |
}, | |
{ | |
"id": 4105, | |
"userId": 4, | |
"user": null, | |
"contactId": 3031, | |
"contactTypeId": 7, | |
"contactType": { | |
"id": 7, | |
"description": "Stuffer" | |
} | |
} | |
], | |
"userId": 4, | |
"user": null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment