Y
Y
Yordi2018-08-29 15:26:13
ASP.NET
Yordi, 2018-08-29 15:26:13

How to hide unwanted fields in JSON response using Dapper?

I am writing a REST API server on ASP.NET Core 2.1. I want to use Dapper as ORM. Previously, he developed only small desktop projects for working with the database.
There is a class corresponding to a table in the database:

public class User
    {
        public int id { get; set; }
        public string FIO { get; set; }
        public int age { get; set; }
        public string city { get; set; }
        public string somedata1 { get; set; }
        public string somedata2 { get; set; }
        public string somedata3 { get; set; }
    }

Interface:
public interface IUser
        {
            IEnumerable<User> GetUsers();
            IEnumerable<User> GetSomeData1();
            IEnumerable<User> GetSomeData2();
        }

The class that implements it:
public class UserRepository : IUser
    {
        /* ... */

        public IEnumerable<User> GetUsers()
        {
            using (IDbConnection connection = GetConnection())
            {
                return connection.Query<User>("select id, fio, age, city, somedata1, somedata2, somedata3 from users");
            }
        }
    }

In the controller class I use the method from the UserRepository:
[HttpGet]
        public IEnumerable<User> GetUsers()
        {
            return _userRepository.GetUsers();
        }

I get something like this JSON response:
[{"id":1, "fio":"Иванов И. И.", "age":10, "city":"dc", "somedata1":"data1", "somedata2":"data2", "somedata3":"data3"}]

Good. How can I now implement the GetSomeData1() method so that only those table fields that I select in the query are returned in JSON? Why not create several variations of the entity class from the base for each method?
For example, to code:
public IEnumerable<User> GetSomeData1()
        {
            using (IDbConnection connection = GetConnection())
            {
                return connection.Query<User>("select id, fio, somedata3 from users where id in (1,2)");
            }
        }

returned
[{"id":1, "fio":"Иванов И. И.", "somedata3":"data3"},
{"id":2, "fio":"Петров А. А.", "somedata3":null}]

instead of
[{"id":1, "fio":"Иванов И. И.", "age":null, "city":null, "somedata1":null, "somedata2":null, "somedata3":"data3"},
{"id":2, "fio":"Петров А. А.", "age":null, "city":null, "somedata1":null, "somedata2":null, "somedata3":null}]

Found a variant using an attribute
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
on the field of the entity class, but it seems that this method will not work for me, because it will also be necessary to return JSON with Null fields.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2018-08-29
@devspec

Something like this

public class Car
{
  // included in JSON
  public string Model { get; set; }
  public DateTime Year { get; set; }
  public List<string> Features { get; set; }

  // ignored
  [JsonIgnore]
  public DateTime LastModified { get; set; }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question