Пример #1
0
 public static string JsonEncode(JsonValue json)
 {
     StringBuilder builder = new StringBuilder(BUILDER_CAPACITY);
     bool success = SerializeValue(json, builder);
     return (success ? builder.ToString() : null);
 }
Пример #2
0
        protected static JsonValue ParseValue(char[] json, ref int index)
        {
            JsonValue result = new JsonValue();
            switch (LookAhead(json, index))
            {
                case JSON.TOKEN_STRING:
                    return ParseString(json, ref index);
                case JSON.TOKEN_NUMBER:
                    return ParseNumber(json, ref index);
                case JSON.TOKEN_CURLY_OPEN:
                    return ParseObject(json, ref index);
                case JSON.TOKEN_SQUARED_OPEN:
                    return ParseArray(json, ref index);
                case JSON.TOKEN_TRUE:
                    NextToken(json, ref index);
                    result.Boolean = true;
                    break;
                case JSON.TOKEN_FALSE:
                    NextToken(json, ref index);
                    result.Boolean = false;
                    break;
                case JSON.TOKEN_NULL:
                    NextToken(json, ref index);
                    break;
                case JSON.TOKEN_NONE:
                    break;
            }

            return result;
        }
Пример #3
0
        protected static bool SerializeValue(JsonValue value, StringBuilder builder)
        {
            bool success = true;

            switch (value.Type)
            {
            case JsonType.String:
                success = SerializeString(value.String, builder);
                break;
            case JsonType.Object:
                success = SerializeObject(value.Object, builder);
                break;
            case JsonType.Array:
                success = SerializeArray(value.Array, builder);
                break;
            case JsonType.Number:
                success = SerializeNumber(value.Double, builder);
                break;
            case JsonType.Boolean:
                if (value.Boolean)
                builder.Append("true");
                else
                builder.Append("false");
                break;
            default:
                builder.Append("null");
                break;
            }
            return success;
        }
Пример #4
0
        protected static JsonValue ParseString(char[] json, ref int index)
        {
            JsonValue result = new JsonValue();
            StringBuilder s = new StringBuilder(BUILDER_CAPACITY);
            char c;

            EatWhitespace(json, ref index);

            // "
            c = json[index++];

            bool complete = false;
            while (!complete)
            {

                if (index == json.Length)
                {
                    break;
                }

                c = json[index++];
                if (c == '"')
                {
                    complete = true;
                    break;
                }
                else if (c == '\\')
                {

                    if (index == json.Length)
                    {
                        break;
                    }
                    c = json[index++];
                    if (c == '"')
                    {
                        s.Append('"');
                    }
                    else if (c == '\\')
                    {
                        s.Append('\\');
                    }
                    else if (c == '/')
                    {
                        s.Append('/');
                    }
                    else if (c == 'b')
                    {
                        s.Append('\b');
                    }
                    else if (c == 'f')
                    {
                        s.Append('\f');
                    }
                    else if (c == 'n')
                    {
                        s.Append('\n');
                    }
                    else if (c == 'r')
                    {
                        s.Append('\r');
                    }
                    else if (c == 't')
                    {
                        s.Append('\t');
                    }
                    else if (c == 'u')
                    {
                        int remainingLength = json.Length - index;
                        if (remainingLength >= 4)
                        {
                            // parse the 32 bit hex into an integer codepoint
                            uint codePoint;
                            if (!UInt32.TryParse(new string(json, index, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out codePoint))
                            {
                                return result;
                            }
                            // convert the integer codepoint to a unicode char and add to string
                            s.Append(Char.ConvertFromUtf32((int)codePoint));
                            // skip 4 chars
                            index += 4;
                        }
                        else
                        {
                            break;
                        }
                    }

                }
                else
                {
                    s.Append(c);
                }

            }

            if (complete)
            {
                result.String = s.ToString();
            }
            return result;
        }
Пример #5
0
        protected static JsonValue ParseObject(char[] json, ref int index)
        {
            JsonValue result = new JsonValue();
            result.Object = new Dictionary<string, JsonValue>();
            int token;

            // {
            NextToken(json, ref index);

            bool done = false;
            while (!done)
            {
                token = LookAhead(json, index);
                if (token == JSON.TOKEN_NONE)
                {
                    result.Empty();
                    return result;
                }
                else if (token == JSON.TOKEN_COMMA)
                {
                    NextToken(json, ref index);
                }
                else if (token == JSON.TOKEN_CURLY_CLOSE)
                {
                    NextToken(json, ref index);
                    break;
                }
                else
                {

                    // name
                    JsonValue name = ParseString(json, ref index);
                    if (name.Type != JsonType.String)
                    {
                        result.Empty();
                        return result;
                    }

                    // :
                    token = NextToken(json, ref index);
                    if (token != JSON.TOKEN_COLON)
                    {
                        result.Empty();
                        return result;
                    }

                    // value
                    JsonValue value = ParseValue(json, ref index);
                    if (value.Type == JsonType.None)
                    {
                        return value;
                    }

                    result.Object[name.String] = value;
                }
            }

            return result;
        }
Пример #6
0
        protected static JsonValue ParseNumber(char[] json, ref int index)
        {
            JsonValue result = new JsonValue();

            EatWhitespace(json, ref index);

            int lastIndex = GetLastIndexOfNumber(json, index);
            int charLength = (lastIndex - index) + 1;

            long lVal;
            if (Int64.TryParse(new string(json, index, charLength), NumberStyles.Any, CultureInfo.InvariantCulture, out lVal))
            {
                index = lastIndex + 1;
                result.Long = lVal;
            }

            double lfVal;
            if (result.Type == JsonType.None && Double.TryParse(new string(json, index, charLength), NumberStyles.Any, CultureInfo.InvariantCulture, out lfVal))
            {
                index = lastIndex + 1;
                result.Double = lfVal;
            }
            return result;
        }
Пример #7
0
        protected static JsonValue ParseArray(char[] json, ref int index)
        {
            JsonValue result = new JsonValue();
            result.Array = new List<JsonValue>();

            // [
            NextToken(json, ref index);

            bool done = false;
            while (!done)
            {
                int token = LookAhead(json, index);
                if (token == JSON.TOKEN_NONE)
                {
                    result.Empty();
                    return result;
                }
                else if (token == JSON.TOKEN_COMMA)
                {
                    NextToken(json, ref index);
                }
                else if (token == JSON.TOKEN_SQUARED_CLOSE)
                {
                    NextToken(json, ref index);
                    break;
                }
                else
                {
                    JsonValue value = ParseValue(json, ref index);
                    if (value.Type == JsonType.None)
                    {
                        return value;
                    }

                    result.Array.Add(value);
                }
            }

            return result;
        }
Пример #8
0
        private static void UpdateAuctions(Database db, JsonValue status, AuctionFaction faction, Dictionary<long, bool> webIds, IBlizzardProgress bp)
        {
            string factionName = (faction == AuctionFaction.Alliance) ? "alliance" :
                                 (faction == AuctionFaction.Horde) ? "horde" :
                                 "neutral";

            if (status.Object.ContainsKey(factionName))
            {
                var query = from auction in status.Object[factionName].Object["auctions"].Array
                            select new Auction()
                            {
                                AuctionId = auction.Object["auc"].Long,
                                ItemId = auction.Object["item"].Long,
                                Owner = auction.Object["owner"].String,
                                Bid = auction.Object["bid"].Long,
                                Buyout = auction.Object["buyout"].Long,
                                Quantity = auction.Object["quantity"].Long,
                                TimeLeft = (auction.Object["timeLeft"].String == "VERY_LONG") ? AuctionTimeLeft.VeryLong :
                                           (auction.Object["timeLeft"].String == "LONG") ? AuctionTimeLeft.Long :
                                           (auction.Object["timeLeft"].String == "MEDIUM") ? AuctionTimeLeft.Medium :
                                        AuctionTimeLeft.Short,
                                Faction = faction,
                                Status = AuctionStatus.Active,
                                FirstSeen = DateTime.Now,
                                LastSeen = DateTime.Now
                            };

                int progressCurrent = 0;
                int progressCount = query.Count();
                bp.UpdateProgress(progressCurrent, progressCount);

                foreach (Auction webAuction in query)
                {
                    Auction dbAuction = db.Auctions.SingleOrDefault(auction => auction.AuctionId == webAuction.AuctionId);
                    if (dbAuction != null)
                    {
                        dbAuction.LastSeen = DateTime.Now;
                        dbAuction.TimeLeft = webAuction.TimeLeft;
                    }
                    else
                    {
                        db.Auctions.InsertOnSubmit(webAuction);
                    }

                    webIds[webAuction.AuctionId] = true;

                    progressCurrent++;
                    if (progressCurrent % 1000 == 0)
                    {
                        bp.UpdateProgress(progressCurrent, progressCount);
                        db.SubmitChanges();
                    }
                }

                bp.UpdateProgress(progressCount, progressCount);
                db.SubmitChanges();
            }
        }