Exemplo n.º 1
0
        public override DateTime Read(ref CborReader reader)
        {
            switch (reader.GetCurrentDataItemType())
            {
            case CborDataItemType.String:
                ReadOnlySpan <byte> rawString = reader.ReadRawString();
                if (!TryRead(rawString, out DateTime dateTime))
                {
                    throw reader.BuildException($"Invalid date format {Encoding.UTF8.GetString(rawString)}");
                }
                return(dateTime);

            case CborDataItemType.Signed:
            case CborDataItemType.Unsigned:
                long unixTime = reader.ReadInt64();
                return(DateTimeOffset.FromUnixTimeSeconds(unixTime).DateTime);

            case CborDataItemType.Double:
            case CborDataItemType.Single:
                double unixTimeDouble = reader.ReadDouble();
                return(DateTimeOffset.FromUnixTimeSeconds(0).DateTime.AddSeconds(unixTimeDouble));

            default:
                throw reader.BuildException($"Invalid date format");
            }
        }
Exemplo n.º 2
0
        public override T Read(ref CborReader reader)
        {
            switch (reader.GetCurrentDataItemType())
            {
            case CborDataItemType.Unsigned:
            case CborDataItemType.Signed:
                return(ReadInt32(ref reader));

            case CborDataItemType.String:
                return(ReadString(ref reader));

            default:
                throw new NotSupportedException();
            }
        }
Exemplo n.º 3
0
        public void Read(ref CborReader reader, object obj)
        {
            if (reader.GetCurrentDataItemType() == CborDataItemType.Null)
            {
                if (_requirementPolicy == RequirementPolicy.DisallowNull || _requirementPolicy == RequirementPolicy.Always)
                {
                    throw new CborException($"Property '{Encoding.UTF8.GetString(_memberName.Span)}' cannot be null.");
                }
            }

            if (_memberSetter == null)
            {
                throw new CborException($"No member setter for '{Encoding.UTF8.GetString(_memberName.Span)}'");
            }

            _memberSetter((T)obj, _converter.Read(ref reader));
        }
Exemplo n.º 4
0
        public void Read(ref CborReader reader, ref T instance)
        {
            if (reader.GetCurrentDataItemType() == CborDataItemType.Null)
            {
                if (_requirementPolicy == RequirementPolicy.DisallowNull || _requirementPolicy == RequirementPolicy.Always)
                {
                    throw new CborException($"Property '{MemberNameAsString}' cannot be null.");
                }
            }

            if (_memberSetter == null)
            {
                throw new CborException($"No member setter for '{MemberNameAsString}'");
            }

            _memberSetter(ref instance, _converter.Read(ref reader));
        }
        public override CborValue Read(ref CborReader reader)
        {
            switch (reader.GetCurrentDataItemType())
            {
            case CborDataItemType.Boolean:
                return(reader.ReadBoolean());

            case CborDataItemType.Null:
                reader.ReadNull();
                return(CborValue.Null);

            case CborDataItemType.Signed:
                return(reader.ReadInt64());

            case CborDataItemType.Unsigned:
                return(reader.ReadUInt64());

            case CborDataItemType.Single:
                return(reader.ReadSingle());

            case CborDataItemType.Double:
                return(reader.ReadDouble());

            case CborDataItemType.Decimal:
                return(reader.ReadDecimal());

            case CborDataItemType.String:
                return(reader.ReadString());

            case CborDataItemType.Array:
                return(((ICborConverter <CborArray>) this).Read(ref reader));

            case CborDataItemType.Map:
                return(((ICborConverter <CborObject>) this).Read(ref reader));

            case CborDataItemType.ByteString:
                return(reader.ReadByteString());

            default:
                throw reader.BuildException("Unexpected data item type");
            }
        }