-1

I have created the following test vector class:

public class TestVector
{
   public UInt16 MaxBlockSize { get; }
   public byte[] Payload { get; set; }

   public TestVector(ushort maxBlockSize, byte[] payload)
   {
      MaxBlockSize = maxBlockSize;
       Payload = payload;
   }
 }

In my test, I am populating a list of vectors defined as per:

private static HashSet<TestVector> myVectors = new HashSet<TestVector>();

And then serialising "myVectors" using JsonConvert and write the result to a file as per:

var jsonOuput = JsonConvert.SerializeObject(myVectors , new JsonSerializerSettings{ObjectCreationHandling = ObjectCreationHandling.Replace})

File.WriteAllText(@"e:\MyJson.json", jsonOuput);

Here is a Json typical output (with a list/Hashset composed of 2 vectors):

[
 {
    "MaxBlockSize": 256,
    "Payload": "bjQSAAAAAABvNBIAAAAA..."
  },
  {
    "MaxBlockSize": 256,
    "Payload": "VjQSVzQSWDQS...."
  },
 ]

Now what I do not get is why "Payload" is serialised as a string and not as an array.
My questions are:

  1. What is this string format (ASCII code maybe?) and why is it used instead of a byte[] type of representation?
  2. Is there a way to get the "Payload" byte[] to be printed in a more readable way?
12
  • 2
    What would you expect it to be an array of, exactly? And what Bytes are binary data, but JSON is a text format, so it has to convert the binary data into a textual representation. I'm pretty sure it'll be a base64 string. What "readable" representation of binary data would you want? And why would it matter? JSON isn't really intended for human consumption. As long as you can deserialise it again (which you can) then it's irrelevant. Commented Dec 10, 2020 at 14:39
  • Usually, when I serialise an object, I get the actually values composing the array, not a string. eg. When I have a double[], I get a list of doubles. Commented Dec 10, 2020 at 14:41
  • 2
    That's because doubles can be directly represented in a text format (i.e. the written representation of a number). I guess the other way for your byte array could be a big strings of 0s and 1s but, would that really help you? What were you hoping to see, exactly? Also see my edit to the comment above, the bit about JSON not being intended to be read by humans. Same applies to raw binary data, for that matter. Commented Dec 10, 2020 at 14:43
  • @ADyson, the problem is that I need to do a sanity check compare of these vectors by eye against actual byte arrays. These strings are unreadable. But I get your point, thanks Commented Dec 10, 2020 at 14:48
  • 1
    stackoverflow.com/a/15228384/7565574 describes how to configure NewtonSoft JSON to serialize byte arrays like any other arrays. Commented Dec 10, 2020 at 14:59

1 Answer 1

2

What is this string format (ASCII code maybe?) and why is it used instead of a byte[] type of representation?

See json.Net documentation for primitive types:

Byte[] String (base 64 encoded)

So the format is base64. This is probably used since it is a reasonably efficient encoding of binary data, encoding 6 bits per character. Encoding values as an array would use much more space.

It is somewhat common to encode images or similar chunks of data as byte arrays. Since these can be large it is useful to keep the size down as much as possible.

Is there a way to get the "Payload" byte[] to be printed in a more readable way?

There are various base64 converters online that can convert it to hex, oct, string, or whatever format you prefer to view your binary data in. But for many applications it is not very useful since the binary data often represents something that is already serialized in some way.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarification, it makes sense!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.