コード例 #1
0
        public void GivenObject_Enumerable_MustVisitAllProperty()
        {
            var obj = new
            {
                A = 1,
                B = 2
            };
            var visitor = new JsonVisitor(obj);

            var map = visitor.ToDictionary(x => x.Key.ToLower(), x => x.Value.ToString());

            Assert.AreEqual(2, map.Count);
            Assert.AreEqual("1", map["a"]);
            Assert.AreEqual("2", map["b"]);

            IEnumerable v = visitor;

            map = new Dictionary <string, string>();
            var enu = v.GetEnumerator();

            while (enu.MoveNext())
            {
                var c = (KeyValuePair <string, IJsonVisitor>)enu.Current;
                map.Add(c.Key.ToLower(), c.Value.ToString());
            }

            Assert.AreEqual(2, map.Count);
            Assert.AreEqual("1", map["a"]);
            Assert.AreEqual("2", map["b"]);

            visitor.Dispose();
        }
コード例 #2
0
        public void GivenObject_DisposeIt()
        {
            var obj     = new object();
            var visitor = new JsonVisitor(obj);

            visitor.Dispose();
        }
コード例 #3
0
ファイル: with_indexed_people.cs プロジェクト: h3mpti/Relax
 protected static void IndexJson(string x)
 {
     var observer = luceneServiceFactory.GetIndexingObserverForIndex("test");
     var visitor = new JsonVisitor();
     visitor.Subscribe(observer);
     visitor.Accept(x);
 }
コード例 #4
0
ファイル: JsonSource.Enumerator.cs プロジェクト: teoadal/velo
            public bool MoveNext()
            {
                while (_tokenizer.MoveNext())
                {
                    var tokenType = _tokenizer.Current.TokenType;

                    if (tokenType == JsonTokenType.ArrayEnd)
                    {
                        break;
                    }
                    if (tokenType != JsonTokenType.ObjectStart)
                    {
                        throw Error.Deserialization(JsonTokenType.ObjectStart, tokenType);
                    }

                    var entityData = JsonVisitor.VisitObject(_tokenizer);
                    var entityType = entityData.TryGet("_type", out var typeData)
                        ? _descriptions.GetEntityType(_converters.Read <string>(typeData))
                        : Typeof <TEntity> .Raw;

                    var entityConverter = _converters.Get(entityType);
                    var entity          = (TEntity)entityConverter.ReadObject(entityData) !;

                    Current = entity !;

                    return(true);
                }

                return(false);
            }
コード例 #5
0
        public void GivenPrimaryType_Get_MustParsedValue()
        {
            var visitor = new JsonVisitor("1");
            var val     = JsonVisitorExtensions.TryGet(visitor, typeof(int), out var res);

            Assert.IsTrue(val);
            Assert.AreEqual(1, res);

            visitor = new JsonVisitor("1.123");
            val     = JsonVisitorExtensions.TryGet(visitor, typeof(double), out res);
            Assert.IsTrue(val);
            Assert.AreEqual(1.123d, res);

            visitor = new JsonVisitor("1.123");
            val     = JsonVisitorExtensions.TryGet <double>(visitor, out var dres);
            Assert.IsTrue(val);
            Assert.AreEqual(1.123d, dres);

            visitor = new JsonVisitor("1.123");
            val     = JsonVisitorExtensions.TryGet <double?>(visitor, out var ndres);
            Assert.IsTrue(val);
            Assert.AreEqual(1.123d, ndres.Value);

            visitor = new JsonVisitor("1");
            val     = JsonVisitorExtensions.TryGet <Colors>(visitor, out var color);
            Assert.IsTrue(val);
            Assert.AreEqual(Colors.Red, color);
        }
コード例 #6
0
        public async Task <ComicPage[]> GetPagesAsync(string targetUrl)
        {
            var tur = new Uri(targetUrl).Segments;
            var seg = tur[tur.Length - 2].Trim('/');
            var idx = tur[tur.Length - 1].Replace(".html", string.Empty);
            var url = string.Format(pagesUrl, seg, idx);

            using (var stream = await GetStreamAsync(url))
            {
                var str = string.Empty;
                using (var sr = new StreamReader(stream))
                {
                    str = sr.ReadToEnd();
                }
                using (var jobj = JsonVisitor.FromString(str))
                {
                    var imgs  = jobj["listImg"].ToEnumerable();
                    var pages = new List <ComicPage>();
                    var i     = 0;
                    foreach (var item in imgs)
                    {
                        var page = new ComicPage {
                            Name = i++.ToString(), TargetUrl = item.ToString()
                        };
                        pages.Add(page);
                    }
                    return(pages.ToArray());
                }
            }
        }
コード例 #7
0
        public void GivenNullString_MustParsed()
        {
            var visitor = JsonVisitor.FromString(null);

            Assert.IsNotNull(visitor);
            visitor = JsonVisitor.FromString(string.Empty);
            Assert.IsNotNull(visitor);
        }
コード例 #8
0
        public void ParesFail_MustOutException(string value, Type type)
        {
            var visitor = new JsonVisitor(value);
            var val     = JsonVisitorExtensions.TryGet(visitor, type, out var ex, out var res);

            Assert.IsFalse(val);
            Assert.IsNotNull(ex);
        }
コード例 #9
0
        protected static void IndexJson(string x)
        {
            var observer = luceneServiceFactory.GetIndexingObserverForIndex("test");
            var visitor  = new JsonVisitor();

            visitor.Subscribe(observer);
            visitor.Accept(x);
        }
コード例 #10
0
        public void ParseEnum_MustPass()
        {
            var visitor = new JsonVisitor("1");
            var val     = JsonVisitorExtensions.TryGet(visitor, typeof(Colors), out var ex, out var res);

            Assert.IsTrue(val);
            Assert.IsNull(ex);
            Assert.AreEqual(Colors.Red, res);
        }
コード例 #11
0
        public void PasePrimary_MustPass(string value, Type type)
        {
            var visitor = new JsonVisitor(value);
            var val     = JsonVisitorExtensions.TryGet(visitor, type, out var ex, out var res);

            Assert.IsTrue(val);
            Assert.IsNull(ex);
            Assert.IsInstanceOfType(res, type);
        }
コード例 #12
0
        public void ParseToString_MustReturnOrigin()
        {
            var visitor = new JsonVisitor("aaa");
            var val     = JsonVisitorExtensions.TryGet(visitor, typeof(string), out var ex, out var res);

            Assert.IsTrue(val);
            Assert.IsNull(ex);
            Assert.AreEqual(visitor.ToString(), res);
        }
        public IDictionary <string, string> Serialize(object source, string rootSectionName)
        {
            var json       = JsonConvert.SerializeObject(source);
            var jsonConfig = JObject.Parse(json);

            var visitor = new JsonVisitor();

            return(visitor.ParseObject(jsonConfig, rootSectionName));
        }
コード例 #14
0
        public void PaseNullableType_MustReturnValue(string value, Type type)
        {
            var t       = Nullable.GetUnderlyingType(type);
            var visitor = new JsonVisitor(value);
            var val     = JsonVisitorExtensions.TryGet(visitor, type, out var ex, out var res);

            Assert.IsTrue(val);
            Assert.IsNull(ex);
            Assert.IsInstanceOfType(res, t);
        }
コード例 #15
0
        public void GivenArray_GetProperty_MustGotArray()
        {
            var obj     = new[] { 1, 2, 3, 4, 5, 6 };
            var visitor = new JsonVisitor(obj);
            var arr     = visitor.ToArray();
            var count   = arr.Count();

            Assert.AreEqual(obj.Length, count);
            Assert.AreEqual("1", arr.First().Value.ToString());
        }
コード例 #16
0
        public async Task <SearchComicResult> SearchAsync(string keywork, int skip, int take)
        {
            var page = 1;

            if (take != 0)
            {
                page = Math.Max(1, skip / take);
            }
            var targetUrl = string.Format(url, keywork, page, take);
            var setting   = new RequestSettings
            {
                Address  = targetUrl,
                Referrer = "https://www.kuaikanmanhua.com/"
            };
            var datas = await networkAdapter.GetStringAsync(setting);

            using (var visit = JsonVisitor.FromString(datas))
            {
                var hit = visit["data"]["hit"].ToArray();
                var res = new SearchComicResult
                {
                    Total   = hit.Count(),
                    Support = true
                };
                var sns = new List <ComicSnapshot>();
                foreach (var item in hit)
                {
                    var conv  = item["vertical_image_url"].ToString();
                    var title = item["title"].ToString();
                    var id    = item["id"].ToString();
                    var desc  = item["description"].ToString();
                    var user  = item["user"]["nickname"].ToString();
                    var sn    = new ComicSnapshot
                    {
                        Author    = user,
                        Descript  = desc,
                        ImageUri  = conv,
                        Name      = title,
                        TargetUrl = targetUrl,
                        Sources   = new ComicSource[]
                        {
                            new ComicSource
                            {
                                TargetUrl = "https://www.kuaikanmanhua.com/web/topic/" + id,
                                Name      = "Kuaikan"
                            }
                        }
                    };
                    sns.Add(sn);
                }
                res.Snapshots = sns.ToArray();
                return(res);
            }
        }
コード例 #17
0
        public void InitWithNull_HasValueMustFalse()
        {
            var visitor = new JsonVisitor((object)null);

            Assert.IsFalse(visitor.HasValue);
            visitor.Dispose();
#if NET45
            visitor = new JsonVisitor((JToken)null);
            Assert.IsFalse(visitor.HasValue);
            visitor.Dispose();
#endif
        }
コード例 #18
0
        public void GivenStringToCreate_MustParsedToObjectOrArray()
        {
            var obj    = @"{""A"":1,""B"":2}";
            var arr    = @"[1,2,3,4]";
            var visit1 = JsonVisitor.FromString(obj);
            var val    = visit1["A"];

            Assert.AreEqual("1", val.ToString());
            var visit2 = JsonVisitor.FromString(arr);

            Assert.AreEqual(4, visit2.ToArray().Count());
        }
コード例 #19
0
        public void GivenObject_GetProperty_MustGotValue()
        {
            var obj = new
            {
                Name  = "Joke",
                Email = "*****@*****.**"
            };
            var visitor = new JsonVisitor(obj);
            var val     = visitor["Name"];

            Assert.AreEqual("Joke", val.ToString());
        }
コード例 #20
0
        public void GivenNullCall_MustThrowException()
        {
            var visitor = new JsonVisitor();
            var type    = typeof(object);

            Assert.ThrowsException <ArgumentNullException>(() => JsonVisitorExtensions.TryGet(null, type, out _));
            Assert.ThrowsException <ArgumentNullException>(() => JsonVisitorExtensions.TryGet(visitor, null, out _));
            Assert.ThrowsException <ArgumentNullException>(() => JsonVisitorExtensions.TryGet <object>(null, out _));
            Assert.ThrowsException <ArgumentNullException>(() => JsonVisitorExtensions.TryGet <object>(null, out _, out _));
            Assert.ThrowsException <ArgumentNullException>(() => JsonVisitorExtensions.TryGet(null, typeof(int), out _, out _));
            Assert.ThrowsException <ArgumentNullException>(() => JsonVisitorExtensions.TryGet(visitor, null, out _, out _));
        }
コード例 #21
0
        public void GivenArrayValue_IsArrayMustTrue()
        {
            var val     = "[]";
            var visitor = JsonVisitor.FromString(val);

            Assert.IsTrue(visitor.IsArray);
            visitor.Dispose();

            val     = "{}";
            visitor = JsonVisitor.FromString(val);
            Assert.IsFalse(visitor.IsArray);
            visitor.Dispose();
        }
コード例 #22
0
        public void SerializationPerformance()
        {
            const int kCount = 100000;

            // Create kCount entities and assign some arbitrary component data
            for (var i = 0; i < kCount; ++i)
            {
                var entity = m_Manager.CreateEntity(typeof(TestComponent), typeof(TestComponent2), typeof(MathComponent), typeof(BlitComponent));

                var comp = m_Manager.GetComponentData <BlitComponent>(entity);
                comp.blit.x = 123f;
                comp.blit.y = 456.789;
                comp.blit.z = -12;
                comp.flt    = 0.01f;

                m_Manager.SetComponentData(entity, comp);
            }

            // Create a reusable string buffer and JsonVisitor
            var buffer  = new StringBuffer(4096);
            var visitor = new JsonVisitor {
                StringBuffer = buffer
            };

            using (var entities = m_Manager.GetAllEntities())
            {
                // Since we are testing raw serialization performance we pre warm the property type bag
                // This builds a property tree for each type
                // This is done on demand for newly discovered types
                // @NOTE This json string will also be used to debug the size for a single entity
                var container = new EntityContainer(m_Manager, entities[0]);

                var json = JsonSerializer.Serialize(ref container);

                var totalTimer = new Stopwatch();
                totalTimer.Start();

                foreach (var entity in entities)
                {
                    container = new EntityContainer(m_Manager, entity);

                    // Visit and write to the underlying StringBuffer, this is the raw json serialization
                    JsonSerializer.Serialize(ref container, visitor);

                    // @NOTE at this point we can call Write(buffer.Buffer, 0, buffer.Length)
                    buffer.Clear();
                }

                totalTimer.Stop();
            }
        }
コード例 #23
0
        public async Task <ComicEntity> GetChaptersAsync(string targetUrl)
        {
            var mc   = new Uri(targetUrl).Segments.Last();
            var part = mc.Remove(0, 2);

            using (var mem = GetStream())
            {
                var arg = $"{{\"comic_id\":{part}}}";
                WrtieStream(mem, arg);
                var stream = await GetStreamAsync(detailUri, "POST", mem);

                string str = null;
                using (var sr = new StreamReader(stream))
                {
                    str = sr.ReadToEnd();
                }
                using (var jobj = JsonVisitor.FromString(str))
                {
                    var data   = jobj["data"];
                    var entity = new ComicEntity
                    {
                        Name     = data["title"].ToString(),
                        Descript = data["evaluate"].ToString(),
                        ComicUrl = targetUrl,
                        ImageUrl = data["vertical_cover"].ToString()
                    };
                    var ep     = data["ep_list"].ToEnumerable();
                    var len    = ep.Count();
                    var chapts = new List <ComicChapter>(ep.Count());
                    foreach (var item in ep)
                    {
                        var title = item["title"].ToString();
                        if (string.IsNullOrWhiteSpace(title))
                        {
                            title = entity.Name + "-" + (len - chapts.Count);
                        }
                        var id  = item["id"].ToString();
                        var url = "https://manga.bilibili.com/" + mc + "/mc" + id;
                        var chp = new ComicChapter
                        {
                            TargetUrl = url,
                            Title     = title
                        };
                        chapts.Add(chp);
                    }
                    chapts.Reverse();
                    entity.Chapters = chapts.ToArray();
                    return(entity);
                }
            }
        }
コード例 #24
0
        public void GivenObject_GetPropertyWithInnder_MustGotValue()
        {
            var obj = new
            {
                Car = new
                {
                    Owner = "Joke"
                }
            };
            var visitor = new JsonVisitor(obj);
            var value   = visitor["Car"]["Owner"].ToString();

            Assert.AreEqual("Joke", value);
        }
コード例 #25
0
        public async Task <ComicPage[]> GetPagesAsync(string targetUrl)
        {
            var    blocks = new List <ComicPage>();
            string str    = null;

            using (var sr = new StreamReader(await CreateRequest(targetUrl)))
            {
                str = sr.ReadToEnd();
            }
            var match = regex.Match(str);
            var sc    = match.Groups[0].Value + ";pages;";
            var strx  = v8.Evaluate(sc)?.ToString();

            if (strx is null)
            {
#if NETSTANDARD1_3 || NET452
                return(new ComicPage[0]);
#else
                return(Array.Empty <ComicPage>());
#endif
            }
            string[] inn     = null;
            var      visitor = JsonVisitor.FromString(strx.Replace("\r\n", "$@$"));
            try
            {
                if (strx.StartsWith("{"))
                {
                    inn = visitor["page_url"].ToString().Split(new[] { "$@$" }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    inn = visitor.ToEnumerable().Select(x => x.ToString()).ToArray();
                }
                foreach (var item in inn)
                {
                    var val  = item.ToString().Trim();
                    var name = HttpUtility.UrlDecode(val.Split('/').Last());
                    blocks.Add(new ComicPage
                    {
                        Name      = name,
                        TargetUrl = "https://images.dmzj.com/" + val
                    });
                }
            }
            finally
            {
                visitor.Dispose();
            }
            return(blocks.ToArray());
        }
コード例 #26
0
        public void GivenNullOrNot_HasValueMustFalseOrTrue()
        {
            var visitor = JsonVisitor.FromString(null);

            Assert.IsFalse(visitor.HasValue);
            visitor.Dispose();

            visitor = JsonVisitor.FromString("{}");
            Assert.IsTrue(visitor.HasValue);
            visitor.Dispose();

            visitor = JsonVisitor.FromString("[]");
            Assert.IsTrue(visitor.HasValue);
            visitor.Dispose();
        }
コード例 #27
0
        public async Task <ComicPage[]> GetPagesAsync(string targetUrl)
        {
            const int width        = 660;
            var       epId         = new Uri(targetUrl).Segments.Last().TrimStart('m', 'c');
            string    targetObjStr = null;

            using (var mem = GetStream())
            {
                var arg = $"{{\"ep_id\":{epId}}}";
                WrtieStream(mem, arg);
                var stream = await GetStreamAsync(imgIndexUri, "POST", mem);

                string str = null;
                using (var sr = new StreamReader(stream))
                {
                    str = sr.ReadToEnd();
                }
                using (var jobj = JsonVisitor.FromString(str))
                {
                    var imgs  = jobj["data"]["images"].ToEnumerable();
                    var paths = imgs.Select(x => x["path"].ToString() + $"@{width}w.jpg").ToArray();
                    targetObjStr = "{\"urls\":\"[" + string.Join(",", paths.Select(x => $"\\\"{x}\\\"")) + "]\"}";
                }
            }
            using (var mem = GetStream())
            {
                WrtieStream(mem, targetObjStr);
                var stream = await GetStreamAsync(imgTokenUri, "POST", mem);

                string str = null;
                using (var sr = new StreamReader(stream))
                {
                    str = sr.ReadToEnd();
                }
                using (var jobj = JsonVisitor.FromString(str))
                {
                    var data  = jobj["data"].ToEnumerable();
                    var pages = data.Select(x => x["url"].ToString() + "?token=" + x["token"].ToString())
                                .ToArray();
                    return(Enumerable.Range(0, pages.Length)
                           .Select(x => new ComicPage {
                        Name = x.ToString(), TargetUrl = pages[x]
                    })
                           .ToArray());
                }
            }
        }
コード例 #28
0
        public async Task <ComicPage[]> GetPagesAsync(string targetUrl)
        {
            var str = string.Empty;

            using (var sr = new StreamReader(await GetStreamAsync(targetUrl)))
            {
                str = sr.ReadToEnd();
            }
            var html = new HtmlDocument();

            html.LoadHtml(str);
            var eval = await GetJsEvalAsync(FetchJsEvalAsync);

            var dataBlock  = dataRegex.Match(str);
            var noticLeft  = str.IndexOf("window[\"n", str.IndexOf("window[\"n") + 4);
            var noticRight = str.IndexOf(";", noticLeft);
            var notic      = str.Substring(noticLeft, noticRight - noticLeft);
            var data       = dataBlock.Groups[1].Value;
            var dataJs     = "var DATA='" + data + "';window.DATA=DATA;";
            var sb         = new StringBuilder();

            sb.Append(varJs);
            sb.Append(dataJs);
            sb.Append(notic + ";");
            sb.Append(eval);
            sb.Append(";JSON.stringify(_v);");
            var val = jsEngine.Evaluate(sb.ToString()).ToString();

            using (var doc = JsonVisitor.FromString(val))
            {
                var pages = new List <ComicPage>();
                var pics  = doc["picture"].ToEnumerable();
                foreach (var item in pics)
                {
                    var pid  = item["pid"];
                    var url  = item["url"];
                    var page = new ComicPage
                    {
                        Name      = pid.ToString(),
                        TargetUrl = url.ToString()
                    };
                    pages.Add(page);
                }
                return(pages.ToArray());
            }
        }
コード例 #29
0
        private static async Task <bool> FormatJsonAsync(IWritable outputSink, HttpContent content, List <string> bodyFileOutput, IPreferences preferences)
        {
            string responseContent = await content.ReadAsStringAsync().ConfigureAwait(false);

            try
            {
                JsonConfig config    = new JsonConfig(preferences);
                string     formatted = JsonVisitor.FormatAndColorize(config, responseContent);
                outputSink.WriteLine(formatted);
                bodyFileOutput?.Add(JToken.Parse(responseContent).ToString());
                return(true);
            }
            catch
            {
            }

            return(false);
        }
コード例 #30
0
        private static async Task <bool> FormatJsonAsync(HttpState programState, IWritable outputSink, HttpContent content, StreamWriter bodyFileWriter)
        {
            string responseContent = await content.ReadAsStringAsync().ConfigureAwait(false);

            try
            {
                JsonConfig config    = new JsonConfig(programState);
                string     formatted = JsonVisitor.FormatAndColorize(config, responseContent);
                outputSink.WriteLine(formatted);
                bodyFileWriter.WriteLine(JToken.Parse(responseContent).ToString());
                return(true);
            }
            catch
            {
            }

            return(false);
        }
コード例 #31
0
        internal static IEnumerable <KeyValuePair <string, string> > Traverse(JObject jObject)
        {
            var visitor = new JsonVisitor();
            IDictionary <string, string> data = new SortedDictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (KeyValuePair <string, string> primitive in visitor.VisitJObject(jObject))
            {
                if (data.ContainsKey(primitive.Key))
                {
                    throw new FormatException($"Key {primitive.Key} is duplicated in json");
                }

                data.Add(primitive);
                yield return(primitive);
            }

            //return data;
        }