public DataNode Write(ISerializationManager serializationManager, int value, bool alwaysWrite = false,
                              ISerializationContext?context = null)
        {
            var sequenceNode = new SequenceDataNode();
            var flagType     = serializationManager.GetFlagTypeFromTag(typeof(TTag));

            // Assumption: a bitflag enum has a constructor for every bit value such that
            // that bit is set in some other constructor i.e. if a 1 appears somewhere in
            // the bits of one of the enum constructors, there is an enum constructor which
            // is 1 just in that position.
            //
            // Otherwise, this code may throw an exception
            var maxFlagValue = serializationManager.GetFlagHighestBit(typeof(TTag));

            for (var bitIndex = 1; bitIndex <= maxFlagValue; bitIndex++)
            {
                var bitValue = 1 << bitIndex;

                if ((bitValue & value) == bitValue)
                {
                    var flagName = Enum.GetName(flagType, bitValue);

                    if (flagName == null)
                    {
                        throw new InvalidOperationException($"No bitflag corresponding to bit {bitIndex} in {flagType}, but it was set anyways.");
                    }

                    sequenceNode.Add(new ValueDataNode(flagName));
                }
            }

            return(sequenceNode);
        }
        public DataNode Write(ISerializationManager serializationManager, HashSet <T> value, bool alwaysWrite = false,
                              ISerializationContext?context = null)
        {
            var sequence = new SequenceDataNode();

            foreach (var elem in value)
            {
                sequence.Add(serializationManager.WriteValue(typeof(T), elem, alwaysWrite, context));
            }

            return(sequence);
        }
Exemple #3
0
        private DataNode WriteInternal(ISerializationManager serializationManager, IEnumerable <T> value, bool alwaysWrite = false,
                                       ISerializationContext?context = null)
        {
            var sequence = new SequenceDataNode();

            foreach (var elem in value)
            {
                sequence.Add(serializationManager.WriteValue(typeof(T), elem, alwaysWrite, context));
            }

            return(sequence);
        }
        public SerializationArrayBenchmark()
        {
            InitializeSerialization();

            OneStringDefNode = new SequenceDataNode();
            OneStringDefNode.Add(new MappingDataNode
            {
                ["string"] = new ValueDataNode("ABC")
            });

            TenStringDefsNode = new SequenceDataNode();

            for (var i = 0; i < 10; i++)
            {
                TenStringDefsNode.Add(new MappingDataNode
                {
                    ["string"] = new ValueDataNode("ABC")
                });
            }
        }