示例#1
0
        public void Should_Convert_To_PascalCase_Json_With_Altered_Serialization_Settings()
        {
            var transcoder = new JsonTranscoder(
                new DefaultSerializer(
                    new JsonSerializerSettings(),
                    new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver()
            }));

            var data = new Transcoders.Pascal
            {
                SomeProperty    = "SOME",
                SomeIntProperty = 12345,
                HasPascalCase   = true
            };
            var expectedJsonBytes = Encoding.UTF8.GetBytes("{\"SomeProperty\":\"SOME\",\"SomeIntProperty\":12345,\"HasPascalCase\":true}");

            using var jsonBytes   = new MemoryStream();
            using var jsonEncoded = new MemoryStream();
            transcoder.SerializeAsJson(jsonBytes, data);
            transcoder.Encode(jsonEncoded, data, new Flags
            {
                DataFormat = DataFormat.Json
            }, OpCode.Get);

            Assert.Equal(expectedJsonBytes, jsonBytes.ToArray());
            Assert.Equal(expectedJsonBytes, jsonEncoded.ToArray());
        }
示例#2
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);
        }
示例#3
0
        public void Test_Null()
        {
            var transcoder = new JsonTranscoder();

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

            var expected = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(null));

            using var stream = new MemoryStream();
            transcoder.Encode <string>(stream, null, flags, OpCode.Get);

            Assert.Equal(expected, stream.ToArray());
        }
示例#4
0
        public void Test_Serialize_Int16()
        {
            var   transcoder = new JsonTranscoder();
            Int16 data       = 5;

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

            var expected = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));

            using var stream = new MemoryStream();
            transcoder.Encode(stream, data, flags, OpCode.Get);

            Assert.Equal(expected, stream.ToArray());
        }
示例#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);
        }
示例#6
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);
        }
示例#7
0
        public void Test_Char()
        {
            var transcoder = new JsonTranscoder();
            var value      = 'o';

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

            var json     = JsonConvert.SerializeObject(value);
            var expected = Encoding.UTF8.GetBytes(json);

            using var stream = new MemoryStream();
            transcoder.Encode(stream, value, flags, OpCode.Get);

            Assert.Equal(expected, stream.ToArray());
        }