コード例 #1
0
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                ReadOnlyCollection <CosmosRequestHandler> handlers = value as ReadOnlyCollection <CosmosRequestHandler>;

                if (handlers != null)
                {
                    writer.WriteValue(string.Join(":", handlers.Select(x => x.GetType())));
                    return;
                }

                CosmosJsonSerializerWrapper cosmosJsonSerializerWrapper = value as CosmosJsonSerializerWrapper;

                if (value is CosmosJsonSerializerWrapper)
                {
                    writer.WriteValue(cosmosJsonSerializerWrapper.InternalJsonSerializer.GetType().ToString());
                }
            }
        public void GetCosmosSerializerWithWrapperOrDefaultTest()
        {
            CosmosJsonDotNetSerializer serializer = new CosmosJsonDotNetSerializer();
            CosmosClientOptions        options    = new CosmosClientOptions()
            {
                Serializer = serializer
            };

            CosmosSerializer cosmosSerializer = options.GetCosmosSerializerWithWrapperOrDefault();

            Assert.AreNotEqual(cosmosSerializer, options.PropertiesSerializer, "Serializer should be custom not the default");
            Assert.AreNotEqual(cosmosSerializer, serializer, "Serializer should be in the CosmosJsonSerializerWrapper");

            CosmosJsonSerializerWrapper cosmosJsonSerializerWrapper = cosmosSerializer as CosmosJsonSerializerWrapper;

            Assert.IsNotNull(cosmosJsonSerializerWrapper);
            Assert.AreEqual(cosmosJsonSerializerWrapper.InternalJsonSerializer, serializer);
        }
        public void GetCosmosSerializerWithWrapperOrDefaultWithOptionsTest()
        {
            CosmosSerializationOptions serializerOptions = new CosmosSerializationOptions();

            Assert.IsFalse(serializerOptions.IgnoreNullValues);
            Assert.IsFalse(serializerOptions.Indented);
            Assert.AreEqual(CosmosPropertyNamingPolicy.Default, serializerOptions.PropertyNamingPolicy);

            CosmosClientOptions options = new CosmosClientOptions()
            {
                SerializerOptions = new CosmosSerializationOptions()
                {
                    IgnoreNullValues     = true,
                    Indented             = true,
                    PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
                }
            };

            CosmosSerializer cosmosSerializer = options.GetCosmosSerializerWithWrapperOrDefault();

            Assert.AreNotEqual(cosmosSerializer, options.PropertiesSerializer, "Serializer should be custom not the default");

            CosmosJsonSerializerWrapper cosmosJsonSerializerWrapper = cosmosSerializer as CosmosJsonSerializerWrapper;

            Assert.IsNotNull(cosmosJsonSerializerWrapper);

            // Verify the custom settings are being honored
            dynamic testItem = new { id = "testid", description = (string)null, CamelCaseProperty = "TestCamelCase" };

            using (Stream stream = cosmosSerializer.ToStream <dynamic>(testItem))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    string jsonString = sr.ReadToEnd();
                    // Notice description is not included, camelCaseProperty starts lower case, the white space shows the indents
                    string expectedJsonString = $"{{{Environment.NewLine}  \"id\": \"testid\",{Environment.NewLine}  \"camelCaseProperty\": \"TestCamelCase\"{Environment.NewLine}}}";
                    Assert.AreEqual(expectedJsonString, jsonString);
                }
            }
        }
コード例 #4
0
 public void ToStream_Throws()
 {
     var handler = new CosmosJsonSerializerWrapper(new CosmosJsonSerializerFails());
     var retval  = handler.ToStream("testValue");
 }
コード例 #5
0
 public void FromStream_Throws()
 {
     var handler = new CosmosJsonSerializerWrapper(new CosmosJsonSerializerFails());
     var retval  = handler.FromStream <string>(new MemoryStream());
 }