C
C#3y ago
androidui

serializer runtime type checking?

when writing a Serializer and Deserializer, how would i go about ensuring stuff written and read in the correct order and with expected read sizes and positions (eg in case serializing misses a field that is otherwise written) eg
ser() {
write();
write();
write();
write();
write();
}

des() {
read();
read();
read();
read();
// missing one here
}
ser() {
write();
write();
write();
write();
write();
}

des() {
read();
read();
read();
read();
// missing one here
}
6 Replies
androidui
androiduiOP3y ago
for example
public void WriteSKPaint(SKPaint paint)
{
if (WriteNullable(paint))
{
WriteSKFont(paint.ToFont());
Write((byte)paint.BlendMode);
WriteSKColorF(paint.ColorF);
WriteSKColorFilter(paint.ColorFilter);
Write(paint.FakeBoldText);
Write((byte)paint.FilterQuality);
Write((byte)paint.HintingLevel);
WriteSKImageFilter(paint.ImageFilter);
Write(paint.IsAntialias);
Write(paint.IsAutohinted);
Write(paint.IsDither);
Write(paint.IsEmbeddedBitmapText);
Write(paint.IsLinearText);
Write(paint.IsStroke);
Write(paint.LcdRenderText);
WriteSKMaskFilter(paint.MaskFilter);
WriteSKPathEffect(paint.PathEffect);
WriteSKShader(paint.Shader);
Write((byte)paint.StrokeCap);
Write((byte)paint.StrokeJoin);
Write(paint.StrokeMiter);
Write(paint.StrokeWidth);
Write((byte)paint.Style);
Write(paint.SubpixelText);
Write((byte)paint.TextAlign);
Write((byte)paint.TextEncoding);
Write(paint.TextScaleX);
Write(paint.TextSize);
Write(paint.TextSkewX);
}
}
public void WriteSKPaint(SKPaint paint)
{
if (WriteNullable(paint))
{
WriteSKFont(paint.ToFont());
Write((byte)paint.BlendMode);
WriteSKColorF(paint.ColorF);
WriteSKColorFilter(paint.ColorFilter);
Write(paint.FakeBoldText);
Write((byte)paint.FilterQuality);
Write((byte)paint.HintingLevel);
WriteSKImageFilter(paint.ImageFilter);
Write(paint.IsAntialias);
Write(paint.IsAutohinted);
Write(paint.IsDither);
Write(paint.IsEmbeddedBitmapText);
Write(paint.IsLinearText);
Write(paint.IsStroke);
Write(paint.LcdRenderText);
WriteSKMaskFilter(paint.MaskFilter);
WriteSKPathEffect(paint.PathEffect);
WriteSKShader(paint.Shader);
Write((byte)paint.StrokeCap);
Write((byte)paint.StrokeJoin);
Write(paint.StrokeMiter);
Write(paint.StrokeWidth);
Write((byte)paint.Style);
Write(paint.SubpixelText);
Write((byte)paint.TextAlign);
Write((byte)paint.TextEncoding);
Write(paint.TextScaleX);
Write(paint.TextSize);
Write(paint.TextSkewX);
}
}
we might read this back incorrectly due to needing to serialize so many fields we might miss a field or two when writing the ReadSKPaint method to deserialize this or we might accidentally get the order wrong
Yawnder
Yawnder3y ago
I take it you just wanted to share that with us and don't have a question?
androidui
androiduiOP3y ago
is there way to verify/check such when writing these methods? possibly at runtime? eg read_incorrect_field() // generates an error at runtime if field length and position differs from the one written
Yawnder
Yawnder3y ago
That's basically your job, as a dev, to ensure you're not missing any. Unit tests would be a way to increase your confidence in your code, and no, it doesn't execute at run time. If you want to ensure the input is in the proper format, that's a different thing. You would go about it the same way as you would in any other method. But given the context, I do have to wonder why you're not using an existing serializer.
androidui
androiduiOP3y ago
for example WriteInt(); // write at position 0 ReadSingle(); // read at position 0 but would error at runtime due to incorrect type and/or size kinda like runtime type checking but for serialized data i guess cus i dont have access to the source code (eg i cannot simply make it [Serializable]) but i CAN manually serialize it anyway are there any kind of serializers that do this kind of checking? something like this, which the Reader will verify when reading and error if it detects it is attempting to read an incorrect type and incorrect length
Want results from more Discord servers?
Add your server