-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE REQUEST] SerializeWhile attribute, the negation of SerializeUntil #179
Comments
I would second a similar idea. I was looking at RFC8010 section 3.1.3 to describe the "additional attribute" (which is defined when a field is 0) and having a difficult time without this type of operator. I was thinking more of: [ItemSerializeWhile(nameof(AttributeValue.NameLength), 0x00)]
public List<AttributeValue>? AdditionalAttributes { get; set; } |
Working on this now. My plan is to add a configurable comparison operator to the SerializeUntil and ItemSerializeUntil attributes. |
Perfect :) |
I've run into a tricky edge condition with this. The current behavior during serialization is to correctly set the termination value of the final item in a collection. This causes inconsistent behavior during serialization now that the termination condition is configurable. For example, if the condition is != then to what does the final value get set? It seems to me there are a number of options:
Not sure which I like the best yet. Possibly option 3 but when I run into complex cases like this it makes me feel like I'm missing something. Obviously none of this is a problem during deserialization, which is what I assume your use cases are restricted to anyway :) |
There's another confusing bit here where currently [SerializeUntil(0, ComparisonOperator = ComparisonOperator.NotEqual, LastItemMode = LastItemMode.Defer]
public byte[] Padding { get; set; } I guess that would work but it's kind of crappy. |
The more I think about it, the more I like your suggestion @brogdonm. Maybe SerializeWhile and ItemSerializeWhile are more explicit. It would be nice to get ride of the last item mode altogether as well but I need to think about that. |
But a |
@jefffhaynes Yeah, I forked your repo and tried a couple different routes and was getting stuck too, but I think most of it was me still learning the code. In my case I have a deserialize and a serialize scenario. |
Yeah @brogdonm I would say in some sense your scenario is more straightforward. |
@sn4k3 I agree it's confusing. Unfortunately the current default behavior for SerializeUntil is to discard the terminating value. So in your example, the resulting string would be REVIEW rather than PREVIEW. |
Yup, i guessed that. We should be able to control whatever to discard or seek back the sizeof value |
Sorry, tbh I'm recovering from surgery and the pain meds are not helping my thought process. Let me regroup and take another run at this in a bit :) |
Man, hope you recover without issues. |
Well this is bonkers. After spending many hours on this I've resigned myself to the fact that the only way to implement it is to remove byte arrays as a "primitive" type from the serialization system. That's something that always bothered me but I originally did for performance reasons. However, I've since made performance improvements to other areas that hopefully will allow me to make the change. So bear with me. This turned out to be a doozy... |
Ok, I think I have it working but currently there is almost a 10x performance hit when dealing with arrays. I'll probably publish an alpha package tomorrow and maybe you can take a look and see if it checks all the boxes before I spend a lot of time optimizing. |
https://www.nuget.org/packages/BinarySerializer/8.6.3-alpha Good luck. Try SerializeWhile and ItemSerializeUntil with new configurations. Let me know if you need help with it. There's a slight gotcha with SerializeWhile but I'll wait to tell you :) |
It is nearly there, I have a regression issue with using public class StringValue : Value<string>
{
[FieldOrder(0)]
[FieldLength(nameof(AttributeValue.ValueLength), RelativeSourceMode = RelativeSourceMode.FindAncestor, AncestorType = typeof(AttributeValue))]
public new string? Data { get; set; }
} public abstract class Value<T> : Value
{
[FieldOrder(0)]
[FieldLength(nameof(AttributeValue.ValueLength), RelativeSourceMode = RelativeSourceMode.FindAncestor, AncestorType = typeof(AttributeValue))]
public new T? Data { get; set; }
} and public class Value
{
[FieldOrder(0)]
[FieldLength(nameof(AttributeValue.ValueLength), RelativeSourceMode = RelativeSourceMode.FindAncestor, AncestorType = typeof(AttributeValue))]
public byte[]? Data { get; set; }
} And using like so for serialization purposes: [Subtype(nameof(Tag), ValueTags.IntegerValue, typeof(IntegerValue), BindingMode = BindingMode.OneWay)]
[Subtype(nameof(Tag), ValueTags.NaturalLanguage, typeof(StringValue), BindingMode = BindingMode.OneWay)]
[SubtypeDefault(typeof(Value))]
public Value? Value { get; set; } And on previous released version, this was serializing correctly. But now, it is always serializing the data into the default class property (e.g. as a
|
@jefffhaynes I looked for your changes in a branch to debug, but didn't see the relevant changes. If you need anything else form me, please let me know. With that said, it did appear to obey the |
@jefffhaynes any updates on this? |
Sometimes i can have unknown padding lenght that i need to skip. I don't find a attribute to by pass this so my proposal:
I know that can mess up with numbers and eat that bytes, but in my case i have a obligatory string after the padding, so in this case is safe for the use :)
Example highlight in blue:
That padding length vary on files from 8 to 12 bytes
The text was updated successfully, but these errors were encountered: