I want to allow a user to determine some optional fields for the response. The example here is simple strings, but in a live example, this could be used to show/not show hundreds or even thousands of lines of complex properties.
Say that I have a class
public class MyClass
{
public string Foo { get; set; }
public string Bar { get; set; }
public string Baz { get; set; }
}
//By default, I only want to return Foo
//Ex. 1
// GET /myclass
//Returns
{
Foo = "Hello Foo"
}
However, I want to allow the user to decide whether to include Bar and Baz.
The user could do this
//Ex. 2
// GET /myclass?includeFoo=true&includeBar=true
//Returns
{
Foo = "Hello Foo",
Bar = "Hello Bar",
Baz = "Hello Baz"
}
or the user could just include one of bar or baz
//Ex 3
// GET /myclass?includeBar=true
//Returns
{
Foo = "Hello Foo",
Bar = "Hello Bar"
}
I know that I could use a dynamic object type to accomplish this goal, but is there a way to do that without using dynamic objects?
3
Answers
You could do a null trick and ignore the data as @Serge pointed out, you can also use the (System.Text.Json) JsonExtensionDataAttribute or (Newtonsoft) JsonExtensionDataAttribute
Assuming that you’ve already handled the processing of the incoming query string to determine the set of included properties, you can construct your response as normal and then do some final processing on it to remove fields from the final response the user hasn’t asked for.
Using
JSON.NET
, you can serialise this "fat" response object into JSON and load it into aJObject
. From there, you can use the Remove method to strip out properties from the response you don’t want to send back.Something like this:
It’s not a giant leap from there to extract this into a helper method that accepts a type, an instance of that type, a set of properties to include and returns back JSON with only the requested properties:
You can do this by configuring the serializer. If you are using System.Text.Json then see here for details:
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-ignore-properties?pivots=dotnet-6-0#ignore-all-null-value-properties
If you are using Newtonsoft.Json then the configuration should be similar but it is called NullValueHandling, see here for details:
https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm