This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Let's see how we can configure the JSON serializer in order to resolve the issue that we encountered when serializing our data to JSON.
Follow Along
To follow along committing your changes to this course, you'll need to fork the aspnet-fitness-frog-spa repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd aspnet-fitness-frog-spa
git checkout tags/v3.3 -b configuring-the-json-serializer
Configuring Json.NET to Ignore Model Properties
Fortunately, the Json.NET framework provides a convenient way to tell the serializer to ignore a property. To get started with this change, we need to add the Json.NET framework—using the NuGet package manager—to the shared class library.
Go ahead and stop the app if it's running. Then use the NuGet package manager to add the Json.NET framework (v6.0.4) to the shared class library. We're installing an older version of the Json.NET framework, because that's the version that was installed when we installed the Web API framework.
After the package has finished installing, open the Activity.cs file located in the "Models" folder.
And add the JsonIgnore attribute to the Entries
property.
[JsonIgnore]
public IList<Entry> Entries { get; set; }
Now, let's retest by pressing F5 to start debugging. Using Postman again, make a GET request against the "/api/entries" endpoint. In Visual Studio, let's remove any breakpoints and press F5 to continue execution.
And back in Postman, we can see a much slimmed down version of our JSON entries data!
Reducing Response Data Payloads Using DTOs
Our current response data payload contains more data than it needs to. As we saw above, we can decorate the Activity Entries
property with a JsonIgnore
attribute in order to instruct the JSON serializer to not serialize that property.
We could also leverage data transfer objects—or DTOs—to refine or shape the response data. If we had an EntryListDto class that looked like this:
public class EntryListDto
{
public int Id { get; set; }
public DateTime Date { get; set; }
public string ActivityName { get; set; }
public decimal Duration { get; set; }
public string IntensityName { get; set; }
}
We could use like this in our EntriesController's Get
method:
public IHttpActionResult Get()
{
var entries = _entriesRepository.GetList();
var entryDtos = entries.Select(e => new EntryListDto()
{
Id = e.Id,
Date = e.Date,
ActivityName = e.Activity.Name,
Duration = e.Duration,
IntensityName = e.Intensity.ToString()
}).ToList();
return Ok(entryDtos);
}
We'll also see an example later in this section of using DTOs to prevent users from under-posting data.
Additional Learning
- Json.NET ContractResolver
- Entity Framework Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up