Example #1
0
        public void When_ByteArray_Encoded_Throw_UnsupportedException()
        {
            var transcoder = new JsonTranscoder(new DefaultSerializer());

            var bytes = new byte[0];

            Assert.Throws <UnsupportedException>(() => transcoder.Decode <byte[]>(bytes.AsMemory(),
                                                                                  new Flags {
                DataFormat = DataFormat.Binary
            },
                                                                                  OpCode.NoOp));
        }
Example #2
0
        public void DecodeString_Returns_String_When_Buffer_Is_Empty_And_Type_Is_String()
        {
            var transcoder = new JsonTranscoder(new DefaultSerializer());

            var bytes  = new byte[0];
            var result = transcoder.Decode <string>(bytes.AsMemory(),
                                                    new Flags {
                DataFormat = DataFormat.String
            },
                                                    OpCode.NoOp);

            Assert.Null(result);
        }
Example #3
0
        public void Test_Deserialize_Char()
        {
            var transcoder = new JsonTranscoder();
            var value      = 'o';

            var flags = new Flags
            {
                Compression = Couchbase.Core.IO.Operations.Compression.None,
                DataFormat  = DataFormat.Binary,
                TypeCode    = Convert.GetTypeCode(value)
            };

            var expected = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value));
            var actual   = transcoder.Decode <char>(expected.AsMemory(), flags, OpCode.Get);

            Assert.Equal(value, actual);
        }
Example #4
0
        public void Test_Deserialize_Int()
        {
            var transcoder = new JsonTranscoder();
            var five       = 5;

            var flags = new Flags
            {
                Compression = Couchbase.Core.IO.Operations.Compression.None,
                DataFormat  = DataFormat.Json,
                TypeCode    = Convert.GetTypeCode(five)
            };

            using var stream = new MemoryStream();
            transcoder.Encode(stream, five, flags, OpCode.Get);
            var actual = transcoder.Decode <int>(stream.ToArray(), flags, OpCode.Get);

            Assert.Equal(five, actual);
        }
Example #5
0
        public void Test_Deserialize_String()
        {
            var transcoder = new JsonTranscoder();
            // ReSharper disable once StringLiteralTypo
            var value = "astring";

            var flags = new Flags
            {
                Compression = Couchbase.Core.IO.Operations.Compression.None,
                DataFormat  = DataFormat.Json,
                TypeCode    = Convert.GetTypeCode(value)
            };

            using var stream = new MemoryStream();
            transcoder.Encode(stream, value, flags, OpCode.Get);
            var actual = transcoder.Decode <string>(stream.ToArray(), flags, OpCode.Get);

            Assert.Equal(value, actual);
        }
Example #6
0
        public void Test_Deserialize_Null()
        {
            var    transcoder = new JsonTranscoder();
            object value      = null;

            var flags = new Flags
            {
                Compression = Couchbase.Core.IO.Operations.Compression.None,
                DataFormat  = DataFormat.Json,
                TypeCode    = TypeCode.Empty
            };

            using var stream = new MemoryStream();
            // ReSharper disable once ExpressionIsAlwaysNull
            transcoder.SerializeAsJson(stream, value);
            var actual = transcoder.Decode <object>(stream.ToArray(), flags, OpCode.Get);

            Assert.Equal(value, actual);
        }
Example #7
0
        // ReSharper disable once IdentifierTypo
        public void Test_Poco()
        {
            var transcoder = new JsonTranscoder();
            var value      = new Person {
                Name = "jeff"
            };

            var flags = new Flags
            {
                Compression = Couchbase.Core.IO.Operations.Compression.None,
                DataFormat  = DataFormat.Json,
                TypeCode    = Type.GetTypeCode(typeof(Person))
            };

            using var stream = new MemoryStream();
            transcoder.Encode(stream, value, flags, OpCode.Get);
            var actual = transcoder.Decode <Person>(stream.ToArray(), flags, OpCode.Get);

            Assert.Equal(value.Name, actual.Name);
        }