I have a case when I get a very big text data & each line contains some metadata + json data string. I need to process the json data on each line.
This is what I have:
public Data GetData(string textLine)
{
var spanOfLine = textLine.AsSpan();
var indexOfComma = spanOfLine.IndexOf(":");
var dataJsonStringAsSpan = spanOfLine.Slice(indexOfComma + 1);
// now use dataJsonStringAsSpan which is ReadOnlySpan<char> to deserialize the Data
}
Where Data is a Dto class which has bunch of (7) different attributes:
public class Data
{
public int Attribute1 { get; set; }
public double Attribute2 { get; set; }
// ... more properties, emitted for the sake of brevity
}
I'm trying to achieve this with System.Text.Json
API. Surprisingly it doesn't have any overload to deserialize from ReadOnlySpan<char>
, so I come up with this:
public Data GetData(string textLine)
{
var spanOfLine = textLine.AsSpan();
var indexOfComma = spanOfLine.IndexOf(":");
var dataJsonStringAsSpan = spanOfLine.Slice(indexOfComma + 1);
var byteCount = Encoding.UTF8.GetByteCount(dataJsonStringAsSpan);
Span<byte> buffer = stackalloc byte[byteCount];
Encoding.UTF8.GetBytes(dataJsonStringAsSpan, buffer);
var data = JsonSerializer.Deserialize<Data>(buffer);
return data;
}
While this works, it looks very convoluted.
Is this the way to go or am I missing something more simple ?
dataJsonStringAsSpan.ToString()
(ToString onReadOnlySpan<char>
is “copy these contents to a string”, not “be a debugging aid/display value”) – bartonjsstring
to begin with, it will go on the large object heap and possibly obviate any advantages you get from usingSystem.Text.Json
. (Maybe I'm misunderstanding though and eachtextLine
is not very big?) Are you reading the JSON from a local file, from a HTTP response, or something else? – dbcDeserialize(string json, Type returnType, JsonSerializerOptions options = null)
here. – dbcint start, int length
, and pass those toAsSpan(string, int, int)
. You'd also need your own version of a couple methods fromJsonReaderHelper
. Not sure it's worth it. – dbc