简体   繁体   English

反序列化数据时,UInt64的值太大或太小

[英]Value was either too large or too small for a UInt64 while deserializing data

I am posting json string to my method and getting error while deserializing to my class. 我将json字符串发布到我的方法中,并在反序列化到我的类时遇到错误。

Error : 错误

Error converting value -1 to type 'System.Nullable`1[System.UInt64]'. 将值-1转换为类型'System.Nullable`1 [System.UInt64]'时出错。 Path 'MyMappings 1 .MySqlColumns[7].size'. 路径“ MyMappings 1 .MySqlColumns [7] .size”。 Inner Exception : {"Value was either too large or too small for a UInt64."} 内部异常:{“对于UInt64,值太大或太小。”}

Below is my class in which I want to output after deserializing json value : 以下是我要反序列化json值后要在其中输出的类:

public class MyMappings
   {
       public string Name { get; set; }
       public List<MysqlColumns> MySqlColumns { get; set; }
   }

public class MySqlColumns
    {
        public string Column { get; set; }
        public string Datatype { get; set; }
        public string IsNullable { get; set; }
        public UInt64? Size { get; set; }
        public UInt64? NumericPrecision { get; set; }
        public UInt64? NumericScale { get; set; }
    }

string myJson =  "MyMappings": [
    {
      "Name": "dbo.Table1",
      "MySqlColumns": [
        {
          "Name": "Address",
          "datatype": "nvarchar",
          "isNullable": "YES",
          "size": -1,
          "numericPrecision": null,
          "numericScale": null
        }]

var deserializeData = JsonConvert.DeserializeObject<MyMappings>(myJson);

Now when user set nvarchar(max) for any columns then I set size as -1 which is setinel value to represent max size. 现在,当用户为任何列设置nvarchar(max)时,我将大小设置为-1,这是代表最大大小的setinel值。 Now reason for setting size as -1 is because while reading column whose size is nvarchar(max) I get setinel value as -1 as per Marc Gravell's comment 现在将大小设置为-1的原因是因为在读取大小为nvarchar(max)的列时,根据Marc Gravell的评论,我将setinel值设置为-1

I have seen this answer but my question is how do I use it while deserialization. 我已经看到了这个答案,但是我的问题是在反序列化时如何使用它。 I am using NewtonSoft json . 我正在使用NewtonSoft json

Update 更新

Reason for specifying size as uint: 将尺寸指定为uint的原因:

在此处输入图片说明

The U in types names stands for unsigned that is those values is always positive. 类型名称中的U代表无符号,即这些值始终为正。 So the value -1 is too small to be stored in such a variable. 因此,值-1太小而无法存储在这样的变量中。 Either change your type to signed Int64 . 将您的类型更改为带符号的Int64 Or use null as marker for max and interpret it as accordingly while creating your column. 或使用null作为max的标记,并在创建列时相应地对其进行解释。


As for your update the only thing that you can do is to use null and make your logic interpret it as -1 when needed. 至于更新,您唯一可以做的就是使用null并在需要时让逻辑将其解释为-1。 As it is sad you can't have your cake and eat it at the same time. 很难过,您不能同时吃蛋糕。

I would add new property (bool) to MySqlColumns class. 我将新属性(布尔)添加到MySqlColumns类。 Lets name it MaxLength. 让我们将其命名为MaxLength。 This will be used as a max value marker. 这将用作最大值标记。 You will look at this property if your datatype is nvarchar, varchar or any other type, where you can define length. 如果您的数据类型是nvarchar,varchar或任何其他可以定义长度的类型,则将查看此属性。

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

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