0

I am posting json string to my method and getting error while deserializing to my class.

Error:

Error converting value -1 to type 'System.Nullable`1[System.UInt64]'. Path 'MyMappings1.MySqlColumns[7].size'. Inner Exception : {"Value was either too large or too small for a UInt64."}

Below is my class in which I want to output after deserializing json value :

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. 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

I have seen this answer but my question is how do I use it while deserialization. I am using NewtonSoft json.

Update

Reason for specifying size as uint:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 7
    An unsigned integer cannot hold negative numbers. Why did you define it as such? – CodeCaster Jan 10 '17 at 07:29
  • 2
    Have you tried by changing `UInt64` to `Int64` – sujith karivelil Jan 10 '17 at 07:29
  • 2
    A well-written question! – C.Evenhuis Jan 10 '17 at 07:33
  • @un-lucky : After changing uint64 to int64 i get error while fetching columns from mysql :Specified cast is not valid.When i have uint64 i dont get this error – I Love Stackoverflow Jan 10 '17 at 07:45
  • @Learning If **fixing** a problem causes another one, you probably didn't fix the right problem. – Manfred Radlwimmer Jan 10 '17 at 07:59
  • @ManfredRadlwimmer:Sorry what do you mean by this.can you please tell me something specific to my problem – I Love Stackoverflow Jan 10 '17 at 08:02
  • @Learning You tried to fix a problem by introducing a new one - trying to fit a negative number into an UInt64, just because it apparently "fixes" another problem. The new problem you introduced cannot be solved by adding more workarounds. You should abandon the current approach and try to fix the original problem. – Manfred Radlwimmer Jan 10 '17 at 08:04
  • @ManfredRadlwimmer:You have a point but as you can see my update where it is taking unint64 that is why i have taken unint64 as datatype – I Love Stackoverflow Jan 10 '17 at 08:10
  • And the sad part is no matter how hard i will try to explain my question well and show my efforts i would still get downvotes even without knowing downvoting reasons :) – I Love Stackoverflow Jan 10 '17 at 08:12
  • I think the approach is not correct, then you have `nvarchar(max)`, this represent a definite size for a given database, is there a special reason, why you want it to be r represented as `-1,0,null`, why such alibis, UInt has very high limit, it will fill in all kinds of system values and you never enter the representational zone – Mrinal Kamboj Jan 10 '17 at 08:14
  • @MrinalKamboj: see what happens is i fetch columns from mysql database tables and then show it to users.there user can changes datatype size etc whatever user wants to changes and then user post this column configuration to my method.after reading this configuration i generate create table script which will generate table in my mssql local instance.So the thing is when i will read -1 value i understand that i need to use max as size in case of varchar and nvarchar – I Love Stackoverflow Jan 10 '17 at 08:18
  • @Learning No you don't. `UInt64?` is nullable and can be set to `null` instead of `-1`. If your Database contains `-1` then the data isn't `UInt64` – Manfred Radlwimmer Jan 10 '17 at 08:20
  • Would you consider using string instead, which can contain the value Max and other integers and while parsing you may wan to check if it contains integer or the Max value is defined – Mrinal Kamboj Jan 10 '17 at 08:22

2 Answers2

3

The U in types names stands for unsigned that is those values is always positive. So the value -1 is too small to be stored in such a variable. Either change your type to signed Int64. Or use null as marker for max and interpret it as accordingly while creating your column.


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. As it is sad you can't have your cake and eat it at the same time.

dbc
  • 104,963
  • 20
  • 228
  • 340
Rafal
  • 12,391
  • 32
  • 54
1

I would add new property (bool) to MySqlColumns class. Lets name it 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.

Tomas Chabada
  • 2,869
  • 1
  • 17
  • 18