-1

I am using Jsonserializer.SerializeObject trying to convert a byte[] into a specific object.

The class structure of message is following:

public class ProjectMessageQueueMessage
{
    public ProjectMessageQueueMessage();
    public byte[] MessageData { get; set; }
    public string MessageID { get; set; }
    public string MessageType { get; set; }
}

And when I try and serialize it into a specific class, like so

byte[] output = JsonSerializer.SerializeObject<ExtendedScanMessage>(message.MessageData);

I get the following error:

Cannot convert from byte[] to ExtendedScanMessage

I can remove the type, like so:

byte[] output = JsonSerializer.SerializeObject(message.MessageData);

But then output will not be serialized to my class.

Am I missing something?

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
andrewb
  • 2,995
  • 7
  • 54
  • 95
  • Yes, you are missing something `ExtendedScanMessage` isn't a `byte[]`. Without some sort of deserialization it won't ever get something else. You are now trying to serialize that byte array to another byte array. Not sure what that means. – Patrick Hofman Oct 20 '16 at 15:22
  • Serialisation makes string from object. what you want to do is DESERIALIZE – Misiakw Oct 20 '16 at 15:22
  • @Misiakw But then first from byte array to a string. – Patrick Hofman Oct 20 '16 at 15:23
  • @PatrickHofman yep, sure. he shoulb make string from byte and than try `JsonConvert.DeserializeObject(outputAsString)` – Misiakw Oct 20 '16 at 15:26

1 Answers1

0

depending on encoding you use you should write something like in code below

var outputString = Encoding.Default.GetString(message.MessageData);
var output = JsonConvert.DeserializeObject<ExtendedScanMessage>(outputString);

depending on your encoding you may use some of folowing: Encoding.Default Encoding.ASCII Encoding.BigEndianUnicode Encoding.UTF32 Encoding.UTF7 Encoding.UTF8 Encoding.Unicode

Misiakw
  • 902
  • 8
  • 28