简体   繁体   English

JSON到Javascript序列化/反序列化验证

[英]JSON to Javascript serialisation/deserialisation validation

A lot of times we come across code where we use JSON format without actual data contract validation. 很多时候,我们遇到的代码中,我们使用JSON格式而没有实际的数据合同验证。 eg I have a ASP.Net server side code which returns a serialized answer to Javascript client. 例如,我有一个ASP.Net服务器端代码,该代码将序列化答案返回给Javascript客户端。 Further this client uses some of the values within the response to make other server side calls. 此外,此客户端使用响应中的某些值来进行其他服务器端调用。 Though not very frequent but we do see issues where someone increased the data type on server side breaking client side in really bad way. 尽管不是很频繁,但是我们确实看到了这样的问题:有人在服务器端增加了数据类型,从而以非常糟糕的方式破坏了客户端。 I am looking for suggestions how can I implement a proper build time validation system to avoid such issues 我正在寻找建议,如何实施适当的构建时间验证系统来避免此类问题

Let me explain with a hypothetical example. 让我用一个假设的例子来解释。 Client makes a call to getCars(int employeeID) ==> Response object has List<CarIDs, CarNames> . 客户端调用getCars(int employeeID) ==>响应对象具有List<CarIDs, CarNames> Using CarIDs iterative calls are made from UI to getCarDetails(CarID) . 使用CarIDs可以从UI对getCarDetails(CarID)进行迭代调用。 Now earlier the data type of CarID was int , which recently got updated to long. 现在, CarID的数据类型为int ,最近已更新为long。 Javascript had a limitation to handle only upto 15 digits numbers, so on the client side any digits are de-serialization are rounded up. Javascript有一个限制,只能处理最多15位数字,因此在客户端,任何反序列化的数字都被四舍五入。 This caused inaccuracy in results. 这导致结果不准确。

Let me know if there are any suggestions on how I can implement some validation/contract upfront to get rid of such issues. 让我知道是否有关于如何预先实施一些验证/合同以消除此类问题的建议。

JSON模式正是为此目的而设计的,并且出色的JSON.NET库支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM