コード例 #1
0
        public void Serialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
        {
            var innerType  = fromType.GetGenericArguments()[0];
            var innerValue = fromType.GetProperty("Value").GetValue(fromValue);

            context.Serialize(path, innerValue, innerType, options);
        }
コード例 #2
0
        public bool IsIgnore(MapPath path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            return(this._ignores.Any(p => p.Equals(path)));
        }
コード例 #3
0
        public IMapSerializeOptions Map(MapPath from, params MapPath[] to)
        {
            if (from == null)
            {
                throw new ArgumentNullException(nameof(from));
            }

            return(this.Map(from, to.AsEnumerable()));
        }
コード例 #4
0
        public void Serialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options)
        {
            //! ignore path
            if (options.IsIgnore(path))
            {
                return;
            }

            this._serializer.Serialize(path, fromValue, fromType, options, this);
        }
コード例 #5
0
        public IMapSerializeOptions Ignore(MapPath path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            this._ignores.Add(path);
            return(this);
        }
コード例 #6
0
        public object Deserialize(MapPath path, Type toType, IMapDeserializeOptions options)
        {
            //! ignore path;
            if (options.IsIgnore(path))
            {
                return(null);
            }

            return(this._deserializer.Deserialize(path, toType, options, this));
        }
コード例 #7
0
        public bool CanDeserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
        {
            var token = context.Table.GetToken(path);

            if (!(token is MapNullToken))
            {
                return(false);
            }

            return((!toType.IsValueType) || (toType.IsValueType && toType.IsNullable()));
        }
コード例 #8
0
        public object Deserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
        {
            var converter = this._converters.FirstOrDefault(x => x.CanDeserialize(path, toType, options, context));

            if (converter == null)
            {
                throw new InvalidOperationException($"无法找到转换器。[node: {path}][type: {toType}]");
            }

            return(converter.Deserialize(path, toType, options, context));
        }
コード例 #9
0
        public bool CanDeserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
        {
            var token = context.Table.GetToken(path);

            if (!(token is MapEnumToken))
            {
                return(false);
            }

            return(token.Compatible(toType));
        }
コード例 #10
0
        public bool CanDeserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
        {
            var token = context.Table.GetToken(path);

            if (!(token is MapArrayToken))
            {
                return(false);
            }

            return(ArrayUtil.Check(toType));
        }
コード例 #11
0
        public bool CanSerialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
        {
            if (fromType.Namespace.StartsWith("System"))
            {
                return(false);
            }

            if (fromType.IsEnum)
            {
                return(false);
            }

            return(true);
        }
コード例 #12
0
        public object Deserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
        {
            var obj      = Activator.CreateInstance(toType);
            var children = context.Table.GetChildren(path).ToList();

            foreach (var child in children)
            {
                var name  = child.Key.LastSegment();
                var prop  = toType.GetProperty(name);
                var type  = prop.PropertyType;
                var value = context.Deserialize(child.Key, type, options);
                prop.SetValue(obj, value);
            }

            return(obj);
        }
コード例 #13
0
        public object Deserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
        {
            var children = context.Table.GetChildren(path);
            var elemType = ArrayUtil.GetElementType(toType);
            var arr      = Array.CreateInstance(elemType, children.Count());

            foreach (var child in children)
            {
                var value = context.Deserialize(child.Key, elemType, options);
                ArrayUtil.SetValue(arr, int.Parse(child.Key.LastSegment()), value);
            }

            if (ArrayUtil.IsArray(toType))
            {
                return(arr);
            }

            return(ArrayUtil.ToList(elemType, arr));
        }
コード例 #14
0
        public void Serialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
        {
            context.Table.AddToken(path, new MapObjectToken());
            var props = fromType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var prop in props)
            {
                var name    = prop.Name;
                var type    = prop.PropertyType;
                var subPath = path.Append(name);

                var value = prop.GetValue(fromValue);
                if (value == null)
                {
                    continue;
                }

                context.Serialize(subPath, value, type, options);
            }
        }
コード例 #15
0
        public void Serialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
        {
            var arr      = fromValue;
            var elemType = ArrayUtil.GetElementType(fromType);

            context.Table.AddToken(path, new MapArrayToken(fromType, elemType));

            if (ArrayUtil.IsEnumerable(fromType))
            {
                arr = ArrayUtil.ToArray(elemType, arr);
            }

            var count = ArrayUtil.GetCount(arr);

            for (var i = 0; i < count; ++i)
            {
                var value = ArrayUtil.GetValue(arr, i);
                context.Serialize(path.Append($"{i}"), value, elemType, options);
            }
        }
コード例 #16
0
        public void Serialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
        {
            if (fromType == null)
            {
                throw new ArgumentNullException(nameof(fromType));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var converter = this._converters.FirstOrDefault(x => x.CanSerialize(path, fromValue, fromType, options, context));

            if (converter == null)
            {
                throw new InvalidOperationException($"无法找到转换器。[value: {fromValue}][type: {fromType}]");
            }

            converter.Serialize(path, fromValue, fromType, options, context);
        }
コード例 #17
0
        public IMapSerializeOptions Map(MapPath from, IEnumerable <MapPath> to)
        {
            if (from == null)
            {
                throw new ArgumentNullException(nameof(from));
            }

            if (to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }

            if (this._maps.ContainsKey(from))
            {
                this._maps[from].AddRange(to);
            }
            else
            {
                this._maps.Add(from, new List <MapPath>(to));
            }

            return(this);
        }
コード例 #18
0
 public void Serialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
 {
     context.Table.AddToken(path, new MapEnumToken(fromValue, fromType));
 }
コード例 #19
0
 public bool CanSerialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
 {
     return(fromType.IsEnum);
 }
コード例 #20
0
 public object Deserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
 {
     return(context.Table.GetToken(path).GetValue(toType));
 }
コード例 #21
0
 public bool CanDeserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
 {
     return(toType.IsNullable());
 }
コード例 #22
0
        public object Deserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
        {
            var innerType = toType.GetGenericArguments()[0];

            return(context.Deserialize(path, innerType, options));
        }
コード例 #23
0
 public bool CanSerialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
 {
     return(TYPES.Any(x => x.Equals(fromType)));
 }
コード例 #24
0
        public void Serialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
        {
            if (fromType.Equals <Single>())
            {
                context.Table.AddToken(path, new MapSingleToken((Single)fromValue));
                return;
            }

            if (fromType.Equals <Double>())
            {
                context.Table.AddToken(path, new MapDoubleToken((Double)fromValue));
                return;
            }

            if (fromType.Equals <Decimal>())
            {
                context.Table.AddToken(path, new MapDecimalToken((Decimal)fromValue));
                return;
            }

            if (fromType.Equals <SByte>())
            {
                context.Table.AddToken(path, new MapInt8Token((SByte)fromValue));
                return;
            }

            if (fromType.Equals <Int16>())
            {
                context.Table.AddToken(path, new MapInt16Token((Int16)fromValue));
                return;
            }

            if (fromType.Equals <Int32>())
            {
                context.Table.AddToken(path, new MapInt32Token((Int32)fromValue));
                return;
            }

            if (fromType.Equals <Int64>())
            {
                context.Table.AddToken(path, new MapInt64Token((Int64)fromValue));
                return;
            }

            if (fromType.Equals <Byte>())
            {
                context.Table.AddToken(path, new MapUInt8Token((Byte)fromValue));
                return;
            }

            if (fromType.Equals <UInt16>())
            {
                context.Table.AddToken(path, new MapUInt16Token((UInt16)fromValue));
                return;
            }

            if (fromType.Equals <UInt32>())
            {
                context.Table.AddToken(path, new MapUInt32Token((UInt32)fromValue));
                return;
            }

            if (fromType.Equals <UInt64>())
            {
                context.Table.AddToken(path, new MapUInt64Token((UInt64)fromValue));
                return;
            }

            throw new NotImplementedException();
        }
コード例 #25
0
 public static IMapperBuilder Ignore <T>(this IMapperBuilder builder, Expression <Func <T, object> > field)
 {
     return(builder.Ignore(typeof(T), MapPath.Parse(field)));
 }
コード例 #26
0
 public void Serialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
 {
     context.Table.AddToken(path, new MapTimeSpanToken((TimeSpan)fromValue));
 }
コード例 #27
0
 public bool CanSerialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
 {
     return(fromType.Equals <TimeSpan>());
 }
コード例 #28
0
 public bool CanSerialize(MapPath path, object fromValue, Type fromType, IMapSerializeOptions options, IMapSerializeContext context)
 {
     return(ArrayUtil.Check(fromType));
 }
コード例 #29
0
 public object Deserialize(MapPath path, Type toType, IMapDeserializeOptions options, IMapDeserializeContext context)
 {
     return(null);
 }