1

I'm working on Web Application (ASP.Net MVC) that will allow users to edit a JSON object in the browser and save it to database.

To do so, JSON object will be sent from view to server side (controller) and will be converted to entities (C# objects). At that moment, I can perform server side validation. After that, entities are converted back to JSON and saved to database.

I understand the necessity of server side validation, but I'm not happy with converting JSON to C# entities and back to JSON.

Is this a proper way to do server side validation? If not, How can I properly validate and save JSON object from client to server?

  • 1
    Marshalling and Unmarshalling is very common and very useful. – Kieveli Apr 12 '18 at 13:40
  • Could you just use regex to validate the JSON? – Daniel Tate Apr 13 '18 at 3:46
2

JSON object will be sent from view to server-side (controller) and will be converted to entities (C# objects). At that moment, I can perform server-side validation. After that, entities are converted back to JSON [...]

I understand the necessity of server-side validation, but I'm not happy with converting JSON to C# entities and back to JSON.

Indeed, deserializing JSON strings to objects just for validating the content can be overkill. Overall, if we end up serializing them back to JSON (well, we don't need to serialize them again since we already have the original JSON).

Is this a proper way to do server-side validation?

Quoting @Christopher

Yes, this is a proper way to do validation - nothing is inherently wrong with the operation... But here are some other options if you want to improve performance and still maintain the current functionality

JSON Schemas

JSON schemas allow you to validate the JSON format (model) and several constraints like not nulls, required fields, matching patterns, etc. If your library is customizable, probably you will be allowed to extend the validations process so that, you can add new constraints (rather related to the business). If you are familiar with XML, JSON Schemas and XSD are homologous.

JSONPath

In addition to JSON Schemas we have JsonPath1 for querying JSON strings without having to map JSONs to specific data models. Well, at least not to one we have to care about. If you need a programmatic approach (in contrast with declarative, as JSON Schemas use to be), JsonPath should do the job.

In any case, both are not mutually exclusive.


1: This is pretty much the same idea of XPath

  • 1
    I think this addresses the nuanced question of "is this efficient", but for the actually question posed, I think the answer should include "Yes, this is a proper way to do validation - nothing is inherently wrong with the operation... But here are some other options if you want to improve performance and still maintain the current functionality". – Christopher Apr 11 '18 at 14:53
  • @Christopher you are right. I focused only on the underlying question. – Laiv Apr 11 '18 at 15:38
  • Still the best answer. If OP does not want to convert to C# objects then using JsonPath or Schemas is the next best thing (also great tool to have in your pocket). But unless it is noticeably impacting performance or scalability is a concern, they can probably maintain course. – Christopher Apr 11 '18 at 15:44
1

You can use JSON schema to enforce type checking and validation on JSON objects. You can rewrite your server in Node and use one of the many NPM packages available for validating JSON objects against a JSON schema document.

If rewriting the server in node is not an option, you can use the Newtonsoft JSON Schema library.

  • 1
    Your answer makes it sound like rewriting in Node just to get JSON schema validation is preferable to using a library in the existing project. – Sebastian Redl Apr 11 '18 at 12:29
  • Using JSON Schema and rewriting in Node makes perfect sense in his case. He is deserialising objects in .Net to perform model validation only to serialise it back to JSON to store it in the database. This is not only inefficient but it can also be error prone. Because the serialisation process is not guaranteed to return the same JSON string if you take account locales in dates and amounts. – Kriztian Jake Sta. Teresa Apr 12 '18 at 12:46
  • This may be part of a larger application. All he needs is schema validation, which Newtonsoft.Json is perfectly capable of providing. Are you seriously suggesting that introducing a Node environment into the backend is a reasonable thing to do? – Sebastian Redl Apr 12 '18 at 14:07
  • I did suggest using Newtonsoft JSON schema library if rewriting the server is not an option. What I failed to mention was that its a paid add on to Newtonsoft.JSON. The question expressed concern about deserialising and serialising. And asks about how to properly do server side validation of JSON from client to database. My answer addresses these 2 points accounting for the facts that was presented. Whether its acceptable in his environment or not is up to him to decide. – Kriztian Jake Sta. Teresa Apr 12 '18 at 23:45
0

JSON is a format that is used to transfer information from one place to another.

Your C# code accepts JSON from the server, converts it to C# objects, and possibly modifies the C# objects and sends them to the server. Of course it sends them as JSON.

Your server code stores its object any way it likes (for example in an SQL database), and when requested converts its data to JSON and sends it to a client, or receives JSON from the client, then checks it and stores it any way it likes.

You NEVER operate on JSON data directly. Your code converts between JSON and your code's model objects. Other code (like on the server) does the exact same thing. The details are none of your business, except that the server provides and accepts JSON as the common exchange format.

  • I was thinking about improving performance but it seems it is not a good idea to skip converting JSON to C# model objects. – Mhd Apr 11 '18 at 14:23
  • 1
    Depends. Do you need to operate with the model they represent? In other words, is there any logic that requires you to convert JSON to a specific domain data model? if yes, @gnasher729 is right. You always want to operate with your domain model. If not, if you only need query the content, mapping JSON to objects is arguable. – Laiv Apr 11 '18 at 15:34
  • You avoid bugs by always going through the same code. Like always go from JSON to model object. That way, if anything changes with the mapping, you don't have some JSON only code lying around that suddenly is broken. – gnasher729 Apr 12 '18 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.