示例#1
0
        public static void Serialize <T>(Stream stream, T obj)
        {
            Debug.Assert(_state != null, "State can't be null");
            var description = _state.GetDescription(RuntimeHelpers.GetHashCode(typeof(T)));
            var writer      = FormatterFactory <Writer> .GetFormatter(stream, Encoding);

            writer.Write(description.TypeId);
            ((SpecializedTypeDescription <T>)description).SerializeHanlder(obj, writer);
            FormatterFactory <Writer> .ReleaseFormatter(writer);
        }
示例#2
0
        public static void Serialize(Stream stream, object obj)
        {
            Debug.Assert(_state != null, "State can't be null");
            var description = _state.GetDescription(obj.GetType());
            var writer      = FormatterFactory <Writer> .GetFormatter(stream, Encoding);

            writer.Write(description.TypeId);
            description.Serialize(writer, obj);
            FormatterFactory <Writer> .ReleaseFormatter(writer);
        }
        public async Task <string> Handle(CalcFrequencyCommand request, CancellationToken cancellationToken)
        {
            var posts = await _postService.GetLastPostInfoAsync(request.UserName, COUNT_POST);

            var resultFrequency = new LetterFrequency().Calc(posts.Select(p => p.Text));
            var result          = FormatterFactory.GetFormatter().PrepareString(resultFrequency.allLetter, resultFrequency.letterCounter);

            var statisticMessage = $"{request.UserName}, статистика для последних {COUNT_POST} постов: {result}";
            await _postService.CreatePostAsync(statisticMessage, request.UserName);

            return(result);
        }
示例#4
0
        public static object Deserialize(Stream stream)
        {
            var reader = FormatterFactory <Reader> .GetFormatter(stream, Encoding);

            var state  = _state;
            var typeId = reader.ReadShort();

            if (typeId >= state.DescriptionsById.Length)
            {
                state.UnknownTypeId(typeId);
            }
            var description = state.DescriptionsById[typeId];
            var obj         = description.Deserialize(reader);

            FormatterFactory <Reader> .ReleaseFormatter(reader);

            return(obj);
        }
示例#5
0
        private void CheckFormat(PropertyInfo field, FieldExtractor fieldExtractor)
        {
            //check custom formatter
            Attribute.Formatter formatter = field.GetCustomAttribute <Attribute.Formatter>();
            Type formatterType;

            if (formatter?.ObjectFormatter != null)
            {
                //  if (!formatter.formatter().equals(ObjectFormatter.class)) {
                IObjectFormatter objectFormatter = formatter.ObjectFormatter;
                objectFormatter.InitParam(formatter.Value);
                fieldExtractor.ObjectFormatter = objectFormatter;
                return;
                //   }
            }
            else
            {
                formatterType = FormatterFactory.GetFormatter(field.PropertyType);
            }

            if (!fieldExtractor.Multi)
            {
                IObjectFormatter objectFormatter = GetObjectFormatter(formatterType);
                if (objectFormatter == null)
                {
                    throw new Exception("Can't find formatter for field " + field.Name + " of type " + formatterType);
                }
                fieldExtractor.ObjectFormatter = objectFormatter;
            }
            else
            {
                if (!field.PropertyType.IsGenericType)
                {
                    throw new SpiderExceptoin("Field " + field.Name + " must be generice type.");
                }

                Type[] genericType = field.PropertyType.GetGenericArguments();
                if (genericType.Length != 1)
                {
                    throw new SpiderExceptoin("Field " + field.Name + " must be single generice type like List<T> Hashset<T>. Not support Diction<K,V> etc...");
                }

                MethodInfo method = fieldExtractor.Field.PropertyType.GetMethod("Add", genericType);
                if (method == null)
                {
                    throw new SpiderExceptoin("Field " + field.Name + " did not contains Add(T t) method.");
                }

                IObjectFormatter objectFormatter = GetObjectFormatter(formatterType);
                if (objectFormatter == null)
                {
                    if (formatter != null)
                    {
                        throw new SpiderExceptoin("Can't find formatter for field " + field.Name + " of type " + formatter.SubType);
                    }
                }
                else
                {
                    fieldExtractor.ObjectFormatter = objectFormatter;
                }
            }
        }
 protected virtual ISessionDataFormatter GetFormatter()
 {
     return(FormatterFactory.GetFormatter(Configuration));
 }