I have such endpoint in my controller :
public async Task<<IEnumerable<ItemsList>>> GetItems()
{
List<ItemsList>> items = await _itemManager.GetItemsAsync();
return items;
}
And when I get result from this endpoint :
{
"Type": "SomeType",
"Items":[{"Id":1,"ItemType":"SomeType"}]
}
but I want to be Camel Case, such as :
{
"type": "SomeType",
"items":[{"id":1,"itemType":"SomeType"}]
}
Here is of
public class ItemsList
{
public ItemType Type { get; set; }
public List<Items> Items { get; set; }
}
public class Item
{
public int ItemId { get; set; }
public ItemType ItemType { get; set; }
}
I found solution like :
public async Task<<IEnumerable<ItemsList>>> GetItems()
{
List<ItemsList>> items = await _itemManager.GetItemsAsync();
var serializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
return Json(items),serializerSettings);
}
So, what I want to do it's to create Attribute, which can be applied to specific endpoint and make result from the endpoint to be camelCase.
Applying Json attributes to Dto's or formatting the whole controller isn't my case.
I like to use the DataMemberAttribute from System.Runtime.Serialization
Example:
[Serializable]
[DataContract]
public class SomeDto
{
[DataMember(Name = "unitCount")]
public int UnitCount { get; set; }
[DataMember(Name = "packagingType")]
public PackagingType PackagingTypeIdEtc { get; set; }
// ...
The output will be camelCase and you'll also be able to control things like emitDefaultValue and so on.
{
unitCount: 3,
packagingType: "box",
/* ... */
}
On the entity, you can use:
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
On a property, you can use:
[JsonProperty(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
Also on a property, you can manually specify the property name if you wish:
[JsonProperty(PropertyName = "myPropertyName")]
Try putting below in Register function under WebApiConfig
public static void Register(HttpConfiguration config)
{
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractresolvder();
}
Related
I have two 'data' classes:
public class BaseData
{
public string BaseDataStuff { get; set; }
}
public class ChildData : BaseData
{
public string ChildDataStuff { get; set; }
}
and a 'container' class:
public class Container
{
public BaseData Data { get; set; }
}
Then I have the following controller:
public class Controller : ControllerBase
{
private readonly ChildData Data;
private readonly Container Container;
public Controller()
{
Data = new ChildData()
{
BaseDataStuff = "base stuff",
ChildDataStuff = "child stuff"
};
Container = new Container()
{
Data = Data
};
}
[HttpGet("data")]
public ActionResult<BaseData> GetData() => Ok(Container.Data);
[HttpGet("container")]
public ActionResult<Container> GetContainer() => Ok(Container);
}
The first method just returns the ChildData instance. When I run it in swagger, I get the JSON I expect:
{
"childDataStuff": "child stuff",
"baseDataStuff": "base stuff"
}
When I run the second method through swagger, it looks like it casts the ChildData instance to BaseData. I get the following JSON:
{
"data": {
"baseDataStuff": "base stuff"
}
}
Can someone explain what is happening here please?
This question was interesting. See How to serialize properties of derived classes with System.Text.Json
There it explains that System.Text.Json does not handle serializing polymorphic objects the way you want prior to .NET 7. There are some work arounds but those allow you to serialize when the root object is polymorpic (Container in you case) but not members of the root (BaseData and ChildData).
In .NET 7 you can do this
using System.Text.Json.Serialization;
[JsonDerivedType(typeof(ChildData))]
public class BaseData
{
public string BaseDataStuff { get; set; } = string. Empty;
}
and
public class ChildData : BaseData
{
public string ChildDataStuff { get; set; } = string. Empty;
}
and
public class Container
{
public BaseData Data { get; set; } = new BaseData();
}
Then this
var data = new ChildData()
{
BaseDataStuff = "base stuff",
ChildDataStuff = "child stuff"
};
var container = new Container
{
Data = data
};
jsonString = JsonSerializer.Serialize(container);
Console.WriteLine(jsonString);
gives you
{"Data":{"ChildDataStuff":"child stuff","BaseDataStuff":"base stuff"}}
Which I think is what you want. Of course, going to .NET 7 may not be convenient.
You could try serializing yourself like this
options = new JsonSerializerOptions
{
WriteIndented = true
};
jsonString = JsonSerializer.Serialize<object>(Container, options);
or another option, is to explicitly mark them as serializable for the serializer:
[DataContract]
public class BaseData
{
[DataMember]
public string BaseDataStuff { get; set; }
}
[DataContract]
public class ChildData : BaseData
{
[DataMember]
public string ChildDataStuff { get; set; }
}
I am practicing with web api. My goal is to create a Get endpoint, which receive data from an external api, then return a different result. external api link: https://www.themealdb.com/api/json/v1/1/search.php?f=a, The external api data looks like:
{
"meals": [
{
"idMeal": "52768",
"strMeal": "Apple Frangipan Tart",
"strDrinkAlternate": null,
"strCategory": "Dessert",
.....
},
{
"idMeal": "52893",
"strMeal": "Apple & Blackberry Crumble",
....
}
]
}
I want my endpoint provide a different result like the following:
[
{
"idMeal": "52768",
"strMeal": "Apple Frangipan Tart",
"ingredients": ["Apple", "sugar"...]
},
{
"idMeal": "52893",
"strMeal": "Apple & Blackberry Crumble",
"ingredients": ["Apple", "sugar"...]
}
]
The following code is what I attempted so far, It's working, but the moment I changed property ingredient1 from public to private, that ingredient in list will become null, also, there are so many ingredients, some of them are null by default, I don't want to add them if they are null, how can I fix these two issues? Thanks a lot
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using RestSharp;
namespace testAPI.Controllers;
public class Content
{
[JsonPropertyName("meals")]
public List<Meal> Meals { get; set; }
}
public class Meal
{
[JsonPropertyName("idMeal")]
public string MealId { get; set; }
[JsonPropertyName("strMeal")]
public string Name { get; set; }
[JsonPropertyName("strIngredient1")]
public string Ingredient1 { get; set; }
[JsonPropertyName("strIngredient2")]
public string Ingredient2 { get; set; }
[JsonPropertyName("strIngredient20")]
public string Ingredient20 { get; set; }
public List<string> Ingredients
{
get { return new List<string>(){Ingredient1, Ingredient2, Ingredient20};}
}
}
[ApiController]
[Route("api/[controller]")]
public class DishesController : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetAllRecipes()
{
var client = new RestClient($"https://www.themealdb.com/api/json/v1/1/search.php?s=");
var request = new RestRequest();
var response = await client.ExecuteAsync(request);
var mealList = JsonSerializer.Deserialize<Content>(response.Content);
return Ok(mealList.Meals);
}
}
To address the problems one at a time...
the moment I changed property ingredient1 from public to private, that ingredient in list will become null
Changing the access modifier affects both deserialization and serialization, so this cannot be used to only stop it from serializing the property. You should split the data models up into what you want to receive and what you want to expose/return.
there are so many ingredients, some of them are null by default, I don't want to add them if they are null
Addition to splitting up the data models you can handle this when mapping from one model to the other.
The following code should fix both issues:
namespace TheMealDb.Models
{
// These are the models you receive from TheMealDb
// JSON converted to classes with https://json2csharp.com/
public class Root
{
public List<Meal> meals { get; set; }
}
public class Meal
{
public string idMeal { get; set; }
public string strMeal { get; set; }
public string strIngredient1 { get; set; }
public string strIngredient2 { get; set; }
public string strIngredient3 { get; set; }
// Other properties removed for brevity...
}
}
namespace Internal.Models
{
// This is the model you want to return from your controller action
public class Meal
{
[JsonPropertyName("id")] // No need to use the same name as from themealdb
public string Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("ingredients")]
public List<string> Ingredients { get; set; }
}
}
Now, to fetch, map and return the data in your controller action:
[HttpGet]
public async Task<IActionResult> GetAllRecipes()
{
var client = new RestClient($"https://www.themealdb.com/api/json/v1/1/search.php?s=");
var request = new RestRequest();
var response = await client.ExecuteAsync(request);
// Deserialize to the "TheMealDb" models
var mealList = JsonSerializer.Deserialize<TheMealDb.Models.Root>(response.Content);
// Map to your own models
var myMealList = mealDbList.meals?.Select(MapToInternal);
return Ok(myMealList);
}
// Map "TheMealDb" model to your own model
private Internal.Models.Meal MapToInternal(TheMealDb.Models.Meal externalMeal)
{
return new Internal.Models.Meal
{
Id = externalMeal.idMeal,
Name = externalMeal.strMeal,
Ingredients = new []
{
externalMeal.strIngredient1,
externalMeal.strIngredient2,
externalMeal.strIngredient3,
// ...
}
// Remove empty/null ingredients
.Where(ingr => !string.IsNullOrEmpty(ingr))
.ToList()
};
}
See the code in action.
I have following Json-based configuration file:
{
"PostProcessing": {
"ValidationHandlerConfiguration": {
"MinimumTrustLevel": 80,
"MinimumMatchingTrustLevel": 75
},
"MatchingCharacterRemovals": [
"-",
"''",
":"
]
},
"Processing": {
"OrderSelection": {
"SelectionDaysInterval": 30,
"SelectionDaysMaximum": 365
}
}
}
As serialization framework I use Newtonsoft. To serialize this config into objects I have implemented following classes:
[JsonObject(MemberSerialization.OptIn)]
public class RecognitionConfiguration {
[JsonProperty(PropertyName = "PostProcessing", Required = Required.Always)]
public PostRecognitionConfiguration PostRecognitionConfiguration { get; set; }
[JsonProperty(PropertyName = "Processing", Required = Required.Always)]
public ProcessRecognitionConfiguration ProcessRecognitionConfiguration { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class PostRecognitionConfiguration {
[JsonProperty(Required = Required.Always)]
public ValidationHandlerConfiguration ValidationHandlerConfiguration { get; set; }
[JsonProperty] public List<string> MatchingCharacterRemovals { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class ProcessRecognitionConfiguration {
[JsonProperty(PropertyName = "OrderSelection", Required = Required.Always)]
public OrderSelectionConfiguration OrderSelectionConfiguration { get; set; }
}
In a class I try to serialize a specific configuration section into these class structures using IConfigurationSection.Get().
var serializedConfiguration = this.ConfigurationSection.Get<RecognitionConfiguration>();
But when I debug the code, I always get an "empty" variable serializedConfiguration which is not null, but all properties are null.
If I use
this.ConfigurationSection.GetSection("Processing").Get<ProcessRecognitionConfiguration>()
or change the naming of the properties in the json file to exactly match the property names in the classes like this:
{
"ProcessRecognitionConfiguration": {
"OrderSelectionConfiguration": {
"SelectionDaysInterval": 30,
"SelectionDaysMaximum": 365
}
}
}
it it works fine. Do you have any idea, why setting PropertyName on JsonProperty does not seem to have any effect?
That is by design. Binding to POCO via configuration is done by convention. Not like Model Binding to Controller Action parameters.
It matches property names on the POCO to keys in the provided JSON.
Reference Configuration in ASP.NET Core
So either you change the settings to match the class like you showed in the original question, or change the class to match the settings keys in the Json-based configuration file.
[JsonObject(MemberSerialization.OptIn)]
public class RecognitionConfiguration {
[JsonProperty(PropertyName = "PostProcessing", Required = Required.Always)]
public PostRecognitionConfiguration PostProcessing{ get; set; }
[JsonProperty(PropertyName = "Processing", Required = Required.Always)]
public ProcessRecognitionConfiguration Processing{ get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class PostRecognitionConfiguration {
[JsonProperty(Required = Required.Always)]
public ValidationHandlerConfiguration ValidationHandlerConfiguration { get; set; }
[JsonProperty]
public List<string> MatchingCharacterRemovals { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class ProcessRecognitionConfiguration {
[JsonProperty(PropertyName = "OrderSelection", Required = Required.Always)]
public OrderSelectionConfiguration OrderSelection { get; set; }
}
public partial class ValidationHandlerConfiguration {
[JsonProperty("MinimumTrustLevel")]
public long MinimumTrustLevel { get; set; }
[JsonProperty("MinimumMatchingTrustLevel")]
public long MinimumMatchingTrustLevel { get; set; }
}
public partial class OrderSelectionConfiguration {
[JsonProperty("SelectionDaysInterval")]
public long SelectionDaysInterval { get; set; }
[JsonProperty("SelectionDaysMaximum")]
public long SelectionDaysMaximum { get; set; }
}
EDIT: I found this one is much more pleasant than my previous solutions: Bind everything in an ExpandoObject, write them to JSON and use JSON.NET to bind them back. Using the code of this article:
namespace Microsoft.Extensions.Configuration
{
public static class ConfigurationBinder
{
public static void BindJsonNet(this IConfiguration config, object instance)
{
var obj = BindToExpandoObject(config);
var jsonText = JsonConvert.SerializeObject(obj);
JsonConvert.PopulateObject(jsonText, instance);
}
private static ExpandoObject BindToExpandoObject(IConfiguration config)
{
var result = new ExpandoObject();
// retrieve all keys from your settings
var configs = config.AsEnumerable();
foreach (var kvp in configs)
{
var parent = result as IDictionary<string, object>;
var path = kvp.Key.Split(':');
// create or retrieve the hierarchy (keep last path item for later)
var i = 0;
for (i = 0; i < path.Length - 1; i++)
{
if (!parent.ContainsKey(path[i]))
{
parent.Add(path[i], new ExpandoObject());
}
parent = parent[path[i]] as IDictionary<string, object>;
}
if (kvp.Value == null)
continue;
// add the value to the parent
// note: in case of an array, key will be an integer and will be dealt with later
var key = path[i];
parent.Add(key, kvp.Value);
}
// at this stage, all arrays are seen as dictionaries with integer keys
ReplaceWithArray(null, null, result);
return result;
}
private static void ReplaceWithArray(ExpandoObject parent, string key, ExpandoObject input)
{
if (input == null)
return;
var dict = input as IDictionary<string, object>;
var keys = dict.Keys.ToArray();
// it's an array if all keys are integers
if (keys.All(k => int.TryParse(k, out var dummy)))
{
var array = new object[keys.Length];
foreach (var kvp in dict)
{
array[int.Parse(kvp.Key)] = kvp.Value;
}
var parentDict = parent as IDictionary<string, object>;
parentDict.Remove(key);
parentDict.Add(key, array);
}
else
{
foreach (var childKey in dict.Keys.ToList())
{
ReplaceWithArray(input, childKey, dict[childKey] as ExpandoObject);
}
}
}
}
}
Usage:
var settings = new MySettings();
this.Configuration.BindJsonNet(settings);
Here is my testing MySettings class:
public class MySettings
{
[JsonProperty("PostProcessing")]
public SomeNameElseSettings SomenameElse { get; set; }
public class SomeNameElseSettings
{
[JsonProperty("ValidationHandlerConfiguration")]
public ValidationHandlerConfigurationSettings WhateverNameYouWant { get; set; }
public class ValidationHandlerConfigurationSettings
{
[JsonProperty("MinimumTrustLevel")]
public int MinimumTrustLevelFoo { get; set; }
[JsonProperty("MinimumMatchingTrustLevel")]
public int MinimumMatchingTrustLevelBar { get; set; }
}
}
}
After the calling, I get everything as you desired:
Old Answer:
According to the source code here, it is simply (near) impossible to do what you are requiring. I have tried both JsonProperty and DataContract, none of which are honored by the Binder, simply because the source code itself simply use the property name.
If you still insist, there are 2 possibilities, however I do not recommend any as changing properties' names are much simpler:
Fork your source code there, or simply copy that file (in my attempt to trace the code, I rename all methods to something like Bind2, BindInstance2 etc), and rewrite the code accordingly.
This one is very specific to current implementation, so it's not future-proof: the current code is calling config.GetSection(property.Name), so you can write your own IConfiguration and provide your own name for GetSection method and tap it into the bootstrap process instead of using the default one.
Changing PropertyName on JsonProperty does have effect. Here is the same I tried and it did worked for me:
my JSON data:
{"name": "John","age": 30,"cars": [ "Ford", "BMW", "Fiat" ]}
and the Model:
public class RootObject
{
[JsonProperty(PropertyName ="name")]
public string Apple { get; set; }
public int age { get; set; }
public List<string> cars { get; set; }
}
and here is the code:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
and this is the output i get
You need to set the PropertyName in JsonProperty same as json file property name but your C# model property can be what you wanted, just that they need to be decorated with [JsonProperty(PropertyName ="jsonPropertyName")] Hope this helps you solve your issue.
Happy coding...
TLDR; Can not parse querystring data to object model using Json.net deserializer.
Long version:
I'm using Newtonsoft.Json's DefaultContractResolver for Json serializer in dotnet core 2.0;
var mvc = services.AddMvc();
mvc.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
});
In web api controller, i'm using a model;
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet]
public Core.Data.Models.Request.DataSourceRequestModel Get(Core.Data.Models.Request.DataSourceRequestModel req)
{
return req;
}
}
And DataSourceRequestModel is;
public sealed class DataSourceRequestModel
{
[JsonProperty(PropertyName = "start")]
public int Start { get; set; }
[JsonProperty(PropertyName = "limit")]
public int Limit { get; set; }
[JsonProperty(PropertyName = "filter")]
public ICollection<DataSourceRequestFilterModel> Filters { get; set; }
[JsonProperty(PropertyName = "sort")]
public ICollection<DataSourceRequestSortModel> Sorts { get; set; }
public DataSourceRequestModel()
{
Limit = 25;
}
}
public class DataSourceRequestFilterModel
{
[JsonProperty(PropertyName = "property")]
public string Property { get; set; }
[JsonProperty(PropertyName = "operator")]
public string Operator { get; set; }
[JsonProperty(PropertyName = "value")]
public string Value { get; set; }
}
public class DataSourceRequestSortModel
{
[JsonProperty(PropertyName = "property")]
public string Property { get; set; }
[JsonProperty(PropertyName = "direction")]
public string Direction { get; set; }
}
I'm calling controller with this query;
http://localhost:9417/api/values/?_dc=1505751693766&page=1&start=99&limit=163&filter=%5B%7B%22Property%22%3A%22Code%22%2C%22Value%22%3A%22%22%2C%22Operator%22%3A%22eq%22%7D%2C%7B%22property%22%3A%22Name%22%2C%22value%22%3A%22%5Cu00fccretli%22%2C%22operator%22%3A%22eq%22%7D%5D
Basically, i'm sending this value as querystring;
_dc:1505755748815
page:1
start:99
limit:163
sort:[{"property":"Name","direction":"ASC"}]
filter:[{"property":"Code","value":"","operator":"eq"},{"property":"Name","value":"Ucretli","operator":"eq"}]
As a result, i'm getting this;
{
"start": 99,
"limit": 163,
"filter": null,
"sort": null
}
Json.net deserializer could not parse filter and sort properties, just simply ignoring them. Even if i change property names, this time it does not return null values but again, i'm getting empty array.
Any ideas which causes that problem?
Thank you in advance.
Edit 1: In DataSourceRequestModel, i've both tried List and DataSourceRequestFilterModel[] array changes and still no success.
Edit 2: For further references, i'm going to share the solution i've found. As dbc2 has mentioned here below, dotnet core does not use Json.Net for complex model binding so the properties in model stays null. So you have to create and use your own custom model binders, detailed here;
http://www.dotnetcurry.com/aspnet-mvc/1368/aspnet-core-mvc-custom-model-binding
Thanks again to everyone who has involved.
I've got a REST hello world service up and running with ServiceStack.
It currently returns JSON from a test object that looks like:
{"Name":"Value"}
The object is simply:
public class TestResponse { public string Name { get; set; } }
Does anyone how I can decorate the class to force a root name in the JSON so it appears like so:
{ root:{"Name":"Value"} }
Thanks.
The JSON returned matches the exact shape of the DTO you populate (i.e. the role of a DTO in the first place).
So you should change the DTO to represent the exact shape you want, e.g.
public class TestResponse {
public TestRoot Root { get; set; }
}
public class TestRoot {
public string Name { get; set; }
}
Then you can return it as you would expect:
return new TestResponse { Root = new TestRoot { Name = "Value" } };
Or if you prefer, you could also use a Dictionary:
public class TestResponse {
public TestResponse() {
this.Root = new Dictionary<string,string>();
}
public Dictionary<string,string> Root { get; set; }
}
and return it with:
return new TestResponse { Root = { { "Name", "Value" } } };