Esempio n. 1
0
        public void ReadStringValue_Numbers_NotString()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader("[56,56]"));

            reader.Read();

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsDateTime();
            },
                "Unexpected character encountered while parsing value: 5. Path '', line 1, position 2."
                );

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsDateTime();
            },
                "Unexpected character encountered while parsing value: 6. Path '', line 1, position 3."
                );

            ExceptionAssert.Throws <JsonReaderException>(
                () =>
            {
                reader.ReadAsDateTime();
            },
                "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 4."
                );

            Assert.AreEqual(56, reader.ReadAsInt32());
            Assert.IsTrue(reader.Read());
        }
Esempio n. 2
0
        internal Match(JsonTextReader jsonReader)
        {
            int Depth = jsonReader.Depth;

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.PropertyName:
                    switch (jsonReader.Value.ToString())
                    {
                    case "match_id":
                        this.MatchId = long.Parse(jsonReader.ReadAsString());
                        break;

                    case "name":
                        this.Name = jsonReader.ReadAsString();
                        break;

                    case "start_time":
                        this.StartTime = jsonReader.ReadAsDateTime();
                        break;

                    case "end_time":
                        this.EndTime = jsonReader.ReadAsDateTime();
                        break;

                    case "games":
                        IList <Game> games = new List <Game>();
                        while (jsonReader.Read())
                        {
                            if (jsonReader.TokenType == JsonToken.StartObject)
                            {
                                games.Add(new Game(jsonReader));
                            }
                        }
                        this.Games = new ReadOnlyCollection <Game>(games);
                        break;

                    default:

                        break;
                    }
                    break;

                case JsonToken.EndObject:
                    if (jsonReader.Depth == Depth)
                    {
                        return;
                    }
                    break;
                }
            }
        }
        public static DateTime?ParseDateTime(JToken value)
        {
            var rawValue = value.AsString();

            if (string.IsNullOrWhiteSpace(rawValue))
            {
                return(null);
            }

            rawValue = rawValue.Replace("NeoDate", "Date");

            if (!DateRegex.IsMatch(rawValue))
            {
                if (!DateTime.TryParse(rawValue, out var parsed))
                {
                    return(null);
                }

                return(rawValue.EndsWith("Z", StringComparison.OrdinalIgnoreCase) ? parsed.ToUniversalTime() : parsed);
            }

            var text   = $"{{\"a\":\"{rawValue}\"}}";
            var reader = new JsonTextReader(new StringReader(text));

            reader.Read(); // JsonToken.StartObject
            reader.Read(); // JsonToken.PropertyName
            return(reader.ReadAsDateTime());
        }
Esempio n. 4
0
        private static Address CreateAddress(JsonTextReader reader)
        {
            Address a = new Address();

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    switch ((string)reader.Value)
                    {
                    case "Street":
                        a.Street = reader.ReadAsString();
                        break;

                    case "Phone":
                        a.Phone = reader.ReadAsString();
                        break;

                    case "Entered":
                        a.Entered = reader.ReadAsDateTime().GetValueOrDefault();
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            return(a);
        }
Esempio n. 5
0
        internal Exif(JsonTextReader jsonReader)
        {
            int depth = jsonReader.Depth;

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.PropertyName:
                    switch (jsonReader.Value.ToString())
                    {
                    case "private_resource":
                        DateTime = jsonReader.ReadAsDateTime().Value;
                        break;
                    }
                    break;

                case JsonToken.EndObject:
                    if (jsonReader.Depth == depth)
                    {
                        return;
                    }
                    break;
                }
            }
        }
        public static DateTime?ParseDateTime(JToken value)
        {
            var rawValue = value.AsString();

            if (string.IsNullOrWhiteSpace(rawValue))
            {
                return(null);
            }

            rawValue = rawValue.Replace("NeoDate", "Date");

            if (!DateRegex.IsMatch(rawValue))
            {
                DateTime parsed;
                if (!DateTime.TryParse(rawValue, out parsed))
                {
                    return(null);
                }
                if (rawValue.EndsWith("Z"))
                {
                    return(parsed.ToUniversalTime());
                }
                return(parsed);
            }

            var text   = string.Format("{{\"a\":\"{0}\"}}", rawValue);
            var reader = new JsonTextReader(new StringReader(text));

            reader.Read(); // JsonToken.StartObject
            reader.Read(); // JsonToken.PropertyName
            return(reader.ReadAsDateTime());
        }
Esempio n. 7
0
        public static void parse_3_2(Command cmd, Argument arg, JsonTextReader jr)
        {
            string prop = "";

            while (jr.Read())
            {
                if (jr.TokenType == JsonToken.PropertyName)
                {
                    prop = jr.Value.ToString();
                }
                else if (jr.TokenType == JsonToken.EndObject)
                {
                    break;
                }
                if (prop == "type")
                {
                    arg.type = jr.ReadAsString();
                    prop     = "";
                }
                else if (prop == "value")
                {
                    jr.Read();
                    arg.value = jr.Value;
                    prop      = "";
                }
                else if (prop == "date")
                {
                    arg.date = jr.ReadAsDateTime().Value;
                    prop     = "";
                }
            }
        }
Esempio n. 8
0
        public void ReadAsDateTime_MissingComma()
        {
            string         json   = "['2017-02-04T00:00:00Z' '2018-02-04T00:00:00Z' '2019-02-04T00:00:00Z']";
            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(
                new DateTime(2017, 2, 4, 0, 0, 0, DateTimeKind.Utc),
                (DateTime)reader.ReadAsDateTime()
                );

            ExceptionAssert.Throws <JsonReaderException>(
                () => reader.ReadAsDateTime(),
                "After parsing a value an unexpected character was encountered: '. Path '[0]', line 1, position 24."
                );
        }
        public void ReadAsDateTime_Boolean()
        {
            string json = @"true";

            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            ExceptionAssert.Throws <JsonReaderException>(() => { reader.ReadAsDateTime(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1.");
        }
Esempio n. 10
0
        public void ParseJson(string dataStr)
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(dataStr));

            while (reader.Read())
            {
                if (reader.Value != null)
                {
                    var propertyName = reader.Value.ToString();
                    switch (propertyName)
                    {
                    case "MarketName": { Symbol = MarketName = reader.ReadAsString(); break; }

                    case "High": { High = (decimal)reader.ReadAsDecimal(); break; }

                    case "Low": { Low = (decimal)reader.ReadAsDecimal(); break; }

                    case "Volume": { Volume = (decimal)reader.ReadAsDecimal(); break; }

                    case "Last": { Last = (decimal)reader.ReadAsDecimal(); break; }

                    case "BaseVolume": { BaseVolume = (decimal)reader.ReadAsDecimal(); break; }

                    case "TimeStamp": { TimeStamp = (DateTime)reader.ReadAsDateTime(); break; }

                    case "Bid": { Bid = (decimal)reader.ReadAsDecimal(); break; }

                    case "Ask": { Ask = (decimal)reader.ReadAsDecimal(); break; }

                    case "OpenBuyOrders": { OpenBuyOrders = (decimal)reader.ReadAsDecimal(); break; }

                    case "OpenSellOrders": { OpenSellOrders = (decimal)reader.ReadAsDecimal(); break; }

                    case "PrevDay": { PrevDay = (decimal)reader.ReadAsDecimal(); break; }

                    case "Created": { Created = (DateTime)reader.ReadAsDateTime(); break; }

                    default:
                    {
                        break;
                    }
                    }
                }
            }
        }
        public void ReadStringValue_InvalidEndArray()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader("]"));

            ExceptionAssert.Throws <JsonReaderException>(() =>
            {
                reader.ReadAsDateTime();
            }, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1.");
        }
Esempio n. 12
0
        private void DeserializeProperty(JsonSerializer serializer, JsonTextReader reader, string propertyName)
        {
            switch (propertyName)
            {
            case "CommandId":
                _commandId = Guid.Parse(reader.ReadAsString()); reader.Read();
                break;

            case "Address":
                _address = reader.ReadAsString(); reader.Read();
                break;

            case "Country":
                _country = reader.ReadAsString(); reader.Read();
                break;

            case "Date":
                _date = reader.ReadAsDateTime().Value; reader.Read();
                break;

            case "Email":
                _email = reader.ReadAsString(); reader.Read();
                break;

            case "FirstName":
                _firstName = reader.ReadAsString(); reader.Read();
                break;

            case "LastName":
                _lastName = reader.ReadAsString(); reader.Read();
                break;

            case "Phone":
                _phone = reader.ReadAsString(); reader.Read();
                break;

            case "Province":
                _province = reader.ReadAsString(); reader.Read();
                break;

            case "Pizzas":
                reader.Read();
                _pizzas = serializer.Deserialize <List <OrderPizza> >(reader); reader.Read();
                break;

            case "Beverages":
                reader.Read();
                _beverages = serializer.Deserialize <List <OrderBeverage> >(reader); reader.Read();
                break;

            default:
                // skip unuseful properties
                reader.ReadAsString(); reader.Read();
                // throw new NotSupportedException("Property not recognized");
                break;
            }
        }
Esempio n. 13
0
        public void Test()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(@"{""enddate"":-1}"));

            reader.Read();
            reader.Read();

            ExceptionAssert.Throws <JsonReaderException>(
                () => reader.ReadAsDateTime(),
                "Cannot read number value as type. Path 'enddate', line 1, position 13.");
        }
        private static MovieRating ReadOneMovieRating(JsonTextReader reader)
        {
            reader.Read();
            int reviewer = (int)reader.ReadAsInt32();

            reader.Read();
            int movie = (int)reader.ReadAsInt32();

            reader.Read();
            int grade = (int)reader.ReadAsInt32();

            reader.Read();
            DateTime date = (DateTime)reader.ReadAsDateTime();

            return(new MovieRating(reviewer, movie, grade, date));
        }
Esempio n. 15
0
        internal Event(JsonTextReader jsonReader)
        {
            int Depth = jsonReader.Depth;

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.PropertyName:
                    switch (jsonReader.Value.ToString())
                    {
                    case "display_html":
                        this.DisplayHtml = jsonReader.ReadAsString();
                        break;

                    case "beatmap_id":
                        this.BeatmapId = jsonReader.ReadAsInt32();
                        break;

                    case "beatmapset_id":
                        this.BeatmapSetId = jsonReader.ReadAsInt32();
                        break;

                    case "date":
                        this.Date = jsonReader.ReadAsDateTime();
                        break;

                    case "epicfactor":
                        this.Epificator = jsonReader.ReadAsInt32();
                        break;

                    default:

                        break;
                    }
                    break;

                case JsonToken.EndObject:
                    if (jsonReader.Depth == Depth)
                    {
                        return;
                    }
                    break;
                }
            }
        }
Esempio n. 16
0
        public static bool ReadForType(JsonTextReader reader, Type type)
        {
            // Explicitly read values as dates from JSON with reader.
            // We do this because otherwise dates are read as strings
            // and the JsonSerializer will use a conversion method that won't
            // preserve UTC in DateTime.Kind for UTC ISO8601 dates
            if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                reader.ReadAsDateTime();
            }
            else if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?))
            {
                reader.ReadAsDateTimeOffset();
            }
            else
            {
                reader.Read();
            }

            // TokenType will be None if there is no more content
            return(reader.TokenType != JsonToken.None);
        }
Esempio n. 17
0
            internal DecisionServiceEvent Parse(string line)
            {
                using (var jsonReader = new JsonTextReader(new StringReader(line)))
                {
                    while (jsonReader.Read())
                    {
                        if (!(jsonReader.TokenType == JsonToken.PropertyName && "_timestamp".Equals((string)jsonReader.Value, StringComparison.Ordinal)))
                        {
                            continue;
                        }

                        return(new DecisionServiceEvent
                        {
                            Line = line,
                            DateTime = (DateTime)jsonReader.ReadAsDateTime()
                        });
                    }
                }

                Console.WriteLine($"Invalid event found in {this.item.Blob.Uri}");
                // TODO: log invalid event?
                return(null);
            }
Esempio n. 18
0
        //called on every line
        public IEnumerable <IRow> ParseEvent(IUpdatableRow output, Stream input)
        {
            var shared = new SharedFields(output.Schema);

            output.Set(this.ParserErrorColumn, string.Empty);

            TextReader inputReader;
            bool       firstPass = true;
            int        slotIdx   = 0;

            inputReader      = new StreamReader(input, Encoding.UTF8);
            shared.SessionId = Guid.NewGuid().ToString();

            string errorMessage = null;

            // this is a optimized version only parsing parts of the data
            using (var jsonReader = new JsonTextReader(inputReader))
            {
                jsonReader.DateFormatString = "o";
                while (SafeJsonReader.Read(jsonReader, ref errorMessage))
                {
                    switch (jsonReader.TokenType)
                    {
                    case JsonToken.PropertyName:
                    {
                        var propertyName = (string)jsonReader.Value;

                        switch (propertyName)
                        {
                        case "EventId":
                            Helpers.ExtractPropertyString(jsonReader, output, this.EventIdColumn);
                            break;

                        case "Timestamp":
                            shared.Timestamp = (DateTime)jsonReader.ReadAsDateTime();
                            break;

                        case "_outcomes":
                            break;

                        case "_id":
                            Helpers.ExtractPropertyString(jsonReader, output, this.EventIdColumn);
                            output.Set(this.SlotIdxColumn, slotIdx++);
                            break;

                        case "_label_cost":
                            if (!firstPass)
                            {
                                yield return(shared.Apply(output));
                            }
                            firstPass = false;
                            Helpers.ExtractPropertyDouble(jsonReader, output, this.CostColumn);
                            output.Set(this.IsDanglingColumn, false);
                            break;

                        case "EnqueuedTimeUtc":
                            output.Set(this.EnqueuedTimeUtcColumn, (DateTime)jsonReader.ReadAsDateTime());
                            output.Set(this.IsDanglingColumn, true);
                            break;

                        case "_p":
                            if (this.ProbabilityColumn.IsRequired)
                            {
                                output.Set(this.ProbabilityColumn.Idx, Helpers.EnumerateFloats(jsonReader).First());
                            }
                            break;

                        case "_a":
                            if (this.ActionColumn.IsRequired || this.NumActionsPerSlotColumn.IsRequired || this.CbActionColumn.IsRequired)
                            {
                                var actions = Helpers.EnumerateInts(jsonReader).ToList();
                                output.Set(this.NumActionsPerSlotColumn, actions.Count);
                                var chosen = actions[0];
                                output.Set(this.ActionColumn, chosen);
                                if (this.CbActionColumn.IsRequired)
                                {
                                    output.Set(this.CbActionColumn, actions.Count(a => a < chosen));
                                }
                            }
                            break;

                        case "_o":
                            if (!this.HasObservationsColumn.IsRequired)
                            {
                                jsonReader.Skip();
                            }
                            else
                            {
                                if (this.HasObservationsColumn.IsRequired)
                                {
                                    output.Set(this.HasObservationsColumn.Idx, Helpers.CountObjects(jsonReader) > 0 ? 1 : 0);
                                }
                            }
                            break;

                        case "c":
                            if (!shared.NumActionsColumn.IsRequired)
                            {
                                jsonReader.Skip();
                            }
                            break;

                        case "_multi":
                            if (shared.NumActionsColumn.IsRequired)
                            {
                                shared.NumActions = Helpers.CountObjects(jsonReader);
                            }
                            break;

                        case "pdrop":
                            shared.pdrop = Helpers.GetPropertyDouble(jsonReader);
                            break;

                        case "RewardValue":
                            Helpers.ExtractPropertyDoubleOpt(jsonReader, output, this.RewardValueColumn);
                            break;

                        default:
                            jsonReader.Skip();
                            break;
                        }
                    }
                    break;
                    }
                }
                if (!string.IsNullOrEmpty(errorMessage))
                {
                    output.Set(this.ParserErrorColumn, errorMessage);
                }
            }

            yield return(shared.Apply(output));
        }
Esempio n. 19
0
 public static DateTime parseAsDateTime(JsonTextReader reader)
 {
     return(reader.ReadAsDateTime().Value);
 }
Esempio n. 20
0
        private bool ExportArrayReadArray(string server, int port, string signal, DateTime dtStart, DateTime dtEnd, List <TSPointWeb> addTo)
        {
            WebClient webClient = new WebClient();

            string reqStr = CreateRequestArray(server, port, signal, dtStart, dtEnd);
            string json   = webClient.DownloadString(reqStr);

            if (json.StartsWith("Signal:") && json.Contains("not found"))
            {
                OnMessage(string.Format("Error: {0}. Try again.", json));
                return(false);
            }

            if (json.StartsWith("Bad Request Status:"))
            {
                OnMessage(string.Format("Error: {0}", json));
                return(false);
            }

            JsonTextReader txtRdr = new JsonTextReader(new StringReader(json));
            TSTypeWeb      type   = TSTypeWeb.Flag;
            int            count  = 0;

            while (txtRdr.Read())
            {
                var val = txtRdr.Value;
                if (ObjectAsStringEquals(val, "Type"))
                {
                    txtRdr.Read();
                    var txtRdrValue = int.Parse(txtRdr.Value.ToString());
                    type = (TSTypeWeb)txtRdrValue;
                }

                if (ObjectAsStringEquals(val, "Count"))
                {
                    txtRdr.Read();
                    count = int.Parse(txtRdr.Value.ToString());
                    break;
                }
            }

            OnMessage(string.Format("{0}: {1:yyyy MM dd hh:mm:ss} (local) for {2:F0} hours. Type {3}. Count {4}", signal, dtStart.ToLocalTime(), (dtEnd - dtStart).TotalHours, type, count));

            object   o  = null;
            DateTime?dt = null;

            while (txtRdr.Read())
            {
                var val = txtRdr.Value;

                if (ObjectAsStringEquals(val, "Value"))
                {
                    txtRdr.Read();
                    o = txtRdr.Value;
                }

                if (ObjectAsStringEquals(val, "TimeStamp"))
                {
                    dt = txtRdr.ReadAsDateTime();
                }

                if (o != null && dt != null)
                {
                    addTo.Add(TSWebFactory.Create(type, o, dt.Value));
                    o  = null;
                    dt = null;
                }
            }

            return(true);
        }
Esempio n. 21
0
            public IRow ParseEvent(IUpdatableRow output, Stream input)
            {
                TextReader inputReader;

                if (!this.hasData)
                {
                    inputReader = new StreamReader(input, Encoding.UTF8);
                }
                else
                {
                    string data = new StreamReader(input, Encoding.UTF8).ReadToEnd();
                    inputReader = new StringReader(data);

                    if (this.hasData)
                    {
                        output.Set(this.idxData, data);
                    }
                }

                if (this.hasJsonObject)
                {
                    var jsonReader = new JsonTextReader(inputReader);
                    jsonReader.DateFormatString = "o";
                    var jobj = JObject.ReadFrom(jsonReader);

                    var chosenActionIndex = (int)jobj.Value <long>("_labelIndex");

                    // iterate through all expressions
                    foreach (var fe in this.expressions)
                    {
                        if (fe.Idx >= 0)
                        {
                            // special support for chosen action
                            var expr = fe.JsonPath.Replace("$.c._multi[($._labelIndex)]", $"$.c._multi[{chosenActionIndex}]");
                            try
                            {
                                string value = jobj.SelectToken(expr)?.Value <string>();
                                if (value != null)
                                {
                                    output.Set(fe.Idx, value);
                                }
                            }
                            catch (Exception e)
                            {
                                output.Set(fe.Idx, e.Message);
                            }
                        }
                    }

                    // since we already parse it, no need to parse twice
                    if (this.hasEventId)
                    {
                        output.Set(this.idxEventId, jobj.Value <string>("EventId"));
                    }

                    if (this.hasTimestamp)
                    {
                        output.Set(this.idxTimestamp, jobj.Value <DateTime>("Timestamp"));
                    }

                    if (this.hasCost)
                    {
                        output.Set(this.idxCost, (float)jobj.Value <double>("_label_cost"));
                    }

                    if (this.hasProbability)
                    {
                        output.Set(this.idxProbability, (float)jobj.Value <double>("_label_probability"));
                    }

                    if (this.hasAction)
                    {
                        output.Set(this.idxAction, (int)jobj.Value <long>("_label_Action"));
                    }

                    if (this.hasNumActions)
                    {
                        if (jobj["a"] is JArray arr)
                        {
                            output.Set(this.idxNumActions, arr.Count);
                        }
                    }

                    if (this.hasHasObservations)
                    {
                        if (jobj["o"] is JArray arr)
                        {
                            output.Set(this.idxHasObservations, 1);
                        }
                    }

                    // return early
                    return(output.AsReadOnly());
                }
                // TODO: skip the dangling events
                bool foundLabelCost = false;

                // this is a optimized version only parsing parts of the data
                using (var jsonReader = new JsonTextReader(inputReader))
                {
                    jsonReader.DateFormatString = "o";
                    while (jsonReader.Read())
                    {
                        switch (jsonReader.TokenType)
                        {
                        case JsonToken.PropertyName:
                        {
                            var propertyName = (string)jsonReader.Value;

                            // "_label_cost":0,"_label_probability":0.200000003,"_label_Action":4,"_labelIndex":3,"Timestamp":"2018-03-30T01:48:11.5760000Z","Version":"1","EventId":
                            switch (propertyName)
                            {
                            case "EventId":
                                ExtractPropertyString(jsonReader, output, this.idxEventId, this.hasEventId);
                                break;

                            case "Timestamp":
                                if (this.hasTimestamp)
                                {
                                    output.Set(this.idxTimestamp, (DateTime)jsonReader.ReadAsDateTime());
                                }
                                break;

                            case "_label_cost":
                                foundLabelCost = true;
                                ExtractPropertyDouble(jsonReader, output, this.idxCost, this.hasCost);
                                break;

                            case "_label_probability":
                                ExtractPropertyDouble(jsonReader, output, this.idxProbability, this.hasProbability);
                                break;

                            case "_label_Action":
                                ExtractPropertyInteger(jsonReader, output, this.idxAction, this.hasAction);
                                break;

                            case "a":
                                if (!this.hasNumActions)
                                {
                                    jsonReader.Skip();
                                }
                                else
                                {
                                    output.Set(this.idxNumActions, CountArrayElements(jsonReader));
                                }
                                break;

                            case "o":
                                if (!this.hasHasObservations)
                                {
                                    jsonReader.Skip();
                                }
                                else
                                {
                                    output.Set(this.idxHasObservations, 1);
                                }
                                break;

                            default:
                                jsonReader.Skip();
                                break;
                            }
                        }
                        break;
                        }
                    }
                }

                // skip dangling events
                return(foundLabelCost ? output.AsReadOnly() : null);
            }
        public void DateParseHandling()
        {
            string json = @"[""1970-01-01T00:00:00Z"",""\/Date(0)\/""]";

            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            reader.DateParseHandling = Json.DateParseHandling.DateTime;

            Assert.IsTrue(reader.Read());
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value);
            Assert.AreEqual(typeof(DateTime), reader.ValueType);
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value);
            Assert.AreEqual(typeof(DateTime), reader.ValueType);
            Assert.IsTrue(reader.Read());

#if !NET20
            reader = new JsonTextReader(new StringReader(json));
            reader.DateParseHandling = Json.DateParseHandling.DateTimeOffset;

            Assert.IsTrue(reader.Read());
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value);
            Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value);
            Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
            Assert.IsTrue(reader.Read());
#endif

            reader = new JsonTextReader(new StringReader(json));
            reader.DateParseHandling = Json.DateParseHandling.None;

            Assert.IsTrue(reader.Read());
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(@"1970-01-01T00:00:00Z", reader.Value);
            Assert.AreEqual(typeof(string), reader.ValueType);
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(@"/Date(0)/", reader.Value);
            Assert.AreEqual(typeof(string), reader.ValueType);
            Assert.IsTrue(reader.Read());

#if !NET20
            reader = new JsonTextReader(new StringReader(json));
            reader.DateParseHandling = Json.DateParseHandling.DateTime;

            Assert.IsTrue(reader.Read());
            reader.ReadAsDateTimeOffset();
            Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value);
            Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
            reader.ReadAsDateTimeOffset();
            Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value);
            Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
            Assert.IsTrue(reader.Read());

            reader = new JsonTextReader(new StringReader(json));
            reader.DateParseHandling = Json.DateParseHandling.DateTimeOffset;

            Assert.IsTrue(reader.Read());
            reader.ReadAsDateTime();
            Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value);
            Assert.AreEqual(typeof(DateTime), reader.ValueType);
            reader.ReadAsDateTime();
            Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value);
            Assert.AreEqual(typeof(DateTime), reader.ValueType);
            Assert.IsTrue(reader.Read());
#endif
        }
Esempio n. 23
0
        internal Scores(JsonTextReader jsonReader)
        {
            int Depth = jsonReader.Depth;

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.PropertyName:
                    switch (jsonReader.Value.ToString())
                    {
                    case "score":
                        this.Score = jsonReader.ReadAsInt32();
                        break;

                    case "username":
                        this.Username = jsonReader.ReadAsString();
                        break;

                    case "count300":
                        this.Count300 = jsonReader.ReadAsInt32();
                        break;

                    case "count100":
                        this.Count100 = jsonReader.ReadAsInt32();
                        break;

                    case "count50":
                        this.Count50 = jsonReader.ReadAsInt32();
                        break;

                    case "countmiss":
                        this.CountMiss = jsonReader.ReadAsInt32();
                        break;

                    case "maxcombo":
                        this.MaxCombo = jsonReader.ReadAsInt32();
                        break;

                    case "countkatu":
                        this.CountKatu = jsonReader.ReadAsInt32();
                        break;

                    case "countgeki":
                        this.CountGeki = jsonReader.ReadAsInt32();
                        break;

                    case "perfect":
                        this.Perfect = Convert.ToBoolean(jsonReader.ReadAsInt32());
                        break;

                    case "enabled_mods":
                        this.EnabledMods = (Mods)jsonReader.ReadAsInt32();
                        break;

                    case "user_id":
                        this.UserId = jsonReader.ReadAsInt32();
                        break;

                    case "date":
                        this.Date = jsonReader.ReadAsDateTime();
                        break;

                    case "rank":
                        this.Rank = jsonReader.ReadAsString();
                        break;

                    case "pp":
                        this.PP = jsonReader.ReadAsDecimal();
                        break;

                    case "score_id":
                        this.ScoreId = jsonReader.ReadAsInt32();
                        break;

                    case "beatmap_id":
                        this.BeatmapId = long.Parse(jsonReader.ReadAsString());
                        break;

                    default:

                        break;
                    }
                    break;

                case JsonToken.EndObject:
                    if (jsonReader.Depth == Depth)
                    {
                        return;
                    }
                    break;
                }
            }
        }
Esempio n. 24
0
        internal Resource(JsonTextReader jsonReader)
        {
            int depth = jsonReader.Depth;

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.PropertyName:
                    switch (jsonReader.Value.ToString())
                    {
                    case "public_key":
                        publicKey = jsonReader.ReadAsString();
                        break;

                    case "_embedded":
                        embedded = new ResourceList(jsonReader);
                        break;

                    case "name":
                        name = jsonReader.ReadAsString();
                        break;

                    case "created":
                        created = jsonReader.ReadAsDateTime().Value;
                        break;

                    case "custom_properties":
                        IDictionary <string, string> customProperties = new Dictionary <string, string>();
                        int depth2 = jsonReader.Depth;
                        while (jsonReader.Read())
                        {
                            if (jsonReader.TokenType == JsonToken.PropertyName)
                            {
                                customProperties.Add(jsonReader.Value.ToString(), jsonReader.ReadAsString());
                            }
                            else if (jsonReader.TokenType == JsonToken.EndObject)
                            {
                                if (jsonReader.Depth == depth2)
                                {
                                    break;
                                }
                            }
                        }
                        customProperties = new ReadOnlyDictionary <string, string>(customProperties);
                        break;

                    case "public_url":
                        publicUrl = new Uri(jsonReader.ReadAsString());
                        break;

                    case "origin_path":
                        originPath = jsonReader.ReadAsString();
                        break;

                    case "modified":
                        modified = jsonReader.ReadAsDateTime().Value;
                        break;

                    case "path":
                        path = jsonReader.ReadAsString();
                        break;

                    case "md5":
                        md5 = jsonReader.ReadAsString();
                        break;

                    case "type":
                        type = jsonReader.ReadAsString() == "file" ? Type.file : Type.dir;
                        break;

                    case "mime_type":
                        mimeType = jsonReader.ReadAsString();
                        break;

                    case "size":
                        size = long.Parse(jsonReader.ReadAsString());
                        break;

                    case "sha256":
                        sha256 = jsonReader.ReadAsString();
                        break;

                    case "revision":
                        revision = long.Parse(jsonReader.ReadAsString());
                        break;

                    case "media_type":
                        mediaType = jsonReader.ReadAsString();
                        break;

                    case "preview":
                        preview = new Uri(jsonReader.ReadAsString());
                        break;

                    case "resource_id":
                        resourceId = jsonReader.ReadAsString();
                        break;

                    case "comment_ids":
                        commentIds = new CommentIds(jsonReader);
                        break;

                    case "share":
                        share = new ShareInfo(jsonReader);
                        break;

                    case "views_count":
                        viewsCount = jsonReader.ReadAsInt32();
                        break;

                    case "owner":
                        owner = new UserPublicInformation(jsonReader);
                        break;
                    }
                    break;

                case JsonToken.EndObject:
                    if (jsonReader.Depth == depth)
                    {
                        return;
                    }
                    break;
                }
            }
        }
Esempio n. 25
0
        private TestClass DeserializeJsonNetManual(string json)
        {
            TestClass c = new TestClass();

            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            reader.Read();
            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    string propertyName = (string)reader.Value;
                    switch (propertyName)
                    {
                    case "strings":
                        reader.Read();
                        while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                        {
                            c.strings.Add((string)reader.Value);
                        }
                        break;

                    case "dictionary":
                        reader.Read();
                        while (reader.Read() && reader.TokenType != JsonToken.EndObject)
                        {
                            string key = (string)reader.Value;
                            c.dictionary.Add(key, reader.ReadAsInt32().GetValueOrDefault());
                        }
                        break;

                    case "Name":
                        c.Name = reader.ReadAsString();
                        break;

                    case "Now":
                        c.Now = reader.ReadAsDateTime().GetValueOrDefault();
                        break;

                    case "BigNumber":
                        c.BigNumber = reader.ReadAsDecimal().GetValueOrDefault();
                        break;

                    case "Address1":
                        reader.Read();
                        c.Address1 = CreateAddress(reader);
                        break;

                    case "Addresses":
                        reader.Read();
                        while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                        {
                            var address = CreateAddress(reader);
                            c.Addresses.Add(address);
                        }
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return(c);
        }
Esempio n. 26
0
        internal ResourceList(JsonTextReader jsonReader) : base()
        {
            int depth = jsonReader.Depth;

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.PropertyName:
                    switch (jsonReader.Value.ToString())
                    {
                    case "sort":
                        sort = jsonReader.ReadAsString();
                        break;

                    case "public_key":
                        publicKey = jsonReader.ReadAsString();
                        break;

                    case "items":
                        jsonReader.Read();
                        while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray)
                        {
                            items.Add(new Resource(jsonReader));
                        }
                        break;

                    case "path":
                        path = jsonReader.ReadAsString();
                        break;

                    case "limit":
                        limit = jsonReader.ReadAsInt32().Value;
                        break;

                    case "offset":
                        offset = jsonReader.ReadAsInt32().Value;
                        break;

                    case "total":
                        total = jsonReader.ReadAsInt32().Value;
                        break;

                    case "name":
                        name = jsonReader.ReadAsString();
                        break;

                    case "created":
                        created = (DateTime)jsonReader.ReadAsDateTime();
                        break;

                    case "modified":
                        created = (DateTime)jsonReader.ReadAsDateTime();
                        break;

                    case "type":
                        System.Diagnostics.Debug.WriteLine($"type = {jsonReader.ReadAsString()}");
                        break;
                    }
                    break;

                case JsonToken.EndObject:
                    if (jsonReader.Depth == depth)
                    {
                        return;
                    }
                    break;
                }
            }
        }
        /// <summary>
        /// Reads any existing cache index file, building the in-memory representation, without bringing in actual item data until actually requested.
        /// </summary>
        private void RestoreIndex()
        {
            if (DefaultStorageStrategy == CacheStorageStrategy.OnlyMemory)
            {
                return;
            }

            try
            {
                var fn = Path.Combine(RootDirectory, "index.json");

                if (File.Exists(fn))
                {
                    using (var jr = new JsonTextReader(new StreamReader(File.OpenRead(fn))))
                    {
                        // Should be start array
                        jr.Read();

                        while (jr.Read() && jr.TokenType != JsonToken.EndArray)
                        {
                            // Should be start object
                            jr.Read();

                            var i = new MFSEntry();

                            i.FileName = jr.ReadAsString();
                            jr.Read();
                            i.ObjectTypeName = jr.ReadAsString();
                            jr.Read();
                            i.ByQuerySHA = jr.ReadAsString();
                            jr.Read();
                            i.QueryForAll = jr.ReadAsBoolean().GetValueOrDefault();
                            jr.Read();
                            i.ByIdentityComposite = jr.ReadAsString();
                            jr.Read();
                            i.ExpiryDate = jr.ReadAsDateTime().GetValueOrDefault();

                            jr.Read();

                            if (i.ExpiryDate > DateTime.Now)
                            {
                                i.Active    = true;
                                i.Persisted = true;
                                _index.Add(i);
                            }
                        }
                    }
                }
                else
                {
                    FlushAll(RootDirectory);
                    _index.Clear();
                }
            }
            catch (Exception ex)
            {
                CEFDebug.WriteInfo($"Exception in cache restore index: {ex.Message}");

                // Any problems, go with an empty cache
                FlushAll(RootDirectory);
                _index.Clear();
            }
        }
Esempio n. 28
0
        //called on every line
        public IRow ParseEvent(IUpdatableRow output, Stream input)
        {
            TextReader inputReader;

            output.Set(this.SkipLearnColumn, false);
            if (!this.DataColumn.IsRequired)
            {
                inputReader = new StreamReader(input, Encoding.UTF8);
            }
            else
            {
                string data = new StreamReader(input, Encoding.UTF8).ReadToEnd();
                inputReader = new StringReader(data);
                output.Set(this.DataColumn, data);
            }

            if (this.hasJsonObject)
            {
                var jsonReader = new JsonTextReader(inputReader);
                jsonReader.DateFormatString = "o";
                var jobj = JObject.ReadFrom(jsonReader);

                var chosenActionIndex = (int)jobj.Value <long>("_labelIndex");

                // iterate through all expressions
                foreach (var fe in this.expressions)
                {
                    if (fe.Idx >= 0)
                    {
                        // special support for chosen action
                        var expr = fe.JsonPath.Replace("$.c._multi[($._labelIndex)]", $"$.c._multi[{chosenActionIndex}]");
                        try
                        {
                            string value = jobj.SelectToken(expr)?.Value <string>();
                            if (value != null)
                            {
                                output.Set(fe.Idx, value);
                            }
                        }
                        catch (Exception e)
                        {
                            output.Set(fe.Idx, e.Message);
                        }
                    }
                }

                // since we already parse it, no need to parse twice
                output.Set(this.EventIdColumn, jobj.Value <string>("EventId"));
                output.Set(this.TimestampColumn, jobj.Value <DateTime>("Timestamp"));
                output.Set(this.EnqueuedTimeUtcColumn, jobj.Value <DateTime>("EnqueuedTimeUtc"));

                if (this.SkipLearnColumn.IsRequired)
                {
                    var optional = jobj.Value <bool?>("_skipLearn");;
                    output.Set(this.SkipLearnColumn.Idx, (bool)(optional.HasValue ? optional : false));
                }

                output.Set(this.CostColumn, (float)jobj.Value <double>("_label_cost"));
                output.Set(this.ProbabilityColumn, (float)jobj.Value <double>("_label_probability"));
                output.Set(this.ActionColumn, (int)jobj.Value <long>("_label_Action"));

                if (this.PdropColumn.IsRequired)
                {
                    var optional = jobj.Value <double?>("pdrop");
                    output.Set(this.PdropColumn.Idx, (float)(optional.HasValue ? optional.Value : 0.0f));
                }

                if (this.NumActionsColumn.IsRequired)
                {
                    if (jobj["a"] is JArray arr)
                    {
                        output.Set(this.NumActionsColumn.Idx, arr.Count);
                    }
                }

                if (this.HasObservationsColumn.IsRequired)
                {
                    if (jobj["o"] is JArray arr)
                    {
                        output.Set(this.HasObservationsColumn.Idx, 1);
                    }
                }

                // return early
                return(output.AsReadOnly());
            }
            // TODO: skip the dangling events
            bool foundLabelCost = false;

            // this is a optimized version only parsing parts of the data
            using (var jsonReader = new JsonTextReader(inputReader))
            {
                jsonReader.DateFormatString = "o";
                while (jsonReader.Read())
                {
                    switch (jsonReader.TokenType)
                    {
                    case JsonToken.PropertyName:
                    {
                        var propertyName = (string)jsonReader.Value;

                        // "_label_cost":0,"_label_probability":0.200000003,"_label_Action":4,"_labelIndex":3,"Timestamp":"2018-03-30T01:48:11.5760000Z","Version":"1","EventId":
                        switch (propertyName)
                        {
                        case "EventId":
                            Helpers.ExtractPropertyString(jsonReader, output, this.EventIdColumn);
                            break;

                        case "Timestamp":
                            output.Set(this.TimestampColumn, (DateTime)jsonReader.ReadAsDateTime());
                            break;

                        case "_label_cost":
                            foundLabelCost = true;
                            Helpers.ExtractPropertyDouble(jsonReader, output, this.CostColumn);
                            output.Set(this.IsDanglingColumn, false);
                            break;

                        case "_label_probability":
                            Helpers.ExtractPropertyDouble(jsonReader, output, this.ProbabilityColumn);
                            break;

                        case "_label_Action":
                            Helpers.ExtractPropertyInteger(jsonReader, output, this.ActionColumn);
                            break;

                        case "a":
                            if (!this.NumActionsColumn.IsRequired)
                            {
                                jsonReader.Skip();
                            }
                            else
                            {
                                output.Set(this.NumActionsColumn.Idx, Helpers.CountArrayElements(jsonReader));
                            }
                            break;

                        case "o":
                            if (!this.HasObservationsColumn.IsRequired)
                            {
                                jsonReader.Skip();
                            }
                            else
                            {
                                output.Set(this.HasObservationsColumn.Idx, 1);
                            }
                            break;

                        case "pdrop":
                            Helpers.ExtractPropertyDouble(jsonReader, output, this.PdropColumn);
                            break;

                        case "_skipLearn":
                            Helpers.ExtractPropertyBool(jsonReader, output, this.SkipLearnColumn);
                            break;

                        case "EnqueuedTimeUtc":
                            output.Set(this.EnqueuedTimeUtcColumn, (DateTime)jsonReader.ReadAsDateTime());
                            output.Set(this.IsDanglingColumn, true);
                            break;

                        case "RewardValue":
                            Helpers.ExtractPropertyDoubleOpt(jsonReader, output, this.RewardValueColumn);
                            break;

                        default:
                            jsonReader.Skip();
                            break;
                        }
                    }
                    break;
                    }
                }
            }

            return(output.AsReadOnly());
        }
Esempio n. 29
0
        private TSPointWeb ReadPoint(string server, int port, string signal, DateTime dt)
        {
            try
            {
                WebClient webClient = new WebClient();

                string reqStr = CreateRequestPoint(server, port, signal, dt);
                string json   = webClient.DownloadString(reqStr);

                if (json.StartsWith("Signal:") && json.Contains("not found"))
                {
                    OnMessage(string.Format("Error: {0}. Try again.", json));
                    return(null);
                }

                if (json.StartsWith("Bad Request Status:"))
                {
                    OnMessage(string.Format("Error: {0}", json));
                    return(null);
                }

                if (json.StartsWith("Error: point not found"))
                {
                    OnMessage(string.Format("Point not found for {0} at {1} (local)", signal, dt.ToLocalTime()));
                    return(null);
                }

                JsonTextReader txtRdr = new JsonTextReader(new StringReader(json));

                TSTypeWeb type  = TSTypeWeb.Flag;
                int       count = 0;
                object    val;
                while (txtRdr.Read())
                {
                    val = txtRdr.Value;
                    if (ObjectAsStringEquals(val, "Type"))
                    {
                        txtRdr.Read();
                        var txtRdrValue = int.Parse(txtRdr.Value.ToString());
                        type = (TSTypeWeb)txtRdrValue;
                        break;
                    }
                    else
                    {
                        if (val != null)
                        {
                            OnMessage(val.ToString());
                        }
                    }
                }

                object   o   = null;
                DateTime?rdt = null;

                while (txtRdr.Read())
                {
                    val = txtRdr.Value;

                    if (ObjectAsStringEquals(val, "Value"))
                    {
                        txtRdr.Read();
                        o = txtRdr.Value;
                    }

                    if (ObjectAsStringEquals(val, "TimeStamp"))
                    {
                        rdt = txtRdr.ReadAsDateTime();
                    }
                }

                if (o != null && rdt != null)
                {
                    return(TSWebFactory.Create(type, o, rdt.Value));
                }
                return(null);
            }
            catch (Exception e)
            {
                OnMessage("Error: " + e);
                return(null);
            }
        }
Esempio n. 30
0
        internal Game(JsonTextReader jsonReader)
        {
            int Depth = jsonReader.Depth;

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.PropertyName:
                    switch (jsonReader.Value.ToString())
                    {
                    case "game_id":
                        this.GameId = long.Parse(jsonReader.ReadAsString());
                        break;

                    case "start_time":
                        this.StartTime = jsonReader.ReadAsDateTime();
                        break;

                    case "end_time":
                        this.EndTime = jsonReader.ReadAsDateTime();
                        break;

                    case "beatmap_id":
                        this.Beatmap_Id = long.Parse(jsonReader.ReadAsString());
                        break;

                    case "play_mode":
                        this.PlayMode = (Mode)jsonReader.ReadAsInt32();
                        break;

                    case "match_type":
                        this.MatchType = jsonReader.ReadAsInt32();
                        break;

                    case "scoring_type":
                        this.ScoringType = (WinningCondition)jsonReader.ReadAsInt32();
                        break;

                    case "team_type":
                        this.TeamType = (TeamType)jsonReader.ReadAsInt32();
                        break;

                    case "mods":
                        this.Mods = (Mods)jsonReader.ReadAsInt32();
                        break;

                    case "scores":
                        IList <Scores> scores = new List <Scores>();
                        while (jsonReader.Read())
                        {
                            if (jsonReader.TokenType == JsonToken.StartObject)
                            {
                                scores.Add(new Scores(jsonReader));
                            }
                        }
                        this.Scores = new ReadOnlyCollection <Scores>(scores);
                        break;

                    default:

                        break;
                    }
                    break;

                case JsonToken.EndObject:
                    if (jsonReader.Depth == Depth)
                    {
                        return;
                    }
                    break;
                }
            }
        }