0

In C# application, I receive binary data at some point, which are serialized to JObject with structure

{
  "0": 255,
  "1": 216,
  "2": 255,
  "3": 224,
  "4": 0,
  "5": 16,
.
.
.
"12345" : 255

}

, so it's always {"index" : value}. EDIT: the json was just example, the JObject actually has 12345 children of type JToken, where each JToken has name "index" and value JValue (actual binary value).

I need to deserialize this JObject to byte[], with only values stored. Is there any smart way to do this, besides going through the object in cycle and storing the values in byte array, one by one?

Honza
  • 59
  • 9

1 Answers1

0

The code you are looking for is :

jObject.Properties().Select(p => (byte) p.Value).ToArray();

But I still have 2 questions :

  1. Do you really need to convert it from binary to JSON ? Can't you directly deserialize it into an object ?

  2. It's clearly an array so why isn't your JSON an array like this :

    [ 255, 216, ... ]

Arcord
  • 1,724
  • 1
  • 11
  • 16
  • 1. In the app, there is somehow "generic" endpoint that is able to accept any data and load it to json. Using it for file transfer is only an extremely specific use case and any bigger changes to that endpoint would break many things (legacy code, unfortunately). So I'm only using interface that I am given. 2. For some reason, this is how axios is serializing my binary data for REST Post. Since I really don't know much about frontend and I need to manipulate the data on backend anyway, I decided to fix it on backend, though I'm aware that the right way would be stop axios from doing it. – Honza Mar 08 '21 at 13:08