Exemple #1
0
 public static void WriteBoolean_SingleValue_HappyPath(bool input, string hexExpectedEncoding)
 {
     byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
     using var writer = new CborWriter();
     writer.WriteBoolean(input);
     AssertHelper.HexEqual(expectedEncoding, writer.GetEncoding());
 }
Exemple #2
0
 public static void WriteObject(this CborWriter writer, CborObject @object)
 {
     if (@object is CborTextString text)
     {
         writer.WriteTextString(text.Value);
     }
     else if (@object is CborByteString data)
     {
         writer.WriteByteString(data.Value);
     }
     else if (@object is CborBoolean boolean)
     {
         writer.WriteBoolean(boolean.Value);
     }
     else if (@object is CborInteger number)
     {
         writer.WriteInt64(number.Value);
     }
     else if (@object is CborMap map)
     {
         writer.WriteMap(map);
     }
     else if (@object is CborArray array)
     {
         writer.WriteArray(array);
     }
     else if (@object.Type == CborType.Null)
     {
         writer.WriteNull();
     }
     else
     {
         throw new Exception($"Unknown type. Was  {@object.Type}");
     }
 }
Exemple #3
0
            public static void WriteValue(CborWriter writer, object value, bool useDefiniteLengthCollections = true)
            {
                switch (value)
                {
                case null: writer.WriteNull(); break;

                case bool b: writer.WriteBoolean(b); break;

                case int i: writer.WriteInt32(i); break;

                case long i: writer.WriteInt64(i); break;

                case ulong i: writer.WriteUInt64(i); break;

                case float f: writer.WriteSingle(f); break;

                case double d: writer.WriteDouble(d); break;

                case decimal d: writer.WriteDecimal(d); break;

                case string s: writer.WriteTextString(s); break;

                case BigInteger i: writer.WriteBigInteger(i); break;

                case DateTimeOffset d: writer.WriteDateTimeOffset(d); break;

                case byte[] b: writer.WriteByteString(b); break;

                case byte[][] chunks: WriteChunkedByteString(writer, chunks); break;

                case string[] chunks when IsIndefiniteLengthByteString(chunks):
                    byte[][]  byteChunks = chunks.Skip(1).Select(ch => ch.HexToByteArray()).ToArray();

                    WriteChunkedByteString(writer, byteChunks);
                    break;

                case string[] chunks: WriteChunkedTextString(writer, chunks); break;

                case object[] nested when IsCborMapRepresentation(nested): WriteMap(writer, nested, useDefiniteLengthCollections); break;

                case object[] nested when IsEncodedValueRepresentation(nested):
                    byte[]    encodedValue = ((string)nested[1]).HexToByteArray();

                    writer.WriteEncodedValue(encodedValue);
                    break;

                case object[] nested when IsTaggedValueRepresentation(nested):
                    writer.WriteTag((CborTag)nested[0]);

                    WriteValue(writer, nested[1]);
                    break;

                case object[] nested: WriteArray(writer, nested, useDefiniteLengthCollections); break;

                default: throw new ArgumentException($"Unrecognized argument type {value.GetType()}");
                }
                ;
            }
        public override void Write(ref CborWriter writer, CborValue value, LengthMode lengthMode)
        {
            switch (value.Type)
            {
            case CborValueType.Object:
                ((ICborConverter <CborObject>) this).Write(ref writer, (CborObject)value, lengthMode);
                break;

            case CborValueType.Array:
                ((ICborConverter <CborArray>) this).Write(ref writer, (CborArray)value, lengthMode);
                break;

            case CborValueType.Positive:
                writer.WriteUInt64(value.Value <ulong>());
                break;

            case CborValueType.Negative:
                writer.WriteInt64(value.Value <long>());
                break;

            case CborValueType.Single:
                writer.WriteSingle(value.Value <float>());
                break;

            case CborValueType.Double:
                writer.WriteDouble(value.Value <double>());
                break;

            case CborValueType.Decimal:
                writer.WriteDecimal(value.Value <decimal>());
                break;

            case CborValueType.String:
                writer.WriteString(value.Value <string>());
                break;

            case CborValueType.Boolean:
                writer.WriteBoolean(value.Value <bool>());
                break;

            case CborValueType.Null:
                writer.WriteNull();
                break;

            case CborValueType.ByteString:
                writer.WriteByteString(value.Value <ReadOnlyMemory <byte> >().Span);
                break;
            }
        }
 public override void Write(ref CborWriter writer, bool value)
 {
     writer.WriteBoolean(value);
 }