Newest Update w/ Answer
(10/26/2020) - Looking at @N.Dogac's solution I got a working solution reading this document https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to.
Answer can be seen here: https://github.com/RyanHarlich/My-Stackoverflow-Dotnet-Core-REST-API-Question/commit/0db8fa860eda7e9d201874f8f1abc38d168e0eaf.
Update
(10/2/2020) - I realized that reproducing this might be difficult and/or time consuming so I added a GitHub repo to focus on the actual problem; of course, this is only a sample and not my actual work so detail was left out. I only provided enough information to solve my question. Click here My GitHub Repo or click the actual link (https://github.com/RyanHarlich/My-Stackoverflow-Dotnet-Core-REST-API-Question)
If no one has a better solution I am going to try @N.Dogac's solution after awhile.
Question
Question: I was wondering how do I rename a JSONPropertyName with a client that uses a library in C# .NET Core REST API.
For example:
Library
Library DLL w/ MasterNode that enhances model detail
public abstract class APIModel {
public MasterNode Links { get; set; }
public void AddInfo(IUnitOfWork context) {
Links = context.InheritedMasterNode;
Links.Add();
}
public abstract class MasterNode {
public abstract List<float> Info { get; set; }
public void Add() {
Info = new List<float>() { 100.0f };
}
}
}
Model in Library DLL
public class Info : APIModel {
public int id;
public String title;
}
In the Library DLL contains the InfoController
[Route("api/[controller]")]
[ApiController]
public class InfosController : ControllerBase {
private readonly IUnitOfWork _unitOfWork;
public InfosController(IUnitOfWork context) {
_unitOfWork = context;
}
// GET: api/Infos
[HttpGet]
public async Task<ActionResult<IEnumerable<Models.HyperMediaControls.APIBase>>> GetAPIs() {
ResetInfo().Wait();
var conn = _unitOfWork.DBContext.Database.GetDbConnection();
if (conn.State != ConnectionState.Open)
conn.Open();
_unitOfWork.DBContext.Database.ExecuteSqlRaw("SET IDENTITY_INSERT Info ON");
Info info = new Library.Models.Info() { Id = 1 };
info.Title = "Info";
await _unitOfWork.Repository<Info>().InsertEntityAsync(info);
await _unitOfWork.SaveChangesAsync();
conn.Close();
Add(info);
return await _unitOfWork.Repository<Info>().ToListAsync();
}
private async Task ResetInfo() {
Info info = await _unitOfWork.Repository<Info>().FirstOrDefault();
if (info != null) {
_unitOfWork.Repository<Info>().Empty(info);
await _unitOfWork.SaveChangesAsync();
}
}
public void Add(Models.HyperMediaControls.APIBase info) {
info.AddInfo(, _unitOfWork);
}
}
Client
Client: [JsonPropertyName(“InfoInherited”)] is what I am trying to achieve
public abstract class InheritedMasterNode : MasterNode {
[JsonPropertyName(“InfoInherited”)]
public override List<float> Info { get; set; } = null;
}
Details
To get the child InheritedMasterNode from client to library I used this method, which I did not add as it is a lot of code for a Stack Overflow post: https://stackoverflow.com/a/59753787/8360201
- Note: You use the IUnitOfWork to inject the InheritedMasterNode into a MasterNode handler and use it in the APIModel class with the property InheritedMasterNode of UnitOfWork.
The problem that I am facing is as described in my comments here: https://stackoverflow.com/a/37927869/8360201
- Note: The answer to this Stack Overflow post does not work because it serializes to a string; where as, I use C# .NET Core REST API Controllers.
Please let me know what you need in order for me to help you reproduce this. I left out a lot of code as it is my private repo and it would be too much to read to understand the smaller problem.
Overview:
- Belongs to Library DLL: Info, InfoController, APIModel, MasterNode, UnitOfWork, ServiceCollectionExtensions
- Belongs to Client: InheritedMasterNode, Startup.ConfigureService
I have come to the conclusion that it is not possible and would require the .NET Core team to do an upgrade to these DLL, but I am giving it a final try by asking the Stack Overflow community:
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
Here is the actual output vs. desired: info -> infoInherited
Actual Output:
[
{
"id": 1,
"title": "Info",
"links": {
"info": [
]
}
}
]
Desired Output:
[
{
"id": 1,
"title": " Info ",
"links": {
"infoInherited": [
]
}
}
]