Esempio n. 1
0
        public static S2CRes Deserialize(string content, SerialFormat format)
        {
            if (string.IsNullOrEmpty(content))
            {
                log.Error("Content is empty");
                return new S2CRes(format, -1.0, ErrorCodes.FAIL, content);
            }

            switch (format)
            {
                case SerialFormat.JSON:
                    return jsonUtils.ToObject<S2CRes>(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:
                    double execTime = -1;
                    data.ParseNode("ExecTime", ref execTime, false);

                    ErrorCodes status = ErrorCodes.FAIL;
                    data.ParseNode<ErrorCodes>("Status", ref status, 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:
                            S2CRes singleEntity =  new S2CRes(SerialFormat.XML, execTime, status, dataContent.ToString());
                            singleEntity.ContentType = S2CContentType.S2CEntity;
                            return singleEntity;

                        case S2CContentType.ListS2CEntity:
                            S2CRes multEntity = new S2CRes(SerialFormat.XML, execTime, status, dataContent.ToString());
                            multEntity.ContentType = S2CContentType.S2CEntity;
                            return multEntity;

                        case S2CContentType.Obj:
                            return new S2CRes(SerialFormat.XML, execTime, status, 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 S2CRes(SerialFormat.XML, execTime, status, objs);
                    }

                    log.Error("Unknown S2CContentType");
                    return new S2CRes(format, -1.0, ErrorCodes.FAIL, "Unknown S2CContentType");

                case SerialFormat.HTML:
                    return new S2CRes(format, -1.0, ErrorCodes.FAIL, "Unsupported SerialFormat");
                    //TODO???
            }

            log.Error("Unknown SerialFormat");
            return new S2CRes(format, -1.0, ErrorCodes.FAIL, "Unknown SerialFormat");
        }
Esempio n. 2
0
        public static S2CRes Deserialize(string content, SerialFormat format)
        {
            if (string.IsNullOrEmpty(content))
            {
                log.Error("Content is empty");
                return(new S2CRes(format, -1.0, ErrorCodes.FAIL, content));
            }

            switch (format)
            {
            case SerialFormat.JSON:
                return(jsonUtils.ToObject <S2CRes>(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:
                double execTime = -1;
                data.ParseNode("ExecTime", ref execTime, false);

                ErrorCodes status = ErrorCodes.FAIL;
                data.ParseNode <ErrorCodes>("Status", ref status, 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:
                    S2CRes singleEntity = new S2CRes(SerialFormat.XML, execTime, status, dataContent.ToString());
                    singleEntity.ContentType = S2CContentType.S2CEntity;
                    return(singleEntity);

                case S2CContentType.ListS2CEntity:
                    S2CRes multEntity = new S2CRes(SerialFormat.XML, execTime, status, dataContent.ToString());
                    multEntity.ContentType = S2CContentType.S2CEntity;
                    return(multEntity);

                case S2CContentType.Obj:
                    return(new S2CRes(SerialFormat.XML, execTime, status, 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 S2CRes(SerialFormat.XML, execTime, status, objs));
                }

                log.Error("Unknown S2CContentType");
                return(new S2CRes(format, -1.0, ErrorCodes.FAIL, "Unknown S2CContentType"));

            case SerialFormat.HTML:
                return(new S2CRes(format, -1.0, ErrorCodes.FAIL, "Unsupported SerialFormat"));
                //TODO???
            }

            log.Error("Unknown SerialFormat");
            return(new S2CRes(format, -1.0, ErrorCodes.FAIL, "Unknown SerialFormat"));
        }