Beispiel #1
0
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Undefined)
            {
                // undefined 表示空的 Maybe<T> 实例。直接使用 Activator 实例化即可。
                existingValue = Activator.CreateInstance(objectType);
                return(existingValue);
            }

            var valueType = MaybeUtils.GetInnerType(objectType);
            var value     = serializer.Deserialize(reader, valueType);

            if (existingValue != null && MaybeUtils.IsMaybe(existingValue))
            {
                MaybeUtils.SetInnerValue(existingValue, value);
            }
            else
            {
                existingValue = MaybeUtils.Create(valueType, value);
            }

            return(existingValue);
        }
Beispiel #2
0
        public void TestCreate()
        {
            var maybe = (Maybe <int>)MaybeUtils.Create(10);

            Assert.AreEqual(10, maybe.Value);
        }
Beispiel #3
0
        public void TestCreateNullValue()
        {
            var maybe = (Maybe <object>)MaybeUtils.Create(typeof(object), null);

            Assert.IsNull(maybe.Value);
        }
Beispiel #4
0
 public void TestCreateNullValueWithoutType()
 {
     Assert.Throws <ArgumentNullException>(() => MaybeUtils.Create(null));
 }
Beispiel #5
0
 public void TestCreateNullType()
 {
     Assert.Throws <ArgumentNullException>(() => MaybeUtils.Create(null, new object()));
 }