示例#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
        // ReSharper disable once IdentifierTypo
        public void Should_Hydrate_Poco_In_PascalCase_Whatever_The_Case_In_Json()
        {
            var jsonData   = Encoding.UTF8.GetBytes("{ \"SomeProperty\": \"SOME\", \"someIntProperty\": 12345, \"haspAscalCASE\": true }");
            var transcoder = new JsonTranscoder();
            var hydrated   = transcoder.DeserializeAsJson <Pascal>(jsonData.AsMemory());

            Assert.Equal("SOME", hydrated.SomeProperty);
            Assert.Equal(12345, hydrated.SomeIntProperty);
            Assert.True(hydrated.HasPascalCase);
        }
示例#3
0
        public void Test_Json_Deserialize_Int()
        {
            var transcoder = new JsonTranscoder();
            int value      = 42;

            using var stream = new MemoryStream();
            transcoder.SerializeAsJson(stream, value);
            var actual = transcoder.DeserializeAsJson <int>(stream.ToArray());

            Assert.Equal(value, actual);
        }
示例#4
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));
        }
示例#5
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);
        }
        public async Task CustomEncode()
        {
            // #tag::custom-encode[]
            var serializer = new DotnetJsonSerializer();
            var transcoder = new JsonTranscoder(serializer);

            var user = new User
            {
                Name = "John Smith",
                Age  = 27
            };

            await _collection.UpsertAsync("john-smith", user, options => options.Transcoder(transcoder));

            // #end::custom-encode[]
        }
        public async Task CustomDecode()
        {
            // #tag::custom-decode[]
            var serializer = new DotnetJsonSerializer();
            var transcoder = new JsonTranscoder(serializer);

            var user = new User
            {
                Name = "John Smith",
                Age  = 27
            };

            var result = await _collection.GetAsync("john-smith", options => options.Transcoder(transcoder));

            var returnedUser = result.ContentAs <User>();
            // #end::custom-decode[]
        }
示例#8
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);
        }
示例#9
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);
        }
示例#10
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());
        }
示例#11
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());
        }
示例#12
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);
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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());
        }