示例#1
0
        public RawPayload(TextPayload textPayload, string charset = "utf-8")
        {
            var encoding = Encoding.GetEncoding(charset);

            TypeHint    = textPayload.TypeHint;
            ContentType =
                textPayload.ContentType == null
                    ? null
                    : new ContentType(textPayload.ContentType)
            {
                CharSet = encoding.WebName
            }.ToString();
            Body = encoding.GetBytes(textPayload.Body);
        }
示例#2
0
        public object FromText <T>(TextPayload payload, IReadOnlyCollection <ITypeSchema> knownTypes)
        {
            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }
            var serializer = GetTextSerializer(payload.ContentType);
            var typeSchema = knownTypes.FirstOrDefault(p => p.ContractName == payload.TypeHint)
                             ?? knownTypes.FirstOrDefault(p => p.SchemaName == payload.TypeHint);
            var type = typeSchema?.DotNetType ?? typeof(T);

            if (type == typeof(T))
            {
                return(serializer.Deserialize <T>(payload.Body, typeSchema));
            }
            var gMethod = typeof(ITextPayloadSerializer).GetMethod(nameof(ITextPayloadSerializer.Deserialize));
            var method  = gMethod.MakeGenericMethod(type);

            return(method.Invoke(serializer, new object[] { payload.Body, typeSchema }));
        }