Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there @JsonView equivalent in ASP.NET Web API

I have much more experience in Spring and Java, but now I am working on ASP.NET Web API project.

So in Spring there is @JsonView annotation with which I can annotate my DTOs, so I could select which data I will show through REST. And I find that very useful. But I cannot find any equivalent in ASP.NET. So I would need to create DTO for every special usecase.

So for example in Java if I have UserEntity that contains information about users. Some information can be seen publicly and some can be seen only by admins. The siple solution could be this

public class UserEntity {
  @JsonView(Views.Public.class)
  @JsonProperty("ID")
  private Integer id;

  @JsonView(Views.Public.class)
  private String name;

  @JsonView(Views.Admin.class)
  @JsonFormat(
  shape = JsonFormat.Shape.STRING, 
  pattern = "dd-MM-yyyy hh:mm:ss")
  private Date dateOfBirth;

  @JsonView(Views.Admin.class)
  private String email;

  @JsonIgnore
  private String password;
  private Integer version;
}

So in this case for equivalent functionality in ASP.NET I would need to create 2 DTOs. One for user that can be seen publicly and one for user that can be seen only by admin.

public class PublicUserDto {

  public int ID {get; set;}

  public String Name {get; set;}

}

public class AdminUserDto {

  public int ID {get; set;}

  public String Name {get; set;}

  public DateTime DateOfBirth {get; set;}

  public string Email {get; set;}
}

Is there any better solution? Is there some mechanism that I can use to create view over my data in ASP.NET Web API?

like image 509
Ivan Vasiljevic Avatar asked Apr 05 '18 12:04

Ivan Vasiljevic


People also ask

Can we return JSON from Web API?

By default, Web API produces XML but if there is need for JSON, given syntax will do it. Open WebApiConfig. cs file in solution and add mentioned line in it as shown in example.

What is the default data return format in Web API?

Special case formatters By default, string return types are formatted as text/plain (text/html if requested via the Accept header).

What is the best approach for requesting JSON instead of XML?

What is the best approach for requesting JSON instead of XML from an API? Add . json to the URL. APIs do not use XML.

What is serialization in Web API?

Serialization is the process of converting the state of an object into a form (string, byte array, or stream) that can be persisted or transported. Deserialization is the process of converting the serialized stream of data into the original object state.


1 Answers

JSON.NET has something called Conditional Property Initialization. You can write a method with the following format:

public bool ShouldSerialize[YourPropertyName]() => someBoolCondition;

JSON.NET will call that method to determine if that property should be serialized or not. So you could have something like:

public DateTime DateOfBirth {get; set;}

public bool ShouldSerializeDateOfBirth() => isAdmin;

It's not as pretty as JsonView but it should do the job.

like image 168
hardkoded Avatar answered Sep 29 '22 09:09

hardkoded