Ejemplo n.º 1
0
        unsafe protected override BookShelf Deserialize(Stream stream)
        {
            // A small Json like {"Books":[{"Title":"Book 1","Id":1}]}
            // will be translated to the following JsonTokenTypes by the iterator:
            //   String =     "Books"
            //   StartArray   [
            //   StartObject  {
            //   String       "Title"
            //   String       "Book 1"
            //   String       "Id"
            //   Number        1
            //   EndObject     }
            //   EndArray      ]
            //   EndObject     }

            BookShelf lret = new BookShelf()
            {
                Books = new List <Book>()
            };
            ReadOnlySpan <byte> booksPropertySpan = BooksProperty;

            if (stream is MemoryStream mem)
            {
                byte[] buffer = mem.GetBuffer();
                fixed(byte *ptr = buffer)
                {
                    using (ParsedJsonN doc = SimdJsonN.ParseJson(ptr, (int)stream.Length))
                    {
                        // open iterator:
                        myIterator = new ParsedJsonIteratorN(doc);
                        try
                        {
                            while (myIterator.MoveForward() && myIterator.IsString)
                            {
                                if (new Span <byte>(myIterator.GetUtf8String(), (int)myIterator.GetUtf8StringLength()).SequenceEqual(booksPropertySpan))
                                {
                                    if (myIterator.MoveForward() && myIterator.IsObjectOrArray && myIterator.Down())
                                    {
                                        ReadBooks(lret.Books);
                                    }
                                }
                            }
                        }
                        finally
                        {
                            myIterator.Dispose();
                        }
                    }
                }
            }

            return(lret);
        }
Ejemplo n.º 2
0
        public unsafe ulong SimdJsonNUtf8(byte[] data, string fileName, string fileSize)
        {
            ulong wordsCount = 0;

            fixed(byte *dataPtr = data)
            {
                using (ParsedJsonN doc = SimdJsonN.ParseJson(dataPtr, data.Length))
                    using (var iterator = new ParsedJsonIteratorN(doc))
                    {
                        while (iterator.MoveForward())
                        {
                            if (iterator.IsString)
                            {
                                if (*iterator.GetUtf8String() == (byte)'a')
                                {
                                    wordsCount++;
                                }
                            }
                        }
                    }
            }

            return(wordsCount);
        }