Search code examples
servicestack-text

ServiceStack.Text Disposes Input Stream


When deserializing data from a stream ServiceStack.Text closes the input stream. Since there is no issue tracker at Github and their web site refers to SO I post the question here. A call to

 JsonSerializer.DeserializeFromStream<T>(stream);

will dispose the input stream which is in my opinion an error since there might be other data following which is only prefixed with an object. No other serializer I am aware of behaves this way.


Solution

  • The latest v5.1.1 of ServiceStack.Text on MyGet shouldn't auto close streams anymore.

    Also ServiceStack.Text has been rewritten to use .NET's new ReadOnlySpan<char> which can be accessed directly with:

    ReadOnlySpan<char> jsonSpan = json.AsSpan();
    var dto = JsonSerializer.DeserializeFromSpan<T>(jsonSpan);
    

    There's also a new API for reading from streams asynchronously:

    var dto = await JsonSerializer.DeserializeFromStreamAsync<T>(stream);