1

I have a byte[] and I want to parse it as a JsonObject but I want to avoid converting the byte[] to a string for performance reasons.

Here's what I've got so far:

var byteTest = Encoding.ASCII.GetBytes("test".ToCharArray());

JsonSerializer serializer = new JsonSerializer();
JObject data;
using (MemoryStream ms = new MemoryStream(byteTest))
using (StreamReader streamReader = new StreamReader(ms))
{
   data = (JObject)serializer.Deserialize(streamReader, typeof(string));
}

return data;

But I'm getting this error: Unexpected character encountered while parsing value: e. Path '', line 1, position 1. any ideas how I can do this correctly?

10
  • 4
    That isn't valid JSON. Commented Aug 22, 2016 at 14:53
  • I'm not sure what you mean @SLaks. I'm trying to create json from the byte[] Commented Aug 22, 2016 at 14:54
  • At some point it's going to be converted to text as json is a text-based format. Would suggest avoiding this endeavor. Commented Aug 22, 2016 at 14:55
  • 1
    @batthing test is not valid JSON, whether it's represented as a byte array or not. Commented Aug 22, 2016 at 14:55
  • 1
    Are you trying to convert a byte array to json (serializing it) or a byte array containing json back to something (deserializing it)? Please clarify what you mean by "parse" in this case. Also, it would be helpful if you could post what you actually hope to get as a final result. Commented Aug 22, 2016 at 16:31

1 Answer 1

3

The c# literal "test" is not valid JSON because it evaluates to the JSON literal test -- without the quotes. Since a JSON string literal must be quoted, you must do:

var byteTest = Encoding.ASCII.GetBytes("\"test\"");

Next, serializer.Deserialize(streamReader, typeof(string)); will return a string, which cannot be cast to a JObject. Thus the line

data = (JObject)serializer.Deserialize(streamReader, typeof(string));

will generate an InvalidCastException.

If for some reason you need to re-serialize that deserialized string (or any deserialized POCO) into a LINQ-to-JSON hierarchy, you can use JToken.FromObject():

using (MemoryStream ms = new MemoryStream(byteTest))
    using (StreamReader streamReader = new StreamReader(ms))
    {
        var value = serializer.Deserialize(streamReader, typeof(string));
        token = JToken.FromObject(value, serializer);
    }

Or if you really just needed to load a LINQ-to-JSON hierarchy directly from a stream, use JToken.ReadFrom:

using (var ms = new MemoryStream(byteTest))
    using (var streamReader = new StreamReader(ms))
        using (var jsonReader = new JsonTextReader(streamReader))
        {
            token = JToken.ReadFrom(jsonReader);
        }

Here JToken is an abstract base class that can represent any of the data types (object, array, value, string, number) from the JSON Standard. JObject cannot be used because a string value will actually get serialized to a JValue rather than a JObject.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.