I have book class that includes authors and also my author class includes books. So when I try to convert book to json string, it gives the referance loop error. So I do that
public IHttpActionResult GetAllForOfficer()
{
Library library = _libraryManager.GetById(ProviderAuthorization.libraryId);
List<Book> books = _bookManager.GetAll().Where(x=>x.Libraries.Contains(library)).ToList();
return Ok(JsonConvert.SerializeObject(books, Formatting.Indented,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}));
}
And this returns output like that
"[\r\n {\r\n \"Authors\": [\r\n {\r\n \"Books\": [],\r\n \"Id\": 4,\r\n \"Name\": \"Texe Marrs\"\r\n }\r\n ],\r\n \"Categories\": [],\r\n \"Comments\": [],\r\n \"Libraries\": [\r\n {\r\n \"Books\": [\r\n {\r\n \"Authors\": [\r\n {\r\n \"Books\": [\r\n {\r\n \"Authors\": [],\r\n \"Categories\": [\r\n {\r\n \"Books\": [],\r\n \"Id\": 5,\r\n \"Name\": \"dede\",\r\n \"ClickCounter\": 0\r\n },\r\n {\r\n \"Books\": [],\r\n \"Id\": 6,\r\n \"Name\": \"asas\",\r\n \"ClickCounter\": 0\r\n }\r\n ],\r\n \"Comments\": [\r\n {\r\n \"User\": {\r\n \"Comments\": [],\r\n
I have book class like this
public int Id { get; set; }
[Required]
[StringLength(10)]
public string ISBN10 { get; set; }
[Required]
[StringLength(13)]
public string ISBN13 { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Publisher { get; set; }
public int? NumberOfPages { get; set; }
public int? Revision { get; set; }
public int? LatestRevision { get; set; }
[StringLength(50)]
public string Language { get; set; }
[Column(TypeName = "date")]
public DateTime? CreateDate { get; set; }
public string Description { get; set; }
[Column(TypeName = "image")]
public byte[] Image { get; set; }
public int ClickCounter { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Comment> Comments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UserBook> UserBooks { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Author> Authors { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Category> Categories { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Library> Libraries { get; set; }
My output looks a little bit weird. It should be like "Authors":["Name":"Pete Mark"]. But there are some backslashes. I think I have a problem with converting class to json. How can I fix that ?
CodePudding user response:
you don' t need any special conversion, net will do it for you. If you try to make json looking prety, it doesn' t make any sense on the server side. You will have to deserialiaze or parse it on the client side after this.
public IHttpActionResult GetAllForOfficer()
{
Library library = _libraryManager.GetById(ProviderAuthorization.libraryId);
List<Book> books = _bookManager.GetAll().Where(x=>x.Libraries.Contains(library)).ToList();
return Ok(books);
}
CodePudding user response:
A quick way to solve your problem would be to add the [JsonIgnore] attribute to your Authors, Categories and Libraries properties in the Book class (and potentially some other attributes in your other classes aswell).
A better way would be to write custom JsonConverters for those properties. For example, the Book class could use a AuthorNameConverter which converts an Author object into a string with the author's name when serializing. You can add that converter as an attribute of your Authors property, or add it in the available converters of your JsonSerializerSettings.
You can find an example of a JsonConverter here : https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm
For adding it as an attribute :
[JsonProperty(ItemConverterType = typeof(AuthorNameConverter))]
public virtual ICollection<Author> Authors { get; set; }
