Exemple #1
0
        public override T Deserialize <T>(string input)
        {
            // NOTE: important exception situation!!
            // if the requested type is IComponentPresentation, there is a possiblity that the data
            // provided to us actually contains a Component instead. In that case we need to add a
            // dummy CT / CP around the Component and return that!


            if (((SerializationProperties)SerializationProperties).CompressionEnabled)
            {
                input = Compressor.Decompress(input);
            }

            TextReader tr = new StringReader(input);

            XmlSerializer serializer = null;

            if (typeof(T) == typeof(Page) || typeof(T) == typeof(IPage))
            {
                serializer = GetXmlSerializer <PageSerializer>();
            }
            else if (typeof(T) == typeof(Component) || typeof(T) == typeof(IComponent))
            {
                serializer = GetXmlSerializer <ComponentSerializer>();
            }
            else if (typeof(T) == typeof(ComponentPresentation) || typeof(T) == typeof(IComponentPresentation))
            {
                if (typeof(T).Name.Contains("ComponentPresentation") &&
                    !input.Substring(0, 30).ToLower().Contains("componentpresentation"))
                {
                    // handle the exception situation where we are asked to deserialize into a CP but the data is actually a Component
                    serializer = GetXmlSerializer <ComponentSerializer>();
                    Component component = (Component)serializer.Deserialize(tr);
                    IComponentPresentation componentPresentation = new ComponentPresentation()
                    {
                        Component         = component,
                        ComponentTemplate = new ComponentTemplate()
                    };
                    return((T)componentPresentation);
                }
                serializer = GetXmlSerializer <ComponentPresentationSerializer>();
            }
            return((T)serializer.Deserialize(tr));
        }
Exemple #2
0
        public static ISerializerService FindSerializerServiceForContent(string content)
        {
            // first trim leading and trailing whitespace
            string contentToCheck = content.Trim();
            bool   isCompressed   = false;

            foreach (Regex re in serializersByPattern.Keys)
            {
                if (re.IsMatch(contentToCheck))
                {
                    return(Initialize(serializersByPattern[re], isCompressed));
                }
            }
            // content is probably compressed, try uncompressing it now
            // first, try to decompress (this will fail if the content is uncompressed, of course)

            try
            {
                content      = Compressor.Decompress(content);
                isCompressed = true;
            }
            catch
            {
                // content is apparently not compressed, and it does not match any of the
                // known start strings, so we will throw this exception
                throw;
            }

            contentToCheck = content.Trim();
            foreach (Regex re in serializersByPattern.Keys)
            {
                if (re.IsMatch(contentToCheck))
                {
                    return(Initialize(serializersByPattern[re], isCompressed));
                }
            }

            return(Initialize(defaultSerializerServerType, isCompressed));
        }
Exemple #3
0
        public override string Serialize <T>(T input)
        {
            string result;

            if (input is Page || input is IPage)
            {
                result = Serialize(input, GetXmlSerializer <PageSerializer>());
            }
            else if (input is Component || input is IComponent)
            {
                result = Serialize(input, GetXmlSerializer <ComponentSerializer>());
            }
            else if (input is ComponentPresentation || input is IComponentPresentation)
            {
                result = Serialize(input, GetXmlSerializer <ComponentPresentationSerializer>());
            }
            else
            {
                throw new Exception("cannot serialize object of type " + typeof(T));
            }
            return(((SerializationProperties)SerializationProperties).CompressionEnabled ? Compressor.Compress(result) : result);
        }
        public override string Serialize <T>(T input)
        {
            string result = string.Empty;

            using (StringWriter stringWriter = new StringWriter())
            {
                JsonWriter jsonWriter = new JsonTextWriter(stringWriter);
                Serializer.Serialize(jsonWriter, input);
                result = stringWriter.ToString();
                result = ((SerializationProperties)SerializationProperties).CompressionEnabled ? Compressor.Compress(result) : result;
            }
            return(result);
        }