示例#1
0
文件: Match.cs 项目: 0xNF/esk8bst
        public static Match FromPostedMatch(PostedMatchObject match)
        {
            Match m = new Match()
            {
                Companies = match.Companies,
                Currency  = match.Currency ?? CurrencyMap.USD.Code,
                Price     = match.Price,
                bst       = match.BST,
            };

            return(m);
        }
示例#2
0
        public static PostedSubscribeObject FromJson(JObject jobj)
        {
            string email = "";
            List <PostedMatchObject> pmos = new List <PostedMatchObject>();

            if (jobj.TryGetValue("email", out JToken JTEmail))
            {
                email = JTEmail.Value <string>();
                if (String.IsNullOrWhiteSpace(email))
                {
                    throw new Exception("Supplied field's value wasn't valid for fields type");
                }
            }
            else
            {
                throw new Exception("A required field 'email' was missing from this object");
            }


            if (jobj.TryGetValue("matches", out JToken JTMatches))
            {
                if (!(JTMatches is JArray matchArr) || matchArr.Count == 0)
                {
                    throw new Exception("Supplied field's value wasn't valid for fields type");
                }
                foreach (JToken JTMatch in matchArr)
                {
                    JObject           matchObj = JTMatch as JObject;
                    PostedMatchObject pmo      = PostedMatchObject.FromJson(matchObj);
                    if (pmo == null)
                    {
                        throw new Exception("Failed  deserialzing match objects");
                    }
                    pmos.Add(pmo);
                }
            }
            else
            {
                throw new Exception("A required field 'matches' was missing from this object");
            }

            PostedSubscribeObject pso = new PostedSubscribeObject()
            {
                Email   = email,
                Matches = pmos
            };

            return(pso);
        }
示例#3
0
        public static PostedMatchObject FromJson(JObject jobj)
        {
            string        bst       = "NONE";
            List <string> companies = new List <string>();

            if (jobj.TryGetValue("bst", out JToken JTBST))
            {
                string bstval = JTBST.Value <string>();
                if (String.IsNullOrWhiteSpace(bstval))
                {
                    throw new Exception("Field 'bst' wasn't a valid BST string");
                }
                if (bstval == "SELL" || bstval == "BUY" || bstval == "TRADE" || bstval == "BST")
                {
                    bst = bstval;
                }
                else
                {
                    throw new Exception("Field 'bst' wasn't a valid BST string");
                }
            }
            else
            {
                throw new Exception("Missing required field 'bst'");
            }

            if (jobj.TryGetValue("companies", out JToken JTComps))
            {
                if (!(JTComps is JArray compArr))
                {
                    throw new Exception("Field 'companies' wasn't of the expected type");
                }
                foreach (JToken jtComp in JTComps)
                {
                    string s = jtComp.Value <string>();
                    if (!String.IsNullOrWhiteSpace(s))
                    {
                        companies.Add(s);
                    }
                }
            }
            else
            {
                throw new Exception("Missing required field 'companies'");
            }


            string currency = null;

            if (jobj.TryGetValue("currency", out JToken JTCurrency))
            {
                // doesn't matter if it doesn't exist
                string cur = JTCurrency.Value <string>();
                if (!String.IsNullOrWhiteSpace(cur))
                {
                    cur = cur.ToUpperInvariant();
                    if (CurrencyMap.CurrencyDict.ContainsKey(cur))
                    {
                        currency = CurrencyMap.CurrencyDict[cur].Code;
                    }
                }
            }

            int?price = null;

            if (jobj.TryGetValue("price", out JToken JTPrice))
            {
                int p = (int)JTPrice;
                if (p >= 0)
                {
                    price = p;
                }
                // doesn't matter if it doesn't exist
            }

            PostedMatchObject pmo = new PostedMatchObject()
            {
                BST       = bst,
                Companies = companies,
                Currency  = currency ?? CurrencyMap.USD.Code,
                Price     = price
            };

            return(pmo);
        }