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.
testis not valid JSON, whether it's represented as a byte array or not.