Example #1
0
            protected override bool TrySerializeAny(int fieldNumber, SerializerFeatures features, TypeModel model, ref ProtoWriter.State state, object value)
            {
                var serializer = TypeModel.TryGetSerializer <T>(model);

                if (serializer == null)
                {
                    return(false);
                }
                // note this null-check is non-trivial; for value-type T it promotes the null to a default
                T typed = TypeHelper <T> .FromObject(value);

                CheckAnyAuxFlow(features, serializer);
                if ((features & SerializerFeatures.CategoryMessageWrappedAtRoot) == SerializerFeatures.CategoryMessageWrappedAtRoot)
                {
                    if (fieldNumber != TypeModel.ListItemTag)
                    {
                        ThrowHelper.ThrowInvalidOperationException($"Special root-like wrapping is limited to field {TypeModel.ListItemTag}");
                    }
                    state.WriteAsRoot <T>(typed, serializer);
                }
                else
                {
                    state.WriteAny <T>(fieldNumber, features, typed, serializer);
                }
                return(true);
            }