Beispiel #1
0
 /// <summary>
 /// Deserialize a JSON fragment into a new object
 /// </summary>
 /// <param name="xml"></param>
 public static T ParseFromJson <T>(string json)
 {
     try
     {
         return(jsonUtils.ToObject <T>(json));
     }
     catch (Exception e)
     {
         log.ErrorFormat("Cannot parse from json string:{0} due to {1}", json.PrintNull(), e);
         return(default(T));
     }
 }
Beispiel #2
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////


        #region DeSerialization S2CRes
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        public static S2CResBaseEntity <T> DeserializeBaseEntity <T>(string content, SerialFormat format) where T : BaseEntity, new()
        {
            if (string.IsNullOrEmpty(content))
            {
                log.Error("Content is empty");
                return(null);
            }

            switch (format)
            {
            case SerialFormat.JSON:
                try
                {
                    return(jsonUtils.ToObject <S2CResBaseEntity <T> >(content));
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("Error while parsing content {0} due to {1}", content.PrintNull(), ex.Message);
                    return(new S2CResBaseEntity <T>(0.0F, ErrorCodes.FAIL, null));
                }

            case SerialFormat.XML:
                //parse content:
                XElement data = ParseContent(content);
                if (data == null)
                {
                    return(null);
                }

                //parse single elements:
                float execTime = -1;
                data.ParseNode("ExecTime", ref execTime, false);
                ErrorCodes status = ErrorCodes.FAIL;
                data.ParseNode <ErrorCodes>("Status", ref status, false);

                //parse actual data:
                XElement dataContent = data.GetNode("Data", false);
                T        tmp         = new T();
                tmp.Parse((XElement)dataContent.FirstNode);
                return(new S2CResBaseEntity <T>(execTime, status, tmp));
            }

            log.Error("Unknown SerialFormat");
            return(null);
        }
Beispiel #3
0
        private bool ReceiveAccessToken(string username, string password)
        {
            // 1) Request Token:
            string requestTokenData = SendGetRequest(BaseWS.Server + "OAuth/RequestToken", false);

            if (string.IsNullOrEmpty(requestTokenData))
            {
                log.Error("Cannot retrieve request Token");
                return(false);
            }

            OAuthResponse resp = null;

            try
            {
                resp = jsonUtils.ToObject <OAuthResponse>(requestTokenData);
            }
            catch (Exception e)
            {
                log.ErrorFormat("Cannot parse from json string:{0} due to {1}", requestTokenData.PrintNull(), e);
                return(false);
            }
            string reqToken = (resp?.RequestToken);

            if (string.IsNullOrEmpty(reqToken))
            {
                log.Error("Cannot retrieve request Token");
                return(false);
            }

            // 2) Access Token:
            string accessTokenData = SendPostRequest(BaseWS.Server + "OAuth/AccessToken",
                                                     "oauth_token=" + reqToken + "&grant_type=unused&username="******"&password="******"Cannot retrieve Access Token");
                return(false);
            }

            resp = null; //re-init resp
            try
            {
                resp = jsonUtils.ToObject <OAuthResponse>(accessTokenData);
            }
            catch (Exception e)
            {
                log.ErrorFormat("Cannot parse from json string:{0} due to {1}", accessTokenData.PrintNull(), e);
                return(false);
            }
            if (resp == null)
            {
                log.Error("Cannot retrieve Access Token");
                return(false);
            }

            m_accessToken = resp.AccessToken;
            if (resp.Expires > 0)
            {
                m_expirationAccessToken = DateTime.Now.AddSeconds(resp.Expires);
            }
            else
            {
                log.Error("Cannot retrieve Expiration of Access Token");
                return(false);
            }

            return(true);
        }
Beispiel #4
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"));
        }