예제 #1
0
        public static S2CRequest Deserialize(string content, SerialFormat format)
        {
            if (string.IsNullOrEmpty(content))
            {
                log.Error("Content is empty");
                return(new S2CRequest(format, -1, "Content is empty"));
            }

            switch (format)
            {
            case SerialFormat.JSON:
                return(jsonUtils.ToObject <S2CRequest>(content));

            case SerialFormat.XML:
                //parse content:
                XElement data = null;
                try
                {
                    data = XElement.Parse(content);
                }
                catch (Exception e)
                {
                    log.ErrorFormat("Cannot parse XML:{0} due to {1}", content, e.Message);
                    return(null);
                }

                if (data == null)
                {
                    log.ErrorFormat("Cannot parse XML:{0}", content);
                    return(null);
                }

                //parse single elements:
                long id = -1;
                data.ParseNode("ID", ref id, false);

                S2CContentType contType = S2CContentType.Obj;
                data.ParseNode <S2CContentType>("ContType", ref contType, false);

                //parse actual data depending on content type:
                XElement dataContent = data.GetNode("Data", false);
                switch (contType)
                {
                case S2CContentType.S2CEntity:
                    S2CRequest singleEntity = new S2CRequest(SerialFormat.XML, id, dataContent.ToString());
                    singleEntity.ContentType = S2CContentType.S2CEntity;
                    return(singleEntity);

                case S2CContentType.ListS2CEntity:
                    S2CRequest multEntity = new S2CRequest(SerialFormat.XML, id, dataContent.ToString());
                    multEntity.ContentType = S2CContentType.S2CEntity;
                    return(multEntity);

                case S2CContentType.Obj:
                    return(new S2CRequest(SerialFormat.XML, id, dataContent.Value));

                case S2CContentType.ListObj:
                    List <object>   objs  = new List <object>();
                    List <XElement> nodes = dataContent.GetNodes("Obj", false);
                    if (nodes != null)
                    {
                        foreach (XElement node in nodes)
                        {
                            objs.Add(node.Value);
                        }
                    }
                    return(new S2CRequest(SerialFormat.XML, id, objs));
                }

                log.Error("Unknown S2CContentType");
                return(new S2CRequest(format, -1, "Unknown S2CContentType"));

            case SerialFormat.HTML:
                return(new S2CRequest(format, -1, "Unsupported SerialFormat"));
                //TODO???
            }

            log.Error("Unknown SerialFormat");
            return(new S2CRequest(format, -1, "Unknown SerialFormat"));
        }
예제 #2
0
        public static S2CRequest Deserialize(string content, SerialFormat format)
        {
            if (string.IsNullOrEmpty(content))
            {
                log.Error("Content is empty");
                return new S2CRequest(format, -1, "Content is empty");
            }

            switch (format)
            {
                case SerialFormat.JSON:
                    return jsonUtils.ToObject<S2CRequest>(content);

                case SerialFormat.XML:
                    //parse content:
                    XElement data = null;
                    try
                    {
                        data = XElement.Parse(content);
                    }
                    catch (Exception e)
                    {
                        log.ErrorFormat("Cannot parse XML:{0} due to {1}", content, e.Message);
                        return null;
                    }

                    if (data == null)
                    {
                        log.ErrorFormat("Cannot parse XML:{0}", content);
                        return null;
                    }

                    //parse single elements:
                    long id = -1;
                    data.ParseNode("ID", ref id, false);

                    S2CContentType contType = S2CContentType.Obj;
                    data.ParseNode<S2CContentType>("ContType", ref contType, false);

                    //parse actual data depending on content type:
                    XElement dataContent = data.GetNode("Data", false);
                    switch (contType)
                    {
                        case S2CContentType.S2CEntity:
                            S2CRequest singleEntity =  new S2CRequest(SerialFormat.XML, id, dataContent.ToString());
                            singleEntity.ContentType = S2CContentType.S2CEntity;
                            return singleEntity;

                        case S2CContentType.ListS2CEntity:
                            S2CRequest multEntity = new S2CRequest(SerialFormat.XML, id, dataContent.ToString());
                            multEntity.ContentType = S2CContentType.S2CEntity;
                            return multEntity;

                        case S2CContentType.Obj:
                            return new S2CRequest(SerialFormat.XML, id, dataContent.Value);

                        case S2CContentType.ListObj:
                            List<object> objs = new List<object>();
                            List<XElement> nodes = dataContent.GetNodes("Obj", false);
                            if (nodes != null)
                            {
                                foreach (XElement node in nodes)
                                {
                                    objs.Add(node.Value);
                                }
                            }
                            return new S2CRequest(SerialFormat.XML, id, objs);
                    }

                    log.Error("Unknown S2CContentType");
                    return new S2CRequest(format, -1, "Unknown S2CContentType");

                case SerialFormat.HTML:
                    return new S2CRequest(format, -1, "Unsupported SerialFormat");
                    //TODO???
            }

            log.Error("Unknown SerialFormat");
            return new S2CRequest(format, -1, "Unknown SerialFormat");
        }