skip to Main Content

I’m developing application to generate test data.

I don’t know how it’s called, but I need to change JSON schema from:

{
    "id": 1,
    "data": "Daniel",
    "typeID": 1,
    "type": {
        "id": 1,
        "name": "Name",
        "templates": []
    }
}

to:

{
    "id": 1,
    "data": "Daniel",
    "typeID": 1,
    "type": "Name"
}

This is my controller:

[HttpGet("{data}")]
public async Task<IActionResult> GetOne(string data)
{
    Template template =_templateService.GetOne(data);

    if (template == null)
        return await Task.FromResult(NotFound($"Not Found: {data}"));

    return await Task.FromResult(Ok(template));
}

Entities:

public sealed class Type
{
    public int ID { get; set; }
    public string? Name { get; set; }
    public List<Template> Templates { get; set; } = new(); 
}

public sealed class Template
{
    public int ID { get; set; }
    public string? Data { get; set; }
    public int TypeID { get; set; }
    public Type? Type { get; set; }
}

And I am using Newtonsoft.Json.

I tried using [JsonIngore] near

public List<Template> Templates { get; set; } = new();

but that doesn’t seem to be working.

2

Answers


  1. You misunderstood the purpose of the JsonIgnore attribute as mentioned in the comment. From your question and expected result, you need a type casting from the input data to the expected result.

    1. Create a class for the expected result. Implement the implicit conversion operator to convert a Template instance to a TemplateOutputModel instance.
    public class TemplateOutputModel
    {
        public int ID { get; set; }
        public string? Data { get; set; }
        public int TypeID { get; set; }
        public string Type { get; set; }
        
        public static implicit operator TemplateOutputModel(Template template)
        {
            return new TemplateOutputModel
            {
                ID = template.ID,
                Data = template.Data,
                TypeID = template.TypeID,
                Type = template.Type.Name
            };
        }
    }
    
    1. Cast the Template instance to the TemplateOutputModel instance.
    [HttpGet("{data}")]
    public async Task<IActionResult> GetOne(string data)
    {
        Template template = _templateService.GetOne(data);
    
        if (template == null)
            return await Task.FromResult(NotFound($"Not Found: {data}"));
    
        return await Task.FromResult(Ok((TemplateOutputModel)template));
    }
    
    Login or Signup to reply.
  2. To use JsonIgnore is a good idea, but you you will have to add an extra string property too

    using System.Text.Json;
    
    public sealed class Template
    {
        public int ID { get; set; }
        public string? Data { get; set; }
        public int TypeID { get; set; }
        
        [System.Text.Json.Serialization.JsonIgnore]
        public Type? Type { get; set; }
        
        [JsonPropertyName("type")]
        public string TypeName { get {return Type.Name;}}
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search