Issues with .NET Web API when deserializing JSON for an object

I caught up with the project that was developed by the company for us, unfortunately, we do not get much support, and in the long term we must secure support for this. The application consists of a simple web client (HTTP, JavaScript, Knockout Framework) and a REST API service (.NET 4.5, ASP.NET MVC, I think).

Currently, I am only modifying the client, so the server should work as expected.

On clients, I slightly changed the knockout representation model (added some computable ones and optimized the representation of some values). The View model consists of “Questions” and “Comments” (as an array of questions, this is actually a Bug tracker). If I create a new problem, the description is added to the first comment, the entire JSON.stringified model and posting to the .NET API Service. I supported Firebug that the JSON that is being sent looks like this:

{ "Id":0, "Title":"THis is a title", "Comments":[ { "Id":1, "Text":"this is the first comment" } ] } 

On the client side, I have an "IssueController":

 [HttpPost] public HttpResponseMessage PostIssues( Issue issue ) { //issue should already hold the deserialized content of the JSON here, //but it only contains 'Id' and 'Title' not the 'Comments' (empty List) ... } 

The problem domain model object also has an array for storing comments, but for this purpose it is already empty. There is no part in the .NET code that explicitly parses JSON, as I understand it, the MVC Framework does this implicitly using equal property names (is this correct?).

The deserialization was already working as expected, so the .NET code should be fine, but it looks like I modified JSON so that this implicit display of comments no longer works. Unfortunately, I do not have much experience with the .NET MVC Framework (or is it just the .NET WebAPI Framework, I can’t even tell you about it).

These are my questions:

  • What is the .NET REST API Framework? How can I tell?
  • How does this implicit JSON deserialization work and what are its pitfalls, for example, when parts of JSON do not receive deserialization, as expected? Especially on the client side (as I said, I did not change the server code)
  • Any ideas on what I could do with JSON that he doesn't like the server anymore ?;)

EDIT

Issue.cs looks like this (simplified):

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Bo; using Projects.DomainObjects; namespace Projects.Models { public class Issue : DomainObject { public Issue() { this.Comments = new List<Comment>(); } public long Id { get; set; } private string _Title; public string Title { get { return _Title; } set { _Title = value; NotifyChanged(); } } public List<Comment> Comments { get; set; } } 

Comment.cs:

 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Common; using Projects.DomainObjects; namespace Projects.Models { public class Comment : DomainObject { public Comment() { } public long Id { get; set; } public string Text { get; set; } } 
+6
source share
2 answers

I decided the problem was calculated, which returns a sorted array of comments. So my knockout model contains

 self.Comments = ko.observableArray([]); //...which gets filled with the comments of an issue 

and a

 self.CommentsSorted = ko.computed(function() {...}); //...which simply returns a sorted Comments array 

So when I serialize this model, the published JSON now represents the Comments Array, but also the CommentsSorted array. Only when i do

 var i = ko.toJS(issue); delete i.CommentsSorted; 

before i send i as data, .NET is able to deserialize Comments correctly.

The mysterious thing about this is that there were always other computed fields in my knockout model that are completely ignored by .NET and do not interfere with deserialization in any way. It looks like it mostly depends on the name and type of fields in the model (maybe if the first letters are equal?).

The good thing: it works now The bad thing: It is not really deterministic how .NET does the JSON data deserialization, and I also can not debug if it does not behave as expected.

+2
source

I just tried my code right from your post and it worked for me.

So there can be several things on your side:

  • When you submit the object to the server, make sure that you transfer back from the knockout observed on the json object. Therefore, in your ajax request, make sure that it looks like this: data: ko.toJSON(issue) , and not just data: issue .
  • When you submit the object to the server, make sure you send the header content-type: application/json

Here are the answers to other questions:

What is the .NET REST API Framework? How can I tell?

This is not like any custom (at least what you posted), it is just a direct web API in .NET.

How does this implicit JSON deserialization work and what are its pitfalls, for example, when parts of JSON do not receive deserialization, as expected? Especially on the client side (as I said, I did not change the server code)

The deserialization on the server uses a set of formatters that work based on the content-type set by the client. It can become complicated if you want to configure it, but there is information here

Any ideas on what I could do with JSON that the server no longer likes ?;)

As I said, your code worked for me!

+2
source

All Articles