public void ReadFrom()
    {
      JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(new StringReader("{'pie':true}")));
      Assert.AreEqual(true, (bool)o["pie"]);

      JArray a = (JArray)JToken.ReadFrom(new JsonTextReader(new StringReader("[1,2,3]")));
      Assert.AreEqual(1, (int)a[0]);
      Assert.AreEqual(2, (int)a[1]);
      Assert.AreEqual(3, (int)a[2]);

      JsonReader reader = new JsonTextReader(new StringReader("{'pie':true}"));
      reader.Read();
      reader.Read();

      JProperty p = (JProperty)JToken.ReadFrom(reader);
      Assert.AreEqual("pie", p.Name);
      Assert.AreEqual(true, (bool)p.Value);

      JConstructor c = (JConstructor)JToken.ReadFrom(new JsonTextReader(new StringReader("new Date(1)")));
      Assert.AreEqual("Date", c.Name);
      Assert.IsTrue(JToken.DeepEquals(new JValue(1), c.Values().ElementAt(0)));

      JValue v;

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""stringvalue""")));
      Assert.AreEqual("stringvalue", (string)v);

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1")));
      Assert.AreEqual(1, (int)v);

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1.1")));
      Assert.AreEqual(1.1, (double)v);
    }
        public void Example()
        {
            #region Usage
            string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";

            IList<Role> roles = new List<Role>();

            JsonTextReader reader = new JsonTextReader(new StringReader(json));
            reader.SupportMultipleContent = true;

            while (true)
            {
                if (!reader.Read())
                    break;

                JsonSerializer serializer = new JsonSerializer();
                Role role = serializer.Deserialize<Role>(reader);

                roles.Add(role);
            }

            foreach (Role role in roles)
            {
                Console.WriteLine(role.Name);
            }

            // Admin
            // Publisher
            #endregion

            Assert.AreEqual(2, roles.Count);
            Assert.AreEqual("Admin", roles[0].Name);
            Assert.AreEqual("Publisher", roles[1].Name);
        }
        public void ReadDollarQuoteStringWithTag()
        {
            string json = @"{""NameOfStore"":$tag$Forest's Bakery And Cafe$tag$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }
        public bool Read()
        {
            try {
                return(_textReader?.Read() == true);
            } catch (IOException) {
                Log.To.NoDomain.I(Tag, "Remote endpoint hung up, returning false for Read()");
                return(false);
            } catch (NullReferenceException) {
                // For some reason disposing the reader while it is blocked on a read will cause
                // an NRE
                Log.To.NoDomain.I(Tag, "Streaming read cancelled, returning false for Read()...");
                return(false);
            } catch (Exception e) {
                if (e is JsonReaderException)
                {
                    throw Misc.CreateExceptionAndLog(Log.To.NoDomain, StatusCode.BadJson, TAG,
                                                     "Error reading from streaming parser");
                }

                var se = e.InnerException as SocketException;
                if (se?.SocketErrorCode == SocketError.Interrupted)
                {
                    Log.To.NoDomain.I(Tag, "Streaming read cancelled, returning false for Read()...");
                    return(false);
                }

                throw Misc.CreateExceptionAndLog(Log.To.NoDomain, e, TAG, "Error reading from streaming parser");
            }
        }
Ejemplo n.º 5
0
        public void Load()
        {
            JsonReader reader = new JsonTextReader(new StringReader("new Date(123)"));
            reader.Read();

            JConstructor constructor = JConstructor.Load(reader);
            Assert.AreEqual("Date", constructor.Name);
            Assert.IsTrue(JToken.DeepEquals(new JValue(123), constructor.Values().ElementAt(0)));
        }
Ejemplo n.º 6
0
        public void ReadFrom()
        {
            JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(new StringReader("{'pie':true}")));
            Assert.AreEqual(true, (bool)o["pie"]);

            JArray a = (JArray)JToken.ReadFrom(new JsonTextReader(new StringReader("[1,2,3]")));
            Assert.AreEqual(1, (int)a[0]);
            Assert.AreEqual(2, (int)a[1]);
            Assert.AreEqual(3, (int)a[2]);

            JsonReader reader = new JsonTextReader(new StringReader("{'pie':true}"));
            reader.Read();
            reader.Read();

            JProperty p = (JProperty)JToken.ReadFrom(reader);
            Assert.AreEqual("pie", p.Name);
            Assert.AreEqual(true, (bool)p.Value);

            JConstructor c = (JConstructor)JToken.ReadFrom(new JsonTextReader(new StringReader("new Date(1)")));
            Assert.AreEqual("Date", c.Name);
            Assert.IsTrue(JToken.DeepEquals(new JValue(1), c.Values().ElementAt(0)));

            JValue v;

            v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""stringvalue""")));
            Assert.AreEqual("stringvalue", (string)v);

            v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1")));
            Assert.AreEqual(1, (int)v);

            v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1.1")));
            Assert.AreEqual(1.1, (double)v);

#if !NET20
            v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""1970-01-01T00:00:00+12:31"""))
            {
                DateParseHandling = DateParseHandling.DateTimeOffset
            });
            Assert.AreEqual(typeof(DateTimeOffset), v.Value.GetType());
            Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, new TimeSpan(12, 31, 0)), v.Value);
#endif
        }
        private void ParseRequest(string request)
        {
            using (var stringReader = new StringReader(request))
            using (var reader = new JsonTextReader(stringReader))
            {
                reader.DateParseHandling = DateParseHandling.None;

                // Messages parsed as a dictionary with the following
                // parameters:
                //
                //   'r': Request type: 0, 1 or 2
                //   'a': Association ID when the request type is 1 or 2
                //   't': Message type
                //   'p': Payload

                if (!reader.Read())
                    throw new HttpException("Invalid request");

                if (reader.TokenType == JsonToken.StartObject)
                {
                    ProcessMessageRequest(reader);
                }
                else if (reader.TokenType == JsonToken.StartArray)
                {
                    ReadToken(reader, JsonToken.String);

                    string action = (string)reader.Value;

                    ReadToken(reader, JsonToken.EndArray);

                    switch (action)
                    {
                        case "close":
                            _client.Dispose();
                            break;

                        default:
                            throw new HttpException("Invalid request");
                    }
                }
                else
                {
                    throw new HttpException("Invalid request");
                }

                if (reader.Read())
                    throw new HttpException("Invalid request");

            }
        }
        public void Example()
        {
            #region Usage
            string json = @"{
               'CPU': 'Intel',
               'PSU': '500W',
               'Drives': [
                 'DVD read/writer'
                 /*(broken)*/,
                 '500 gigabyte hard drive',
                 '200 gigabype hard drive'
               ]
            }";

            JsonTextReader reader = new JsonTextReader(new StringReader(json));
            while (reader.Read())
            {
                if (reader.Value != null)
                {
                    Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
                }
                else
                {
                    Console.WriteLine("Token: {0}", reader.TokenType);
                }
            }

            // Token: StartObject
            // Token: PropertyName, Value: CPU
            // Token: String, Value: Intel
            // Token: PropertyName, Value: PSU
            // Token: String, Value: 500W
            // Token: PropertyName, Value: Drives
            // Token: StartArray
            // Token: String, Value: DVD read/writer
            // Token: Comment, Value: (broken)
            // Token: String, Value: 500 gigabyte hard drive
            // Token: String, Value: 200 gigabype hard drive
            // Token: EndArray
            // Token: EndObject
            #endregion
        }
        private void ProcessMessageRequest(JsonTextReader reader)
        {
            MessageKind? messageKind = null;
            int? associationId = null;
            int? messageType = null;
            bool hadPayload = false;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                    break;
                else if (reader.TokenType != JsonToken.PropertyName)
                    throw new HttpException("Invalid request");

                switch ((string)reader.Value)
                {
                    case "r":
                        ReadToken(reader, JsonToken.Integer);
                        int value = (int)(long)reader.Value;

                        if (value < 0 || value > 2)
                            throw new HttpException("Invalid request type");

                        messageKind = (MessageKind)value;
                        break;

                    case "a":
                        ReadToken(reader, JsonToken.Integer);
                        associationId = (int)(long)reader.Value;

                        if (associationId < 0)
                            throw new HttpException("Invalid association id");
                        break;

                    case "t":
                        ReadToken(reader, JsonToken.Integer);
                        messageType = (int)(long)reader.Value;

                        if (messageType < 0)
                            throw new HttpException("Invalid message type");
                        break;

                    case "p":
                        hadPayload = true;

                        if (
                            !messageKind.HasValue ||
                                !messageType.HasValue ||
                                    (messageKind.Value != MessageKind.OneWay && !associationId.HasValue)
                            )
                            throw new HttpException("Invalid request");

                        ProcessRequest(
                            messageKind.Value,
                            messageType.Value,
                            (uint)associationId.GetValueOrDefault(0),
                            reader
                            );
                        break;

                    default:
                        throw new HttpException("Invalid request");
                }
            }

            if (!hadPayload)
                throw new HttpException("Invalid request");
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("[");

            int eliminated = 0;
            int kept       = 0;

            string         json   = File.ReadAllText(file);
            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            while (reader.Read())
            {
                if (reader.TokenType.ToString() == "StartObject")
                {
                    reader.Read();
                    if ((string)reader.Value == "id")
                    {
                        reader.Read();
                        Console.Write(reader.Value);

                        int id = int.Parse(reader.Value.ToString());

                        reader.Read();

                        if ((string)reader.Value == "name")
                        {
                            reader.Read();
                            string name = (string)reader.Value;

                            reader.Read();
                            if ((string)reader.Value == "alch_value")
                            {
                                reader.Read();
                                int alch = int.Parse(reader.Value.ToString());
                                Console.WriteLine(alch);

                                if (alch > 2000)
                                {
                                    sb.Append("{");
                                    sb.Append("\"id\": " + id + ",");
                                    sb.Append("\"name\": \"" + name + "\",");
                                    sb.Append("\"alch_value\": " + alch);

                                    sb.Append("},");
                                    kept++;
                                }
                                else
                                {
                                    eliminated++;
                                }
                            }
                        }
                    }
                }
            }


            sb.Remove(sb.Length - 1, 1);
            sb.Append("]");

            using (StreamWriter swriter = new StreamWriter("newoutput.json"))
            {
                swriter.Write(sb.ToString());
            }

            Console.WriteLine("New file produced");
            Console.WriteLine("Number of items elimnated = " + eliminated);
            Console.WriteLine("Number of items kept = " + kept);

            Console.ReadLine();
        }
Ejemplo n.º 11
0
        private static void CreateInputOperation(Process process, TransformalizeRequest request)
        {
            if (!request.Query.ContainsKey("data"))
            {
                return;
            }

            if (!process.Connections.Contains("input"))
            {
                return;
            }
            if (process.Connections.GetConnectionByName("input").Connection.Type != ProviderType.Internal)
            {
                return;
            }

            var rows   = new List <Row>();
            var sr     = new StringReader(request.Query["data"].ToString());
            var reader = new JsonTextReader(sr);

            reader.Read();
            if (reader.TokenType != JsonToken.StartArray)
            {
                throw new JsonException("The data passed into this process must be an array of arrays that contain input data matching the configured fields.  E.g. [['Dale','Newman', 34],['Gavin','Newman', 3]]");
            }

            var entity      = process.Entities.First();
            var inputFields = entity.InputFields();
            var conversion  = Common.GetObjectConversionMap();

            // Note: Input
            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.StartArray)
                {
                    var row = new Row();
                    foreach (var field in inputFields)
                    {
                        reader.Read();
                        row[field.Name] = conversion[field.SimpleType](reader.Value);
                    }
                    rows.Add(row);
                }
                else if (reader.TokenType == JsonToken.StartObject)
                {
                    var row = new Row();
                    do
                    {
                        reader.Read();
                        if (reader.TokenType == JsonToken.PropertyName)
                        {
                            var name = reader.Value.ToString();
                            reader.Read();
                            row[name] = reader.Value;
                        }
                    } while (reader.TokenType != JsonToken.EndObject);

                    foreach (var field in inputFields)
                    {
                        row[field.Name] = conversion[field.SimpleType](row[field.Name]);
                    }
                    rows.Add(row);
                }
            }

            entity.InputOperation = new RowsOperation(rows);
        }
Ejemplo n.º 12
0
    public void LoadFromNestedObjectIncomplete()
    {
      ExceptionAssert.Throws<JsonReaderException>("Unexpected end of content while loading JObject. Path 'short.error.code', line 6, position 15.",
      () =>
      {
        string jsonText = @"{
  ""short"":
  {
    ""error"":
    {
      ""code"":0";

        JsonReader reader = new JsonTextReader(new StringReader(jsonText));
        reader.Read();
        reader.Read();
        reader.Read();
        reader.Read();
        reader.Read();

        JToken.ReadFrom(reader);
      });
    }
Ejemplo n.º 13
0
        public void LoadJson(string url)
        {
            IDictionary <string, int> dict = new Dictionary <string, int>();
            string key = null;

            string json = ReadUrl(url);

            JsonTextReader reader   = new JsonTextReader(new StringReader(json));
            List <string>  toppings = new List <string>();

            while (reader.Read())
            {
                if (reader.Value != null)
                {
                    //Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
                    string str = reader.Value.ToString();
                    toppings.Add(str);
                }
                else
                {
                    //Console.WriteLine("Token: {0}", reader.TokenType);
                    if (reader.TokenType.ToString().Equals("StartArray"))
                    {
                        toppings = new List <string>();
                    }
                    if (reader.TokenType.ToString().Equals("EndArray"))
                    {
                        key = null;
                        int i = 0;

                        var sortedToppings = toppings.OrderBy(s => s).ToList();
                        foreach (string item in sortedToppings)
                        {
                            i++;
                            if (i == toppings.Count)
                            {
                                key = key + item;
                            }
                            else
                            {
                                key = key + item + "-";
                            }
                        }
                        //Console.WriteLine(key);
                        if (dict.ContainsKey(key))
                        {
                            dict[key] = dict[key] + 1;
                        }
                        else
                        {
                            dict.Add(new KeyValuePair <string, int>(key, 1));
                        }
                    }
                }
            }

            var sortedDict = dict.OrderByDescending(m => m.Value);

            int j = 0;

            foreach (KeyValuePair <string, int> kvp in sortedDict)
            {
                if (j < 20)
                {
                    Console.WriteLine(kvp.Key + ", " + kvp.Value);
                }
                j++;
            }
        }
Ejemplo n.º 14
0
        public void ImportData(Stream stream, SmugglerOptions options, bool importIndexes = true)
        {
            EnsureDatabaseExists();
            Stream sizeStream;

            var sw = Stopwatch.StartNew();
            // Try to read the stream compressed, otherwise continue uncompressed.
            JsonTextReader jsonReader;

            try
            {
                sizeStream = new CountingStream(new GZipStream(stream, CompressionMode.Decompress));
                var streamReader = new StreamReader(sizeStream);

                jsonReader = new JsonTextReader(streamReader);

                if (jsonReader.Read() == false)
                {
                    return;
                }
            }
            catch (InvalidDataException)
            {
                sizeStream = stream;
                stream.Seek(0, SeekOrigin.Begin);

                var streamReader = new StreamReader(stream);

                jsonReader = new JsonTextReader(streamReader);

                if (jsonReader.Read() == false)
                {
                    return;
                }
            }

            if (jsonReader.TokenType != JsonToken.StartObject)
            {
                throw new InvalidDataException("StartObject was expected");
            }

            // should read indexes now
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.PropertyName)
            {
                throw new InvalidDataException("PropertyName was expected");
            }
            if (Equals("Indexes", jsonReader.Value) == false)
            {
                throw new InvalidDataException("Indexes property was expected");
            }
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.StartArray)
            {
                throw new InvalidDataException("StartArray was expected");
            }

            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray)
            {
                var index = RavenJToken.ReadFrom(jsonReader);
                if ((options.OperateOnTypes & ItemType.Indexes) != ItemType.Indexes)
                {
                    continue;
                }
                var indexName = index.Value <string>("name");
                if (indexName.StartsWith("Temp/"))
                {
                    continue;
                }
                if (index.Value <RavenJObject>("definition").Value <bool>("IsCompiled"))
                {
                    continue;                     // can't import compiled indexes
                }
                PutIndex(indexName, index);
            }

            // should read documents now
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.PropertyName)
            {
                throw new InvalidDataException("PropertyName was expected");
            }
            if (Equals("Docs", jsonReader.Value) == false)
            {
                throw new InvalidDataException("Docs property was expected");
            }
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.StartArray)
            {
                throw new InvalidDataException("StartArray was expected");
            }
            var  batch         = new List <RavenJObject>();
            int  totalCount    = 0;
            long lastFlushedAt = 0;
            int  batchCount    = 0;
            long sizeOnDisk    = 0;

            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray)
            {
                var before   = sizeStream.Position;
                var document = (RavenJObject)RavenJToken.ReadFrom(jsonReader);
                var size     = sizeStream.Position - before;
                if (size > 1024 * 1024)
                {
                    Console.WriteLine("{0:#,#.##;;0} kb - {1}",
                                      (double)size / 1024,
                                      document["@metadata"].Value <string>("@id"));
                }
                if ((options.OperateOnTypes & ItemType.Documents) != ItemType.Documents)
                {
                    continue;
                }
                if (options.MatchFilters(document) == false)
                {
                    continue;
                }

                totalCount += 1;
                batch.Add(document);
                sizeOnDisk = (sizeStream.Position - lastFlushedAt);
                if (batch.Count >= smugglerOptions.BatchSize ||
                    sizeOnDisk >= MaxSizeOfUncomressedSizeToSendToDatabase)
                {
                    lastFlushedAt = sizeStream.Position;
                    HandleBatch(options, batch, sizeOnDisk);
                    sizeOnDisk = 0;
                    if (++batchCount % 10 == 0)
                    {
                        OutputIndexingDistance();
                    }
                }
            }
            HandleBatch(options, batch, sizeOnDisk);
            OutputIndexingDistance();

            var attachmentCount = 0;

            if (jsonReader.Read() == false || jsonReader.TokenType == JsonToken.EndObject)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.PropertyName)
            {
                throw new InvalidDataException("PropertyName was expected");
            }
            if (Equals("Attachments", jsonReader.Value) == false)
            {
                throw new InvalidDataException("Attachment property was expected");
            }
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.StartArray)
            {
                throw new InvalidDataException("StartArray was expected");
            }
            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray)
            {
                attachmentCount += 1;
                var item = RavenJToken.ReadFrom(jsonReader);
                if ((options.OperateOnTypes & ItemType.Attachments) != ItemType.Attachments)
                {
                    continue;
                }
                var attachmentExportInfo =
                    new JsonSerializer
                {
                    Converters = { new JsonToJsonConverter() }
                }.Deserialize <AttachmentExportInfo>(new RavenJTokenReader(item));
                ShowProgress("Importing attachment {0}", attachmentExportInfo.Key);

                PutAttachment(attachmentExportInfo);
            }
            ShowProgress("Imported {0:#,#;;0} documents and {1:#,#;;0} attachments in {2:#,#;;0} ms", totalCount, attachmentCount, sw.ElapsedMilliseconds);
        }
Ejemplo n.º 15
0
        public void ParseDoubles()
        {
            JsonTextReader reader = null;

            reader = new JsonTextReader(new StringReader("1.1"));
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(1.1d, reader.Value);

            reader = new JsonTextReader(new StringReader("-1.1"));
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(-1.1d, reader.Value);

            reader = new JsonTextReader(new StringReader("0.0"));
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(0.0d, reader.Value);

            reader = new JsonTextReader(new StringReader("-0.0"));
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(-0.0d, reader.Value);

            reader = new JsonTextReader(
                new StringReader(
                    "9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"
                    )
                );
            ExceptionAssert.Throws <JsonReaderException>(
                () => reader.Read(),
                "Unexpected character encountered while parsing number: s. Path '', line 1, position 77."
                );

            reader = new JsonTextReader(new StringReader("1E-06"));
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(0.000001d, reader.Value);

            reader = new JsonTextReader(new StringReader(""));
            Assert.IsFalse(reader.Read());

            reader = new JsonTextReader(new StringReader("-"));
            ExceptionAssert.Throws <JsonReaderException>(
                () => reader.Read(),
                "Input string '-' is not a valid number. Path '', line 1, position 1."
                );

            reader = new JsonTextReader(new StringReader("1.7976931348623157E+308"));
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(Double.MaxValue, reader.Value);

            reader = new JsonTextReader(new StringReader("-1.7976931348623157E+308"));
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(Double.MinValue, reader.Value);

            reader = new JsonTextReader(new StringReader("1E+309"));
#if !(NETSTANDARD2_0 || NETSTANDARD1_3)
            ExceptionAssert.Throws <JsonReaderException>(
                () => reader.Read(),
                "Input string '1E+309' is not a valid number. Path '', line 1, position 6."
                );
#else
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(Double.PositiveInfinity, reader.Value);
#endif

            reader = new JsonTextReader(new StringReader("-1E+5000"));
#if !(NETSTANDARD2_0 || NETSTANDARD1_3)
            ExceptionAssert.Throws <JsonReaderException>(
                () => reader.Read(),
                "Input string '-1E+5000' is not a valid number. Path '', line 1, position 8."
                );
#else
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(Double.NegativeInfinity, reader.Value);
#endif

            reader = new JsonTextReader(new StringReader("5.1231231E"));
            ExceptionAssert.Throws <JsonReaderException>(
                () => reader.Read(),
                "Input string '5.1231231E' is not a valid number. Path '', line 1, position 10."
                );

            reader = new JsonTextReader(new StringReader("1E-23"));
            Assert.IsTrue(reader.Read());
            Assert.AreEqual(typeof(double), reader.ValueType);
            Assert.AreEqual(1e-23, reader.Value);
        }
Ejemplo n.º 16
0
        public void ImportData(Stream stream, SmugglerOptions options, bool importIndexes = true)
        {
            EnsureDatabaseExists();

            var sw = Stopwatch.StartNew();
            // Try to read the stream compressed, otherwise continue uncompressed.
            JsonTextReader jsonReader;

            try
            {
                var streamReader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress));

                jsonReader = new JsonTextReader(streamReader);

                if (jsonReader.Read() == false)
                {
                    return;
                }
            }
            catch (InvalidDataException)
            {
                stream.Seek(0, SeekOrigin.Begin);

                var streamReader = new StreamReader(stream);

                jsonReader = new JsonTextReader(streamReader);

                if (jsonReader.Read() == false)
                {
                    return;
                }
            }

            if (jsonReader.TokenType != JsonToken.StartObject)
            {
                throw new InvalidDataException("StartObject was expected");
            }

            // should read indexes now
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.PropertyName)
            {
                throw new InvalidDataException("PropertyName was expected");
            }
            if (Equals("Indexes", jsonReader.Value) == false)
            {
                throw new InvalidDataException("Indexes property was expected");
            }
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.StartArray)
            {
                throw new InvalidDataException("StartArray was expected");
            }

            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray)
            {
                var index = RavenJToken.ReadFrom(jsonReader);
                if ((options.OperateOnTypes & ItemType.Indexes) != ItemType.Indexes)
                {
                    continue;
                }
                var indexName = index.Value <string>("name");
                if (indexName.StartsWith("Raven/") || indexName.StartsWith("Temp/"))
                {
                    continue;
                }
                PutIndex(indexName, index);
            }

            // should read documents now
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.PropertyName)
            {
                throw new InvalidDataException("PropertyName was expected");
            }
            if (Equals("Docs", jsonReader.Value) == false)
            {
                throw new InvalidDataException("Docs property was expected");
            }
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.StartArray)
            {
                throw new InvalidDataException("StartArray was expected");
            }
            var batch      = new List <RavenJObject>();
            int totalCount = 0;

            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray)
            {
                var document = (RavenJObject)RavenJToken.ReadFrom(jsonReader);
                if ((options.OperateOnTypes & ItemType.Documents) != ItemType.Documents)
                {
                    continue;
                }
                if (options.MatchFilters(document) == false)
                {
                    continue;
                }

                totalCount += 1;
                batch.Add(document);
                if (batch.Count >= 128)
                {
                    FlushBatch(batch);
                }
            }
            FlushBatch(batch);

            var attachmentCount = 0;

            if (jsonReader.Read() == false || jsonReader.TokenType == JsonToken.EndObject)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.PropertyName)
            {
                throw new InvalidDataException("PropertyName was expected");
            }
            if (Equals("Attachments", jsonReader.Value) == false)
            {
                throw new InvalidDataException("Attachment property was expected");
            }
            if (jsonReader.Read() == false)
            {
                return;
            }
            if (jsonReader.TokenType != JsonToken.StartArray)
            {
                throw new InvalidDataException("StartArray was expected");
            }
            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray)
            {
                attachmentCount += 1;
                var item = RavenJToken.ReadFrom(jsonReader);
                if ((options.OperateOnTypes & ItemType.Attachments) != ItemType.Attachments)
                {
                    continue;
                }
                var attachmentExportInfo =
                    new JsonSerializer
                {
                    Converters = { new TrivialJsonToJsonJsonConverter() }
                }.Deserialize <AttachmentExportInfo>(new RavenJTokenReader(item));
                ShowProgress("Importing attachment {0}", attachmentExportInfo.Key);

                PutAttachment(attachmentExportInfo);
            }
            ShowProgress("Imported {0:#,#;;0} documents and {1:#,#;;0} attachments in {2:#,#;;0} ms", totalCount, attachmentCount, sw.ElapsedMilliseconds);
        }
Ejemplo n.º 17
0
        //public static IEnumerable<T> DeserializeFromColumnsAndDataJson<T>(JSONObject json)
        //where T : DataEntry
        //{
        //	DataEntry.< DeserializeFromColumnsAndDataJson > c__Iterator0 < T > variable = null;
        //	return variable;
        //}

        public static void DeserializeFromColumnsAndDataJsonAsync <T>(JsonTextReader reader, Action <T> objectCallback, Action onFinish)
            where T : DataEntry
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (objectCallback == null)
            {
                throw new ArgumentNullException("objectCallback");
            }
            if (!reader.Read())
            {
                throw new JsonException("JSON is finished unexpectedly");
            }
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new JsonException(string.Format("Couldn't find {0} token", JsonToken.StartObject));
            }
            DataEntry.DataEntryColumn[] synchronizableColumns = DataEntry.GetSynchronizableColumns <T>();
            if (!reader.Read())
            {
                throw new JsonException("JSON is finished unexpectedly");
            }
            if (reader.TokenType == JsonToken.PropertyName)
            {
                if ((reader.Value as string ?? string.Empty).ToUpperInvariant() == "COLUMNS")
                {
                    if (!reader.Read())
                    {
                        throw new JsonException("JSON is finished unexpectedly");
                    }
                    string[] strArrays = DataEntry.ParseStringsArray(reader);
                    DataEntry.DataEntryColumn dataEntryColumn = synchronizableColumns.FirstOrDefault <DataEntry.DataEntryColumn>((DataEntry.DataEntryColumn c) => (c.SynchronizableAttribute == null || c.DataColumnAttribute == null ? false : c.DataColumnAttribute.IsPrimaryKey));
                    Dictionary <int, int>     nums            = new Dictionary <int, int>();
                    int num = -1;
                    for (int i = 0; i < (int)strArrays.Length; i++)
                    {
                        int num1 = 0;
                        while (num1 < (int)synchronizableColumns.Length)
                        {
                            if (string.Equals(strArrays[i], synchronizableColumns[num1].SynchronizableName, StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (synchronizableColumns[num1].DataColumnAttribute != null && synchronizableColumns[num1].DataColumnAttribute.IsPrimaryKey)
                                {
                                    num = i;
                                }
                                nums.Add(i, num1);
                                break;
                            }
                            else
                            {
                                num1++;
                            }
                        }
                    }
                    //bool flag = (num != -1 ? false : ReflectionUtilities.IsPrimaryKeyAutoincrement<T>());
                    bool flag = true;
                    if (dataEntryColumn == null && !flag)
                    {
                        throw new InvalidOperationException(string.Format("Couldn't find a synchronizable primary key for type {0}", typeof(T).Name));
                    }
                    if (num == -1 && !flag)
                    {
                        throw new InvalidOperationException(string.Format("Couldn't find a primary key field in JSON for type {0}", typeof(T).Name));
                    }
                    if (!reader.Read())
                    {
                        throw new JsonException("JSON is finished unexpectedly");
                    }
                    if (reader.TokenType == JsonToken.PropertyName)
                    {
                        if ((reader.Value as string ?? string.Empty).ToUpperInvariant() == "DATA")
                        {
                            if (!reader.Read())
                            {
                                throw new JsonException("JSON is finished unexpectedly");
                            }
                            if (reader.TokenType != JsonToken.StartArray)
                            {
                                throw new JsonException(string.Format("Token {0} expected", JsonToken.StartArray));
                            }
                            if (!reader.Read())
                            {
                                throw new JsonException("JSON is finished unexpectedly");
                            }
                            while (reader.TokenType != JsonToken.EndArray)
                            {
                                string[] strArrays1 = DataEntry.ParseStringsArray(reader);
                                int      num2       = (!flag ? strArrays1[num].ParseInt() : DataEntry.GetAvailableId <T>());
                                T        instance   = DataEntry.GetInstance <T>(num2);
                                for (int j = 0; j < (int)strArrays.Length; j++)
                                {
                                    if (j != num && nums.ContainsKey(j))
                                    {
                                        if (synchronizableColumns[nums[j]].SynchronizableAttribute.Pull)
                                        {
                                            try
                                            {
                                                synchronizableColumns[nums[j]].SetValue(instance, strArrays1[j]);
                                            }
                                            catch (FormatException formatException1)
                                            {
                                                FormatException formatException = formatException1;
                                                throw new SynchronizationException(string.Format("Failed to fill synchronizable property '{0}' with value '{1}' for {2} with ID {3}. Exception: {4}", new object[] { strArrays[j], strArrays1[j], typeof(T).Name, num2, formatException }));
                                            }
                                        }
                                    }
                                }
                                objectCallback(instance);
                                if (reader.Read())
                                {
                                    continue;
                                }
                                throw new JsonException("JSON is finished unexpectedly");
                            }
                            if (onFinish != null)
                            {
                                onFinish();
                            }
                            return;
                        }
                    }
                    throw new JsonException(string.Format("Token {0} not found", "DATA"));
                }
            }
            throw new JsonException(string.Format("Token {0} not found", "COLUMNS"));
        }
Ejemplo n.º 18
0
        public Project ReadProject(Stream stream, string projectName, string projectPath, ProjectReaderSettings settings = null)
        {
            settings = settings ?? new ProjectReaderSettings();
            var project = new Project();

            var     reader = new StreamReader(stream);
            JObject rawProject;

            using (var jsonReader = new JsonTextReader(reader))
            {
                rawProject = JObject.Load(jsonReader);

                // Try to read another token to ensure we're at the end of the document.
                // This will no-op if we are, and throw a JsonReaderException if there is additional content (which is what we want)
                jsonReader.Read();
            }

            if (rawProject == null)
            {
                throw FileFormatException.Create(
                          "The JSON file can't be deserialized to a JSON object.",
                          projectPath);
            }

            // Meta-data properties
            project.Name            = rawProject.Value <string>("name") ?? projectName;
            project.ProjectFilePath = Path.GetFullPath(projectPath);

            var version = rawProject.Value <string>("version");

            if (version == null)
            {
                project.Version = new NuGetVersion("1.0.0");
            }
            else
            {
                try
                {
                    var buildVersion = settings.VersionSuffix;
                    project.Version = SpecifySnapshot(version, buildVersion);
                }
                catch (Exception ex)
                {
                    throw FileFormatException.Create(ex, version, project.ProjectFilePath);
                }
            }

            var fileVersion = settings.AssemblyFileVersion;

            if (string.IsNullOrWhiteSpace(fileVersion))
            {
                project.AssemblyFileVersion = project.Version.Version;
            }
            else
            {
                try
                {
                    var simpleVersion = project.Version.Version;
                    project.AssemblyFileVersion = new Version(simpleVersion.Major,
                                                              simpleVersion.Minor,
                                                              simpleVersion.Build,
                                                              int.Parse(fileVersion));
                }
                catch (FormatException ex)
                {
                    throw new FormatException("The assembly file version is invalid: " + fileVersion, ex);
                }
            }

            project.Description = rawProject.Value <string>("description");
            project.Copyright   = rawProject.Value <string>("copyright");
            project.Title       = rawProject.Value <string>("title");
            project.EntryPoint  = rawProject.Value <string>("entryPoint");
            project.TestRunner  = rawProject.Value <string>("testRunner");
            project.Authors     =
                rawProject.Value <JToken>("authors")?.Values <string>().ToArray() ?? EmptyArray <string> .Value;
            project.Language = rawProject.Value <string>("language");

            // REVIEW: Move this to the dependencies node?
            project.EmbedInteropTypes = rawProject.Value <bool>("embedInteropTypes");

            project.Dependencies = new List <LibraryRange>();
            project.Tools        = new List <LibraryRange>();

            // Project files
            project.Files = new ProjectFilesCollection(rawProject, project.ProjectDirectory, project.ProjectFilePath);
            AddProjectFilesCollectionDiagnostics(rawProject, project);

            var commands = rawProject.Value <JToken>("commands") as JObject;

            if (commands != null)
            {
                foreach (var command in commands)
                {
                    var commandValue = command.Value.Type == JTokenType.String ? command.Value.Value <string>() : null;
                    if (commandValue != null)
                    {
                        project.Commands[command.Key] = commandValue;
                    }
                }
            }

            var scripts = rawProject.Value <JToken>("scripts") as JObject;

            if (scripts != null)
            {
                foreach (var script in scripts)
                {
                    var stringValue = script.Value.Type == JTokenType.String ? script.Value.Value <string>() : null;
                    if (stringValue != null)
                    {
                        project.Scripts[script.Key] = new string[] { stringValue };
                        continue;
                    }

                    var arrayValue =
                        script.Value.Type == JTokenType.Array ? script.Value.Values <string>().ToArray() : null;
                    if (arrayValue != null)
                    {
                        project.Scripts[script.Key] = arrayValue;
                        continue;
                    }

                    throw FileFormatException.Create(
                              string.Format("The value of a script in {0} can only be a string or an array of strings", Project.FileName),
                              script.Value,
                              project.ProjectFilePath);
                }
            }

            project.PackOptions    = GetPackOptions(rawProject, project) ?? new PackOptions();
            project.RuntimeOptions = GetRuntimeOptions(rawProject) ?? new RuntimeOptions();
            project.PublishOptions = GetPublishInclude(rawProject, project);

            BuildTargetFrameworksAndConfigurations(project, rawProject);

            PopulateDependencies(
                project.ProjectFilePath,
                project.Dependencies,
                rawProject,
                "dependencies",
                isGacOrFrameworkReference: false);

            PopulateDependencies(
                project.ProjectFilePath,
                project.Tools,
                rawProject,
                "tools",
                isGacOrFrameworkReference: false);

            JToken runtimeOptionsToken;

            if (rawProject.TryGetValue("runtimeOptions", out runtimeOptionsToken))
            {
                var runtimeOptions = runtimeOptionsToken as JObject;
                if (runtimeOptions == null)
                {
                    throw FileFormatException.Create("The runtimeOptions must be an object", runtimeOptionsToken);
                }

                project.RawRuntimeOptions = runtimeOptions.ToString();
            }

            return(project);
        }
Ejemplo n.º 19
0
        public void ReadingIndented()
        {
            string input = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    ""500 gigabyte hard drive""
  ]
}";

            StringReader sr = new StringReader(input);

            using (JsonTextReader jsonReader = new JsonTextReader(sr))
            {
                Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
                Assert.AreEqual(0, jsonReader.LineNumber);
                Assert.AreEqual(0, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject);
                Assert.AreEqual(1, jsonReader.LineNumber);
                Assert.AreEqual(1, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
                Assert.AreEqual(jsonReader.Value, "CPU");
                Assert.AreEqual(2, jsonReader.LineNumber);
                Assert.AreEqual(6, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "Intel");
                Assert.AreEqual(2, jsonReader.LineNumber);
                Assert.AreEqual(14, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
                Assert.AreEqual(jsonReader.Value, "Drives");
                Assert.AreEqual(3, jsonReader.LineNumber);
                Assert.AreEqual(9, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray);
                Assert.AreEqual(3, jsonReader.LineNumber);
                Assert.AreEqual(11, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "DVD read/writer");
                Assert.AreEqual(jsonReader.QuoteChar, '\'');
                Assert.AreEqual(4, jsonReader.LineNumber);
                Assert.AreEqual(21, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "500 gigabyte hard drive");
                Assert.AreEqual(jsonReader.QuoteChar, '"');
                Assert.AreEqual(5, jsonReader.LineNumber);
                Assert.AreEqual(29, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray);
                Assert.AreEqual(6, jsonReader.LineNumber);
                Assert.AreEqual(3, jsonReader.LinePosition);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
                Assert.AreEqual(7, jsonReader.LineNumber);
                Assert.AreEqual(1, jsonReader.LinePosition);

                Assert.IsFalse(jsonReader.Read());
            }
        }
Ejemplo n.º 20
0
        public void ReadFloatingPointNumber()
        {
            string json =
                @"[0.0,0.0,0.1,1.0,1.000001,1E-06,4.94065645841247E-324,Infinity,-Infinity,NaN,1.7976931348623157E+308,-1.7976931348623157E+308,Infinity,-Infinity,NaN]";

            using (JsonReader jsonReader = new JsonTextReader(new StringReader(json)))
            {
                jsonReader.Read();
                Assert.AreEqual(JsonToken.StartArray, jsonReader.TokenType);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(0.0, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(0.0, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(0.1, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(1.0, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(1.000001, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(1E-06, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(4.94065645841247E-324, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(double.PositiveInfinity, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(double.NegativeInfinity, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(double.NaN, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(double.MaxValue, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(double.MinValue, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(double.PositiveInfinity, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(double.NegativeInfinity, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Float, jsonReader.TokenType);
                Assert.AreEqual(double.NaN, jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType);
            }
        }
Ejemplo n.º 21
0
        public void SerializeWithEscapedPropertyName()
        {
            string json = JsonConvert.SerializeObject(
                new AddressWithDataMember
                {
                    AddressLine1 = "value!"
                },
                new JsonSerializerSettings
                {
                    ContractResolver = new EscapedPropertiesContractResolver()
                });

            Assert.AreEqual(@"{""AddressLine1-'-\""-"":""value!""}", json);

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

            Assert.AreEqual(@"AddressLine1-'-""-", reader.Value);
        }
Ejemplo n.º 22
0
        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
        }
Ejemplo n.º 23
0
        public void UnexpectedEndOfControlCharacter()
        {
            JsonReader reader = new JsonTextReader(new StringReader(@"'h\"));

            reader.Read();
        }
        /// <summary>
        /// Connect to tradier API stream
        /// </summary>
        /// <param name="symbols">symbol list</param>
        /// <returns></returns>
        private IEnumerable <TradierStreamData> Stream(List <string> symbols)
        {
            bool success;
            var  symbolJoined = String.Join(",", symbols);
            var  session      = CreateStreamSession();

            if (session == null || session.SessionId == null || session.Url == null)
            {
                Log.Error("Tradier.Stream(): Failed to Created Stream Session", true);
                yield break;
            }
            Log.Trace("Tradier.Stream(): Created Stream Session Id: " + session.SessionId + " Url:" + session.Url, true);


            HttpWebRequest request;

            do
            {
                //Connect to URL:
                success = true;
                request = (HttpWebRequest)WebRequest.Create(session.Url);

                //Authenticate a request:
                request.Accept = "application/json";
                request.Headers.Add("Authorization", "Bearer " + _accessToken);

                //Add the desired data:
                var postData = "symbols=" + symbolJoined + "&filter=trade&sessionid=" + session.SessionId;

                var encodedData = Encoding.ASCII.GetBytes(postData);

                //Set post:
                request.Method        = "POST";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = encodedData.Length;

                //Send request:
                try
                {
                    using (var postStream = request.GetRequestStream())
                    {
                        postStream.Write(encodedData, 0, encodedData.Length);
                    }
                }
                catch (Exception err)
                {
                    Log.Error(err, "Failed to write session parameters to URL", true);
                    success = false;
                }
            }while (!success);

            //Get response as a stream:
            Log.Trace("Tradier.Stream(): Session Created, Reading Stream...", true);
            var response = (HttpWebResponse)request.GetResponse();

            _tradierStream = response.GetResponseStream();
            if (_tradierStream == null)
            {
                yield break;
            }

            using (var sr = new StreamReader(_tradierStream))
                using (var jsonReader = new JsonTextReader(sr))
                {
                    var serializer = new JsonSerializer();
                    jsonReader.SupportMultipleContent = true;

                    // keep going until we fail to read more from the stream
                    while (true)
                    {
                        bool successfulRead;
                        try
                        {
                            //Read the jsonSocket in a safe manner: might close and so need handlers, but can't put handlers around a yield.
                            successfulRead = jsonReader.Read();
                        }
                        catch (Exception err)
                        {
                            Log.Trace("TradierBrokerage.DataQueueHandler.Stream(): Handled breakout / socket close from jsonRead operation: " + err.Message);
                            break;
                        }

                        if (!successfulRead)
                        {
                            // if we couldn't get a successful read just end the enumerable
                            yield break;
                        }

                        //Have a Tradier JSON Object:
                        TradierStreamData tsd = null;
                        try
                        {
                            tsd = serializer.Deserialize <TradierStreamData>(jsonReader);
                        }
                        catch (Exception err)
                        {
                            // Do nothing for now. Can come back later to fix. Errors are from Tradier not properly json encoding values E.g. "NaN" string.
                            Log.Trace("TradierBrokerage.DataQueueHandler.Stream(): Handled breakout / socket close from jsonRead operation: " + err.Message);
                        }

                        // don't yield garbage, just wait for the next one
                        if (tsd != null)
                        {
                            yield return(tsd);
                        }
                    }
                }
        }
Ejemplo n.º 25
0
        private void ParseShops(JsonTextReader reader)
        {
            if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
            {
                throw new InvalidOperationException();
            }

            var shops = new List <Tuple <int, int[]> >();

            while (reader.Read() && reader.TokenType != JsonToken.EndArray)
            {
                if (reader.TokenType != JsonToken.StartObject)
                {
                    throw new InvalidOperationException();
                }
                if (!reader.Read() || reader.TokenType != JsonToken.PropertyName)
                {
                    throw new InvalidOperationException();
                }

                var key = Convert.ToInt32(reader.Value);

                var items = new List <int>();
                if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
                {
                    throw new InvalidOperationException();
                }
                while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                {
                    if (reader.TokenType != JsonToken.StartObject)
                    {
                        throw new InvalidOperationException();
                    }
                    if (!reader.Read() || reader.TokenType != JsonToken.PropertyName)
                    {
                        throw new InvalidOperationException();
                    }

                    var itemKey = Convert.ToInt32(reader.Value);

                    if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
                    {
                        throw new InvalidOperationException();
                    }
                    while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                    {
                    }

                    items.Add(itemKey);

                    if (!reader.Read() || reader.TokenType != JsonToken.EndObject)
                    {
                        throw new InvalidOperationException();
                    }
                }

                shops.Add(Tuple.Create(key, items.ToArray()));

                if (!reader.Read() || reader.TokenType != JsonToken.EndObject)
                {
                    throw new InvalidOperationException();
                }
            }

            _Shops = shops.ToArray();
        }
Ejemplo n.º 26
0
        public override void Execute()
        {
            if (AcceptAllSSLCertificate == true)
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(
                    delegate
                {
                    return(true);
                }
                    );
            }


            switch (SecurityType)
            {
            case eSercurityType.None:

                break;

            case eSercurityType.Ssl3:
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                break;

            case eSercurityType.Tls:
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                break;

            case eSercurityType.Tls11:
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
                break;

            case eSercurityType.Tls12:
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                break;

            default:
                throw new NotSupportedException("The Configured security type is not supported");
            }


            try
            {
                string strURL = EndPointURL.ValueForDriver;

                HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(strURL);
                SetHTTPHeaders(WebReq);
                //Nathan added customizable Network Credentials
                NetworkCredential CustCreds = null;
                if (URLDomain.Value != "" && URLDomain.Value != null)
                {
                    CustCreds          = new NetworkCredential("", "", "");
                    CustCreds.UserName = URLUser.Value;
                    CustCreds.Password = URLPass.Value;
                    CustCreds.Domain   = URLDomain.Value;
                }
                else if (URLUser.Value != "" && URLUser.Value != null) //use current domain
                {
                    CustCreds          = new NetworkCredential("", "", "");
                    CustCreds.UserName = URLUser.Value;
                    CustCreds.Password = URLPass.Value;
                }

                if (CustCreds == null)
                {
                    WebReq.UseDefaultCredentials = true;
                    WebReq.Credentials           = System.Net.CredentialCache.DefaultNetworkCredentials;
                }
                else
                {
                    //Nathan - customized for Swapna ATT D2 Architechture to Pass Network Credentials
                    WebReq.UseDefaultCredentials = false;
                    WebReq.Credentials           = CustCreds;
                    //End Nathan - customized for Swapna ATT D2 Architechture
                }

                //Method type
                WebReq.Method = RequestType.ToString();

                // POSTRequest.ContentType = "text/xml";
                WebReq.ContentType = General.GetEnumValueDescription(typeof(eContentType), ContentType);
                WebReq.KeepAlive   = false;
                if (this.Timeout != null)
                {
                    WebReq.Timeout = (int)this.Timeout * 1000;
                }
                else
                {
                    WebReq.Timeout = int.MaxValue;
                }

                byte[] dataByte = null;
                if (!string.IsNullOrEmpty(RequestBody.ValueForDriver))
                {
                    dataByte = GetBody();
                }
                else if (!string.IsNullOrEmpty(TemplateFile.ValueForDriver))
                {
                    dataByte = GetBodyFromFile();
                }

                switch (CookieMode) //moved
                {
                case eCookieMode.Session:

                    WebReq.CookieContainer = new CookieContainer();
                    foreach (Cookie cooki in _sessionCokkiesDic.Values)
                    {
                        Uri    domainName = new Uri(EndPointURL.ValueForDriver);
                        Cookie ck         = new Cookie();
                        ck.Name  = cooki.Name;
                        ck.Value = cooki.Value;
                        if (String.IsNullOrEmpty(cooki.Domain) || true)
                        {
                            cooki.Domain = null;
                        }
                        WebReq.CookieContainer.Add(domainName, ck);
                    }
                    break;

                case eCookieMode.None:

                    break;

                case eCookieMode.New:
                    _sessionCokkiesDic.Clear();
                    break;

                default:
                    throw new NotSupportedException("Configured mode is not supported");
                }

                //Not sending data on a get request
                if (RequestType != eRequestType.GET)
                {
                    if (ContentType == eContentType.JSon)
                    {
                        WebReq.ContentType = "application/json";
                    }

                    if (RequestType == eRequestType.PATCH)
                    {
                        WebReq.Method = WebRequestMethods.Http.Post;
                        WebReq.Headers.Add("X-Http-Method-Override", "PATCH");
                    }



                    string req = HttpUtility.UrlEncode(RequestBody.Value);
                    if (ReqHttpVersion == eHttpVersion.HTTPV10)
                    {
                        WebReq.ProtocolVersion = HttpVersion.Version10;
                    }
                    else
                    {
                        WebReq.ProtocolVersion = HttpVersion.Version11;
                    }
                    WebReq.ContentLength = dataByte.Length;

                    Stream Webstream = WebReq.GetRequestStream();
                    Webstream.Write(dataByte, 0, dataByte.Length);
                    Webstream.Close();
                }
                // Write the data bytes in the request stream


                //Get response from server
                try
                {
                    WebReqResponse = (HttpWebResponse)WebReq.GetResponse();

                    for (int i = 0; i < WebReqResponse.Headers.Count; i++)
                    {
                        AddOrUpdateReturnParamActual("Header: " + WebReqResponse.Headers.Keys[i], WebReqResponse.Headers[i]);
                    }

                    AddOrUpdateReturnParamActual("Header: Status Code ", WebReqResponse.StatusDescription);
                }
                catch (WebException WE)
                {
                    WebReqResponse = (HttpWebResponse)WE.Response;

                    this.ExInfo = WE.Message;
                    if (DoNotFailActionOnBadRespose != true)
                    {
                        base.Status = Amdocs.Ginger.CoreNET.Execution.eRunStatus.Failed;
                        base.Error  = WE.Message;
                    }
                }
                if (CookieMode != eCookieMode.None)
                {
                    foreach (Cookie cooki in WebReqResponse.Cookies)
                    {
                        if (_sessionCokkiesDic.Keys.Contains(cooki.Name) == false)
                        {
                            _sessionCokkiesDic.Add(cooki.Name, cooki);
                        }
                        else
                        {
                            _sessionCokkiesDic[cooki.Name] = cooki;
                        }
                    }

                    for (int k = 0; k < WebReqResponse.Headers.Count; k++)
                    {
                        if (WebReqResponse.Headers.Keys[k] == "Set-Cookie")
                        {
                            foreach (string httpCookie in  WebReqResponse.Headers.GetValues(k))
                            {
                                String[] cookiearray  = httpCookie.Split(new char[] { ';' }, 3);
                                String[] cookiearray2 = cookiearray[0].Split(new char[] { '=' }, 2);

                                Cookie cks = new Cookie();
                                cks.Name  = cookiearray2[0];
                                cks.Value = cookiearray2[1];
                                cks.Path  = cookiearray[1].Split(new char[] { '=' }, 2)[1];

                                if (cookiearray.Length == 3)
                                {
                                    if (cookiearray[2].Contains("HttpOnly"))
                                    {
                                        cks.HttpOnly = true;
                                    }
                                }
                                if (cks.Path.Length > 1)
                                {
                                    if (cks.Path.StartsWith("."))
                                    {
                                        Uri domainName = new Uri("http://" + cks.Path.Substring(1));
                                        cks.Domain = domainName.Host;
                                    }
                                    else
                                    {
                                        Uri domainName = null;

                                        if (cks.Path.StartsWith("http://") || cks.Path.StartsWith("https://"))
                                        {
                                            domainName = new Uri(cks.Path);
                                        }

                                        else
                                        {
                                            domainName = new Uri("http://" + cks.Path);
                                        }



                                        cks.Domain = domainName.Host;
                                    }
                                }
                                else
                                {
                                    Uri domainName = new Uri(EndPointURL.ValueForDriver);
                                    cks.Domain = domainName.Host;
                                }
                                if (cks.Path.StartsWith("."))
                                {
                                    cks.Path = cks.Path.Substring(1);
                                }
                                try
                                {
                                    _sessionCokkiesDic.Remove(cks.Name);
                                }
                                finally
                                {
                                    _sessionCokkiesDic.Add(cks.Name, cks);
                                }
                            }
                        }
                    }
                }

                string resp = string.Empty;
                byte[] data = new byte[0];
                if (ResponseContentType != eContentType.PDF)
                {
                    //TODO: check if UTF8 is good for all
                    StreamReader reader = new StreamReader(WebReqResponse.GetResponseStream(), Encoding.UTF8);
                    Reporter.ToLog(eLogLevel.DEBUG, "Response");

                    resp = reader.ReadToEnd();
                    Reporter.ToLog(eLogLevel.DEBUG, resp);
                    reader.Close();
                }
                else
                {
                    MemoryStream memoryStream = new MemoryStream();
                    WebResponse  webResponse  = WebReq.GetResponse();

                    webResponse.GetResponseStream().CopyTo(memoryStream);

                    data = memoryStream.ToArray();
                    memoryStream.Close();
                }


                if (RestRequestSave == true && RequestType != eRequestType.GET)
                {
                    string fileName = createRequestOrResponseXMLInFolder("Request", ReqBody, ContentType);
                    AddOrUpdateReturnParamActual("Saved Request File Name", fileName);
                }
                if (RestResponseSave == true)
                {
                    if (ResponseContentType != eContentType.PDF)
                    {
                        string fileName = createRequestOrResponseXMLInFolder("Response", resp, ResponseContentType);
                        AddOrUpdateReturnParamActual("Saved Response File Name", fileName);
                    }
                    else
                    {
                        CreatePdfFile(data);
                    }
                }


                AddOrUpdateReturnParamActual("Respose", resp);
                if (String.IsNullOrEmpty(resp))
                {
                    return;
                }
                XmlDocument doc = null;
                if (ResponseContentType == eContentType.JSon)
                {
                    if (UseLegacyJSONParsing)
                    {
                        JsonTextReader Jreader = new JsonTextReader(new StringReader(resp));
                        while (Jreader.Read())
                        {
                            if (Jreader.Value != null)
                            {
                                Console.WriteLine("Token: {0}, Value: {1}", Jreader.TokenType, Jreader.Value);
                            }
                            else
                            {
                                Console.WriteLine("Token: {0}", Jreader.TokenType);
                            }
                        }


                        if (((resp[0] == '[') && (resp[resp.Length - 1] == ']')))
                        {
                            doc = Newtonsoft.Json.JsonConvert.DeserializeXmlNode("{\"root\":" + resp + "}", "root");
                        }
                        else
                        {
                            doc = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(resp, "root");
                        }


                        List <Amdocs.Ginger.Common.GeneralLib.General.XmlNodeItem> outputTagsList = new List <Amdocs.Ginger.Common.GeneralLib.General.XmlNodeItem>();
                        outputTagsList = Amdocs.Ginger.Common.GeneralLib.General.GetXMLNodesItems(doc);
                        foreach (Amdocs.Ginger.Common.GeneralLib.General.XmlNodeItem outputItem in outputTagsList)
                        {
                            this.AddOrUpdateReturnParamActualWithPath(outputItem.param, outputItem.value, outputItem.path);
                        }
                    }
                    else
                    {
                        try
                        {
                            this.ParseJSONToOutputValues(resp, 1);
                        }
                        catch
                        {
                            this.ParseJSONToOutputValues(General.CorrectJSON(resp), 1);
                        }
                    }
                    // Console.WriteLine(doc.);
                }

                else if (ResponseContentType == eContentType.XML)
                {
                    doc = new XmlDocument();
                    doc.LoadXml(resp);

                    List <Amdocs.Ginger.Common.GeneralLib.General.XmlNodeItem> outputTagsList = new List <Amdocs.Ginger.Common.GeneralLib.General.XmlNodeItem>();
                    outputTagsList = Amdocs.Ginger.Common.GeneralLib.General.GetXMLNodesItems(doc);
                    foreach (Amdocs.Ginger.Common.GeneralLib.General.XmlNodeItem outputItem in outputTagsList)
                    {
                        this.AddOrUpdateReturnParamActualWithPath(outputItem.param, outputItem.value, outputItem.path);
                    }
                }
                //    XMLProcessor x = new XMLProcessor();
                // string s = x
                // x.ParseToReturnValues(s, this);
            }
            catch (WebException WEx)
            {
                this.ExInfo = WEx.Message;
                base.Status = Amdocs.Ginger.CoreNET.Execution.eRunStatus.Failed;
                base.Error  = WEx.Message;
            }
        }
Ejemplo n.º 27
0
        public async Task <IFileSystemNode> AddAsync(Stream stream, string name = "", AddFileOptions options = null, CancellationToken cancel = default(CancellationToken))
        {
            if (options == null)
            {
                options = new AddFileOptions();
            }
            var opts = new List <string>();

            if (!options.Pin)
            {
                opts.Add("pin=false");
            }
            if (options.Wrap)
            {
                opts.Add("wrap-with-directory=true");
            }
            if (options.RawLeaves)
            {
                opts.Add("raw-leaves=true");
            }
            if (options.OnlyHash)
            {
                opts.Add("only-hash=true");
            }
            if (options.Trickle)
            {
                opts.Add("trickle=true");
            }
            if (options.Hash != MultiHash.DefaultAlgorithmName)
            {
                opts.Add($"hash=${options.Hash}");
            }
            if (options.Encoding != MultiBase.DefaultAlgorithmName)
            {
                opts.Add($"cid-base=${options.Encoding}");
            }
            opts.Add($"chunker=size-{options.ChunkSize}");

            var json = await ipfs.UploadAsync("add", cancel, stream, name, opts.ToArray());

            // The result is a stream of LDJSON objects.
            // See https://github.com/ipfs/go-ipfs/issues/4852
            FileSystemNode fsn = null;

            using (var sr = new StringReader(json))
                using (var jr = new JsonTextReader(sr)
                {
                    SupportMultipleContent = true
                })
                {
                    while (jr.Read())
                    {
                        var r = await JObject.LoadAsync(jr, cancel);

                        fsn = new FileSystemNode
                        {
                            Id          = (string)r["Hash"],
                            Size        = long.Parse((string)r["Size"]),
                            IsDirectory = false,
                            Name        = name,
                            IpfsClient  = ipfs
                        };
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("added " + fsn.Id + " " + fsn.Name);
                        }
                    }
                }

            fsn.IsDirectory = options.Wrap;
            return(fsn);
        }
        public void ReadDollarQuotePropertyWithTag2()
        {
            string json = @"{`pp`Name'Of""Store`pp`:`tag`Forest's Bakery And Cafe`tag`}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            Assert.AreEqual(@"Name'Of""Store", jsonTextReader.Value);
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }
Ejemplo n.º 29
0
        static void Main1(string[] args)
        {
            ///   GCSettings.LatencyMode = GCLatencyMode.LowLatency;

            /// System.GC.TryStartNoGCRegion(100 * 1024 * 1024);

            var pool    = new JsonArrayPool();
            int maxSize = 8 * 1024 * 1024;

            for (int i = 0; i < 10; i++)
            {
                pool.Return(pool.Rent(maxSize));
                maxSize = maxSize / 2;
            }

            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("Total Used " + JsonArrayPool.totalUsed);
                ///   JsonArrayPool.values.ForEach(s => Console.WriteLine(s));

                if (i % 2 == 0)
                {
                    Thread.Sleep(1000);
                }
                String path = @"C:\Users\kibandi\Desktop\700KExchangeOutput.txt";

                using (StreamReader sr = File.OpenText(path))
                    using (var jsonTextReader = new JsonTextReader(sr))
                    {
                        // Checking if pooling will help with memory
                        ////jsonTextReader.ArrayPool = pool;

                        while (true)
                        {
                            while (jsonTextReader.Read())
                            {
                                // As per Rest/Exchange Contract, we look at Value tag to extract data.
                                // This is similar to logic in PageProcessor Except that we look at each chunk of data from the stream to determine the extraction step.
                                if (jsonTextReader.TokenType == JsonToken.PropertyName &&
                                    ((string)jsonTextReader.Value).Equals("value"))
                                {
                                    isMessageExtractionStarted = true;
                                    jsonTextReader.Read();

                                    // This triggers the start of Each Message Extraction.
                                    // Even in this scenario, do not load parse the whole content. Load one object from the array of Content in the stream.
                                    // To Identify a single Object/Message is extracted, the JsonTextReader might need to load additional Bytes from the stream than what was needed.
                                    // But this is fine and unavoidable. The additional Data read will be stored in The Reader and used when the next message processing takes place.
                                    if (jsonTextReader.TokenType == JsonToken.StartArray)
                                    {
                                        while (ExtractTokenMessage(jsonTextReader))
                                        {
                                            Console.WriteLine("One Message Extracted");
                                        }
                                    }
                                }

                                // Logic to Parse & Extract the Next Link in the request processing.
                                // Its not necessary that this extraction happens before the extraction of Messages.
                                else if (jsonTextReader.TokenType == JsonToken.PropertyName &&
                                         ((string)jsonTextReader.Value).Equals("@odata.nextLink"))
                                {
                                    jsonTextReader.Read();
                                }
                            }

                            break;
                        }
                    }
            }

            ///  System.GC.EndNoGCRegion();

            Console.ReadKey();
        }
Ejemplo n.º 30
0
        public void UnexpectedEndOfString()
        {
            JsonReader reader = new JsonTextReader(new StringReader("'hi"));

            reader.Read();
        }
        private async Task GetGistsAsync()
        {
            using (var client = new HttpClient())
            {
                try
                {
                    AllGists = new Dictionary <string, string>();
                    HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, gists_url);
                    // Add our custom headers https://api.github.com/users/nirav/followers
                    requestMessage.Headers.Add("User-Agent", "User-Agent-Here");
                    requestMessage.Headers.Add("Authorization", "token e74f57caee7c8c22ff71d49590f63ee66b8921ad");
                    HttpResponseMessage response = await client.SendAsync(requestMessage);

                    response.EnsureSuccessStatusCode();
                    var            Result       = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
                    var            stringResult = Result.ToString();
                    JsonTextReader textReader   = new JsonTextReader(new StringReader(stringResult));
                    while (textReader.Read())
                    {
                        if (textReader.Value != null)
                        {
                            if (textReader.Value.Equals("git_push_url"))
                            {
                                ReadData(textReader, 3);
                                string namedescription = "", url = "";
                                url = textReader.Value.ToString();
                                while (textReader.Read())
                                {
                                    if (textReader.Value != null)
                                    {
                                        if (textReader.Value.Equals("filename"))
                                        {
                                            ReadData(textReader, 1);
                                            if (textReader.Value != null)
                                            {
                                                namedescription = textReader.Value.ToString();
                                            }
                                            while (textReader.Read())
                                            {
                                                if (textReader.Value != null)
                                                {
                                                    if (textReader.Value.Equals("description"))
                                                    {
                                                        ReadData(textReader, 1);
                                                        if (textReader.Value != null)
                                                        {
                                                            namedescription = namedescription + "&&&" + textReader.Value.ToString();
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }

                                /*if (String.IsNullOrEmpty(name))
                                 *  continue;*/
                                AllGists.Add(url, namedescription);
                            }
                        }
                    }
                    return;
                }
                catch (HttpRequestException httpRequestException)
                {
                    return;// "Can't reach GitHub. check your internet connection.";
                }
            }
        }
 public bool Read()
 {
     return(_jsonTextReader.Read());
 }
 private static void ReadToken(JsonTextReader reader, JsonToken token)
 {
     if (!reader.Read() || reader.TokenType != token)
         throw new HttpException("Invalid request");
 }
Ejemplo n.º 34
0
        public void TestDecodeNamedEntities()
        {
            var path    = Path.Combine(TestHelper.ProjectDir, "TestData", "html", "HtmlEntities.json");
            var decoder = new HtmlEntityDecoder();

            using (var json = new JsonTextReader(new StreamReader(path))) {
                while (json.Read())
                {
                    string name, value;

                    if (json.TokenType == JsonToken.StartObject)
                    {
                        continue;
                    }

                    if (json.TokenType != JsonToken.PropertyName)
                    {
                        break;
                    }

                    name = (string)json.Value;

                    if (!json.Read() || json.TokenType != JsonToken.StartObject)
                    {
                        break;
                    }

                    // read to the "codepoints" property
                    if (!json.Read() || json.TokenType != JsonToken.PropertyName)
                    {
                        break;
                    }

                    // skip the array of integers...
                    if (!json.Read() || json.TokenType != JsonToken.StartArray)
                    {
                        break;
                    }

                    while (json.Read())
                    {
                        if (json.TokenType == JsonToken.EndArray)
                        {
                            break;
                        }
                    }

                    // the property should be "characters" - this is what we want
                    if (!json.Read() || json.TokenType != JsonToken.PropertyName)
                    {
                        break;
                    }

                    value = json.ReadAsString();

                    if (!json.Read() || json.TokenType != JsonToken.EndObject)
                    {
                        break;
                    }

                    for (int i = 0; i < name.Length; i++)
                    {
                        Assert.IsTrue(decoder.Push(name[i]), "Failed to push char #{0} of \"{1}\".", i, name);
                    }

                    Assert.AreEqual(value, decoder.GetValue(), "Decoded entity did not match for \"{0}\".", name);

                    decoder.Reset();
                }
            }
        }
        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);
        }
Ejemplo n.º 36
0
    public void LoadFromNestedObjectIncomplete()
    {
      string jsonText = @"{
  ""short"":
  {
    ""error"":
    {
      ""code"":0";

      JsonReader reader = new JsonTextReader(new StringReader(jsonText));
      reader.Read();
      reader.Read();
      reader.Read();
      reader.Read();
      reader.Read();

      JToken.ReadFrom(reader);
    }
Ejemplo n.º 37
0
        private static object DeserializeMessage(JsonTextReader reader, ServiceType type, bool skipRead)
        {
            Require.NotNull(reader, "reader");
            Require.NotNull(type, "type");

            if (!skipRead && !reader.Read())
                throw new HttpException("Invalid request");

            if (reader.TokenType == JsonToken.Null)
                return null;

            object instance = Activator.CreateInstance(type.Type);

            if (reader.TokenType != JsonToken.StartObject)
                throw new HttpException("Invalid request");

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                    break;
                else if (reader.TokenType != JsonToken.PropertyName)
                    throw new HttpException("Invalid request");

                int tag;

                if (!int.TryParse((string)reader.Value, NumberStyles.None, CultureInfo.InvariantCulture, out tag))
                    throw new HttpException("Invalid request");

                ServiceTypeField field;

                if (!type.Fields.TryGetValue(tag, out field))
                    throw new HttpException("Unknown tag");

                if (field.CollectionType != null)
                {
                    var collection = new ArrayList();

                    if (!reader.Read())
                        throw new HttpException("Invalid request");

                    if (reader.TokenType == JsonToken.Null)
                        continue;
                    if (reader.TokenType != JsonToken.StartArray)
                        throw new HttpException("Invalid request");

                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.EndArray)
                            break;

                        if (field.ServiceType != null && !field.ServiceType.Type.IsEnum)
                            collection.Add(DeserializeMessage(reader, field.ServiceType, true));
                        else
                            collection.Add(Convert.ChangeType(reader.Value, field.Type));
                    }

                    if (reader.TokenType != JsonToken.EndArray)
                        throw new HttpException("Invalid request");

                    var targetCollection = (IList)Activator.CreateInstance(field.CollectionType, new object[] { collection.Count });

                    for (int i = 0; i < collection.Count; i++)
                    {
                        if (field.CollectionType.IsArray)
                            targetCollection[i] = collection[i];
                        else
                            targetCollection.Add(collection[i]);
                    }

                    field.Setter(instance, targetCollection);
                }
                else if (field.ServiceType != null && !field.ServiceType.Type.IsEnum)
                {
                    if (!reader.Read())
                        throw new HttpException("Invalid request");

                    field.Setter(instance, DeserializeMessage(reader, field.ServiceType, true));
                }
                else
                {
                    if (!reader.Read())
                        throw new HttpException("Invalid request");

                    field.Setter(instance, reader.Value);
                }
            }

            if (reader.TokenType != JsonToken.EndObject)
                throw new HttpException("Invalid request");

            return instance;
        }
Ejemplo n.º 38
0
        public void StartingEndArrayAndReadFrom()
        {
            StringReader textReader = new StringReader(@"[]");

            JsonTextReader jsonReader = new JsonTextReader(textReader);
            jsonReader.Read();
            jsonReader.Read();

            ExceptionAssert.Throws<JsonReaderException>(@"Error reading JToken from JsonReader. Unexpected token: EndArray. Path '', line 1, position 2.",
                () => JToken.ReadFrom(jsonReader));
        }
        public void ReadDollarQuotePropertyWithTag()
        {
            string json = @"{$pp$Name'Of""Store$pp$:$tag$Forest's Bakery And Cafe$tag$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            Assert.AreEqual(@"Name'Of""Store", jsonTextReader.Value);
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }
Ejemplo n.º 40
0
        public void UnexpectedEndOfHex()
        {
            JsonReader reader = new JsonTextReader(new StringReader(@"'h\u006"));

            reader.Read();
        }
Ejemplo n.º 41
0
        public void ReadWithSupportMultipleContent()
        {
            string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";

            IList<JObject> roles = new List<JObject>();

            JsonTextReader reader = new JsonTextReader(new StringReader(json));
            reader.SupportMultipleContent = true;

            while (true)
            {
                JObject role = (JObject)JToken.ReadFrom(reader);

                roles.Add(role);

                if (!reader.Read())
                    break;
            }

            Assert.AreEqual(2, roles.Count);
            Assert.AreEqual("Admin", (string)roles[0]["name"]);
            Assert.AreEqual("Publisher", (string)roles[1]["name"]);
        }
        public async Task ImportAsync(Stream inputStream, ExportImportOptions options, Action <ExportImportProgressInfo> progressCallback,
                                      ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var progressInfo          = new ExportImportProgressInfo();
            var currencyTotalCount    = 0;
            var packageTypeTotalCount = 0;

            using (var streamReader = new StreamReader(inputStream))
                using (var reader = new JsonTextReader(streamReader))
                {
                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.PropertyName)
                        {
                            if (reader.Value.ToString() == "CurrencyTotalCount")
                            {
                                currencyTotalCount = reader.ReadAsInt32() ?? 0;
                            }
                            else if (reader.Value.ToString() == "Currencies")
                            {
                                reader.Read();
                                if (reader.TokenType == JsonToken.StartArray)
                                {
                                    reader.Read();

                                    var currencies    = new List <Currency>();
                                    var currencyCount = 0;

                                    while (reader.TokenType != JsonToken.EndArray)
                                    {
                                        var currency = _serializer.Deserialize <Currency>(reader);
                                        currencies.Add(currency);
                                        currencyCount++;

                                        reader.Read();
                                    }
                                    cancellationToken.ThrowIfCancellationRequested();

                                    if (currencyCount % _batchSize == 0 || reader.TokenType == JsonToken.EndArray)
                                    {
                                        await _currencyService.SaveChangesAsync(currencies.ToArray());

                                        currencies.Clear();

                                        progressInfo.Description = $"{ currencyCount } Currencies imported";

                                        progressCallback(progressInfo);
                                    }
                                }
                            }
                            else if (reader.Value.ToString() == "PackageTypeTotalCount")
                            {
                                packageTypeTotalCount = reader.ReadAsInt32() ?? 0;
                            }
                            else if (reader.Value.ToString() == "PackageTypes")
                            {
                                reader.Read();
                                if (reader.TokenType == JsonToken.StartArray)
                                {
                                    reader.Read();

                                    var packageTypes     = new List <PackageType>();
                                    var packageTypeCount = 0;

                                    while (reader.TokenType != JsonToken.EndArray)
                                    {
                                        var currency = _serializer.Deserialize <PackageType>(reader);
                                        packageTypes.Add(currency);
                                        packageTypeCount++;

                                        reader.Read();
                                    }
                                    cancellationToken.ThrowIfCancellationRequested();

                                    if (packageTypeCount % _batchSize == 0 || reader.TokenType == JsonToken.EndArray)
                                    {
                                        await _packageTypesService.SaveChangesAsync(packageTypes.ToArray());

                                        packageTypes.Clear();

                                        progressInfo.Description = $"{ packageTypeCount } Package Types imported";

                                        progressCallback(progressInfo);
                                    }
                                }
                            }
                        }
                    }
                }
        }
Ejemplo n.º 43
0
        public static Person ToPerson(this string s)
        {
            StringReader sr = new StringReader(s);
            JsonTextReader reader = new JsonTextReader(sr);

            Person p = new Person();

            // {
            reader.Read();
            // "name"
            reader.Read();
            // "Jerry"
            p.Name = reader.ReadAsString();
            // "likes"
            reader.Read();
            // [
            reader.Read();
            // "Comedy", "Superman", ]
            while (reader.Read() && reader.TokenType != JsonToken.EndArray)
            {
                p.Likes.Add((string)reader.Value);
            }
            // }
            reader.Read();

            return p;
        }
Ejemplo n.º 44
0
        public async Task <string> ExecuteAsync(string executionId, RecipeDescriptor recipeDescriptor, object environment)
        {
            await _recipeEventHandlers.InvokeAsync(x => x.RecipeExecutingAsync(executionId, recipeDescriptor), Logger);

            try
            {
                _environmentMethodProvider = new ParametersMethodProvider(environment);

                var result = new RecipeResult {
                    ExecutionId = executionId
                };

                await _recipeStore.CreateAsync(result);

                using (var stream = recipeDescriptor.RecipeFileInfo.CreateReadStream())
                {
                    using (var file = new StreamReader(stream))
                    {
                        using (var reader = new JsonTextReader(file))
                        {
                            // Go to Steps, then iterate.
                            while (reader.Read())
                            {
                                if (reader.Path == "variables")
                                {
                                    reader.Read();

                                    var variables = JObject.Load(reader);
                                    _variablesMethodProvider = new VariablesMethodProvider(variables);
                                }

                                if (reader.Path == "steps" && reader.TokenType == JsonToken.StartArray)
                                {
                                    while (reader.Read() && reader.Depth > 1)
                                    {
                                        if (reader.Depth == 2)
                                        {
                                            var child = JObject.Load(reader);

                                            var recipeStep = new RecipeExecutionContext
                                            {
                                                Name             = child.Value <string>("name"),
                                                Step             = child,
                                                ExecutionId      = executionId,
                                                Environment      = environment,
                                                RecipeDescriptor = recipeDescriptor
                                            };

                                            var stepResult = new RecipeStepResult {
                                                StepName = recipeStep.Name
                                            };
                                            result.Steps.Add(stepResult);
                                            await _recipeStore.UpdateAsync(result);

                                            ExceptionDispatchInfo capturedException = null;
                                            try
                                            {
                                                await ExecuteStepAsync(recipeStep);

                                                stepResult.IsSuccessful = true;
                                            }
                                            catch (Exception e)
                                            {
                                                stepResult.IsSuccessful = false;
                                                stepResult.ErrorMessage = e.ToString();

                                                // Because we can't do some async processing the in catch or finally
                                                // blocks, we store the exception to throw it later.

                                                capturedException = ExceptionDispatchInfo.Capture(e);
                                            }

                                            stepResult.IsCompleted = true;
                                            await _recipeStore.UpdateAsync(result);

                                            if (stepResult.IsSuccessful == false)
                                            {
                                                capturedException.Throw();
                                            }

                                            if (recipeStep.InnerRecipes != null)
                                            {
                                                foreach (var descriptor in recipeStep.InnerRecipes)
                                                {
                                                    var innerExecutionId = Guid.NewGuid().ToString();
                                                    await ExecuteAsync(innerExecutionId, descriptor, environment);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                await _recipeEventHandlers.InvokeAsync(x => x.RecipeExecutedAsync(executionId, recipeDescriptor), Logger);

                return(executionId);
            }
            catch (Exception)
            {
                await _recipeEventHandlers.InvokeAsync(x => x.ExecutionFailedAsync(executionId, recipeDescriptor), Logger);

                throw;
            }
        }
Ejemplo n.º 45
0
        public async virtual Task ImportData(SmugglerImportOptions importOptions, Stream stream)
        {
            Operations.Configure(SmugglerOptions);
            Operations.Initialize(SmugglerOptions);
            await DetectServerSupportedFeatures(importOptions.To);

            Stream sizeStream;

            var sw = Stopwatch.StartNew();
            // Try to read the stream compressed, otherwise continue uncompressed.
            JsonTextReader jsonReader;

            try
            {
                stream.Position = 0;
                sizeStream      = new CountingStream(new GZipStream(stream, CompressionMode.Decompress));
                var streamReader = new StreamReader(sizeStream);

                jsonReader = new JsonTextReader(streamReader);

                if (jsonReader.Read() == false)
                {
                    return;
                }
            }
            catch (Exception e)
            {
                if (e is InvalidDataException == false)
                {
                    throw;
                }

                stream.Seek(0, SeekOrigin.Begin);

                sizeStream = new CountingStream(new GZipStream(stream, CompressionMode.Decompress));

                var streamReader = new StreamReader(stream);

                jsonReader = new JsonTextReader(streamReader);

                if (jsonReader.Read() == false)
                {
                    return;
                }
            }

            if (jsonReader.TokenType != JsonToken.StartObject)
            {
                throw new InvalidDataException("StartObject was expected");
            }

            var exportCounts          = new Dictionary <string, int>();
            var exportSectionRegistar = new Dictionary <string, Func <int> >();

            SmugglerOptions.CancelToken.Token.ThrowIfCancellationRequested();

            exportSectionRegistar.Add("Indexes", () =>
            {
                Operations.ShowProgress("Begin reading indexes");
                var indexCount = ImportIndexes(jsonReader).Result;
                Operations.ShowProgress(string.Format("Done with reading indexes, total: {0}", indexCount));
                return(indexCount);
            });

            exportSectionRegistar.Add("Docs", () =>
            {
                Operations.ShowProgress("Begin reading documents");
                var documentCount = ImportDocuments(jsonReader).Result;
                Operations.ShowProgress(string.Format("Done with reading documents, total: {0}", documentCount));
                return(documentCount);
            });

            exportSectionRegistar.Add("Attachments", () =>
            {
                Operations.ShowProgress("Begin reading attachments");
                var attachmentCount = ImportAttachments(importOptions.To, jsonReader).Result;
                Operations.ShowProgress(string.Format("Done with reading attachments, total: {0}", attachmentCount));
                return(attachmentCount);
            });

            exportSectionRegistar.Add("Transformers", () =>
            {
                Operations.ShowProgress("Begin reading transformers");
                var transformersCount = ImportTransformers(jsonReader).Result;
                Operations.ShowProgress(string.Format("Done with reading transformers, total: {0}", transformersCount));
                return(transformersCount);
            });

            exportSectionRegistar.Add("DocsDeletions", () =>
            {
                Operations.ShowProgress("Begin reading deleted documents");
                var deletedDocumentsCount = ImportDeletedDocuments(jsonReader).Result;
                Operations.ShowProgress(string.Format("Done with reading deleted documents, total: {0}", deletedDocumentsCount));
                return(deletedDocumentsCount);
            });

            exportSectionRegistar.Add("AttachmentsDeletions", () =>
            {
                Operations.ShowProgress("Begin reading deleted attachments");
                var deletedAttachmentsCount = ImportDeletedAttachments(jsonReader).Result;
                Operations.ShowProgress(string.Format("Done with reading deleted attachments, total: {0}", deletedAttachmentsCount));
                return(deletedAttachmentsCount);
            });

            exportSectionRegistar.Keys.ForEach(k => exportCounts[k] = 0);

            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndObject)
            {
                SmugglerOptions.CancelToken.Token.ThrowIfCancellationRequested();

                if (jsonReader.TokenType != JsonToken.PropertyName)
                {
                    throw new InvalidDataException("PropertyName was expected");
                }
                Func <int> currentAction;
                var        currentSection = jsonReader.Value.ToString();
                if (exportSectionRegistar.TryGetValue(currentSection, out currentAction) == false)
                {
                    throw new InvalidDataException("Unexpected property found: " + jsonReader.Value);
                }
                if (jsonReader.Read() == false)
                {
                    exportCounts[currentSection] = 0;
                    continue;
                }

                if (jsonReader.TokenType != JsonToken.StartArray)
                {
                    throw new InvalidDataException("StartArray was expected");
                }

                exportCounts[currentSection] = currentAction();
            }

            sw.Stop();

            Operations.ShowProgress("Imported {0:#,#;;0} documents and {1:#,#;;0} attachments, deleted {2:#,#;;0} documents and {3:#,#;;0} attachments in {4:#,#.###;;0} s", exportCounts["Docs"], exportCounts["Attachments"], exportCounts["DocsDeletions"], exportCounts["AttachmentsDeletions"], sw.ElapsedMilliseconds / 1000f);

            SmugglerOptions.CancelToken.Token.ThrowIfCancellationRequested();
        }
Ejemplo n.º 46
0
        [InlineData(null)]  // Large randomly generated string
        public static void TestingGetString(string jsonString)
        {
            if (jsonString == null)
            {
                var random    = new Random(42);
                var charArray = new char[500];
                charArray[0] = '"';
                for (int i = 1; i < charArray.Length; i++)
                {
                    charArray[i] = (char)random.Next('?', '\\'); // ASCII values (between 63 and 91) that don't need to be escaped.
                }

                charArray[256] = '\\';
                charArray[257] = '"';
                charArray[charArray.Length - 1] = '"';
                jsonString = new string(charArray);
            }

            var expectedPropertyNames = new List <string>();
            var expectedValues        = new List <string>();

            var jsonNewtonsoft = new JsonTextReader(new StringReader(jsonString));

            while (jsonNewtonsoft.Read())
            {
                if (jsonNewtonsoft.TokenType == JsonToken.String)
                {
                    expectedValues.Add(jsonNewtonsoft.Value.ToString());
                }
                else if (jsonNewtonsoft.TokenType == JsonToken.PropertyName)
                {
                    expectedPropertyNames.Add(jsonNewtonsoft.Value.ToString());
                }
            }

            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);

            var actualPropertyNames = new List <string>();
            var actualValues        = new List <string>();

            var json = new Utf8JsonReader(dataUtf8, true, default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    actualValues.Add(json.GetString());
                }
                else if (json.TokenType == JsonTokenType.PropertyName)
                {
                    actualPropertyNames.Add(json.GetString());
                }
            }

            Assert.Equal(expectedPropertyNames.Count, actualPropertyNames.Count);
            for (int i = 0; i < expectedPropertyNames.Count; i++)
            {
                Assert.Equal(expectedPropertyNames[i], actualPropertyNames[i]);
            }

            Assert.Equal(expectedValues.Count, actualValues.Count);
            for (int i = 0; i < expectedValues.Count; i++)
            {
                Assert.Equal(expectedValues[i], actualValues[i]);
            }

            Assert.Equal(dataUtf8.Length, json.BytesConsumed);
            Assert.Equal(json.BytesConsumed, json.CurrentState.BytesConsumed);
        }
Ejemplo n.º 47
0
    public void LoadFromNestedObject()
    {
      string jsonText = @"{
  ""short"":
  {
    ""error"":
    {
      ""code"":0,
      ""msg"":""No action taken""
    }
  }
}";

      JsonReader reader = new JsonTextReader(new StringReader(jsonText));
      reader.Read();
      reader.Read();
      reader.Read();
      reader.Read();
      reader.Read();

      JObject o = (JObject)JToken.ReadFrom(reader);
      Assert.IsNotNull(o);
      Assert.AreEqual(@"{
  ""code"": 0,
  ""msg"": ""No action taken""
}", o.ToString(Formatting.Indented));
    }
Ejemplo n.º 48
0
        public void Deserialize2()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(source));

            reader.Read();
        }
Ejemplo n.º 49
0
    public void Load()
    {
      JsonReader reader = new JsonTextReader(new StringReader("{'propertyname':['value1']}"));
      reader.Read();

      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
      reader.Read();

      JProperty property = JProperty.Load(reader);
      Assert.AreEqual("propertyname", property.Name);
      Assert.IsTrue(JToken.DeepEquals(JArray.Parse("['value1']"), property.Value));

      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

      reader = new JsonTextReader(new StringReader("{'propertyname':null}"));
      reader.Read();

      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
      reader.Read();

      property = JProperty.Load(reader);
      Assert.AreEqual("propertyname", property.Name);
      Assert.IsTrue(JToken.DeepEquals(new JValue(null, JTokenType.Null), property.Value));

      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
    }
Ejemplo n.º 50
0
        public string JsonToXml(string JsonIn)
        {
            bool JsonInArray = false;

            JsonIn = JsonIn.Trim();
            if (JsonIn.StartsWith("[") && JsonIn.EndsWith("]"))
            {
                JsonIn      = JsonIn.TrimStart('[').TrimEnd(']');
                JsonInArray = true;
            }

            StringReader   JR  = new StringReader(JsonIn);
            JsonTextReader JTR = new JsonTextReader(JR);
            StringWriter   SW  = new StringWriter();
            XmlTextWriter  XW  = new XmlTextWriter(SW);

            XW.Formatting = System.Xml.Formatting.Indented;
            Dictionary <int, string> PropertyDict = new Dictionary <int, string>();

            XW.WriteStartElement("xml");
            XW.WriteAttributeString("fpv", "4");
            if (JsonInArray)
            {
                XW.WriteAttributeString("in_array", "1");
            }
            else
            {
                XW.WriteAttributeString("in_array", "0");
            }
            bool Read     = true;
            bool NextRead = false;

            while (Read)
            {
                if (!NextRead)
                {
                    Read = JTR.Read();
                }
                NextRead = false;

                switch (JTR.TokenType)
                {
                case (JsonToken.StartConstructor):
                    XW.WriteStartElement("cons");
                    break;

                case (JsonToken.EndConstructor):
                    XW.WriteEndElement();
                    break;

                case (JsonToken.PropertyName):
                    if (PropertyDict.ContainsKey(JTR.Depth))
                    {
                        XW.WriteEndElement();
                    }
                    PropertyDict[JTR.Depth] = JTR.Value.ToString();
                    XW.WriteStartElement("prop");
                    XW.WriteAttributeString("oname", JTR.Value.ToString());
                    break;

                case (JsonToken.Boolean):
                    XW.WriteStartElement("bool");
                    if (JTR.Value != null)
                    {
                        //XW.WriteValue(1);
                        this.WriteValue(XW, "1");
                    }
                    else
                    {
                        //XW.WriteValue(0);
                        this.WriteValue(XW, "0");
                    }
                    XW.WriteEndElement();
                    break;

                case (JsonToken.Float):
                case (JsonToken.Integer):
                case (JsonToken.Date):
                    XW.WriteStartElement("num");
                    //XW.WriteValue(JTR.Value.ToString());
                    this.WriteValue(XW, JTR.Value.ToString());
                    XW.WriteEndElement();
                    break;

                case (JsonToken.String):
                    XW.WriteStartElement("str");
                    //XW.WriteValue(JTR.Value.ToString());
                    this.WriteValue(XW, JTR.Value.ToString());
                    XW.WriteEndElement();
                    break;

                case (JsonToken.Null):
                    XW.WriteStartElement("undef");
                    //XW.WriteValue("null");
                    this.WriteValue(XW, "null");
                    XW.WriteEndElement();
                    break;

                case (JsonToken.StartArray):
                    XW.WriteStartElement("arr");
                    Read     = JTR.Read();
                    NextRead = true;
                    if (JTR.TokenType == JsonToken.EndArray)
                    {
                        //XW.WriteValue("");
                        this.WriteValue(XW, "");
                    }
                    break;

                case (JsonToken.EndArray):
                    XW.WriteEndElement();
                    break;

                case (JsonToken.StartObject):
                    XW.WriteStartElement("obj");
                    Read     = JTR.Read();
                    NextRead = true;
                    if (JTR.TokenType == JsonToken.EndObject)
                    {
                        //XW.WriteValue("");
                        this.WriteValue(XW, "");
                    }
                    break;

                case (JsonToken.EndObject):
                    bool       PropertyNameFound = false;
                    List <int> pd_keys           = new List <int>(PropertyDict.Keys);
                    foreach (int k in pd_keys)
                    {
                        if (k > JTR.Depth)
                        {
                            PropertyNameFound = true;
                            PropertyDict.Remove(k);
                        }
                    }
                    if (PropertyNameFound)
                    {
                        XW.WriteEndElement();
                    }
                    XW.WriteEndElement();
                    if (JTR.Depth == 0)
                    {
                        Read = false;
                    }
                    break;
                }
            }

            XW.WriteEndElement();
            XW.Close();
            return(SW.ToString().Trim());
        }