0

I have serialized a DataTable with f1 column as byte[]. The column is serialized to Base-64 string as given below:

        [
          {
            "f1": "5hAAJEA=",
            "f2": "hi_all",
            "id": 1
          },
          {
            "f1": "5hrwJEA=",
            "f2": "hi world",
            "id": 2
          }
        ]

I used the following code to serialize the datatable with Json.Net:

      var json = JsonConvert.SerializeObject(dataTable, settings);

When deserializing the json string to a DataTable, a datatable object is generated with acolumn f1 as string (not byte[]) with the same value of base64 string.

I think that is expected because the datatable is a generic structure without schema.

I know that deserializing using list<Dto> with a property of type byte[] can restore the byte[], but that is not an option because I use the datatable for bulk insert in sql database and deserialization is done at runtime without pre-definition of Dto(POCO) class.

I can create a Typed DataTable from the table definition in the database.

I tried to use ByteJsonConverter ,but it can't help.

You can check the complete c# program online to show the problem: with a complete trace for json serialization/deserialization process.

Update:

I used TypeNameHandling =TypeNameHandling.Objects, to keep data type in json and get json like:

        [
          {
            "f1": {
              "$type": "System.Byte[], mscorlib",
              "$value": "5hAAJEA="
            },
            "f2": "hi all",
            "id": 1
          },
          {
            "f1": {
              "$type": "System.Byte[], mscorlib",
              "$value": "5hrwJEA="
            },
            "f2": "hi world",
            "id": 2
          }
        ]

Now f1 is of type System.Byte[] and it's supposed that DataTableConverter create column of type byte[] not string.

When i try to deserialize, an exception error is fired:

Unexpected JSON token when reading DataTable: StartObject. Path '[0].f1', line 3, position 11

It seems that DataTableConverter can't handle $type/$value pair.

Check the source code

My question:

  • How to resolve that error for DataTableConverter.

  • How to deserialize a json string with Base-64 string to byte[] column in the DataTable.

M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • 1
    "the datatable is a generic structure without schema" So use strongly typed deserialization instead of a generic structure. `"5hrwJEA="` is a string, no matter what is in it, and generic deserialization will not make it anything else. – oerkelens Sep 11 '17 at 15:02
  • The deserialization is done at runtime without pre definition of strong object, but i can create a datatable with the valid schema at runtime. How to deseriialize using Typed datatable with schema. – M.Hassan Sep 11 '17 at 15:06
  • There's not enough information in the JSON to do what you want. You would need to go through all the string-type columns and check to see whether all values in each can be converted to binary successfully using `Convert.FromBase64String()`. The problem is that many commonly encountered strings are valid base64 -- basically any string that contains only the 52 capital and small letters and whose length is divisible by 4 passes the test. For instance `"Help"`, `"File"` and `"overflow"` are all valid base 64. – dbc Sep 11 '17 at 23:59
  • As a workaround solution, I can create the datatable with schema at runtime. How jsonconverter use that typed datatable and fill the the column(s) of type byte[] with Convert.FromBase64String(coresponding_json_token) – M.Hassan Sep 12 '17 at 00:46
  • I update the question and add type byte[] in json, and get an error in deserialization. – M.Hassan Sep 16 '17 at 00:39

0 Answers0