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
        static unsafe void Main(string[] args)
        {
            string helloWorldJson     = @"{ ""answer"": 42, ""name"": ""Egor"" }";
            ReadOnlySpan <byte> bytes = Encoding.UTF8.GetBytes(helloWorldJson);

            // SimdJson is UTF8 only

            fixed(byte *ptr = bytes)
            {
                // SimdJsonN -- N stands for Native, it means we are using Bindings for simdjson native lib
                // SimdJson -- fully managed .NET Core 3.0 port
                using (ParsedJsonN doc = SimdJsonN.ParseJson(ptr, bytes.Length))
                {
                    Console.WriteLine($"Is json valid:{doc.IsValid}\n");

                    // open iterator:
                    using (var iterator = new ParsedJsonIteratorN(doc))
                    {
                        while (iterator.MoveForward())
                        {
                            if (iterator.IsInteger)
                            {
                                Console.WriteLine("integer: " + iterator.GetInteger());
                            }
                            if (iterator.IsString)
                            {
                                Console.WriteLine("string: " + iterator.GetUtf16String());
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public unsafe ulong _SimdJsonN(byte[] data, string fileName, string fileSize)
        {
            ulong numbersCount = 0;

            fixed(byte *dataPtr = data)
            {
                using (ParsedJsonN doc = SimdJsonN.ParseJson(dataPtr, data.Length))
                    using (var iterator = new ParsedJsonIteratorN(doc))
                    {
                        while (iterator.MoveForward())
                        {
                            //if (iterator.IsDouble || iterator.IsInteger)
                            numbersCount++;
                        }
                    }
            }

            return(numbersCount);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public unsafe ulong SimdJsonNUtf16(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.GetUtf16String().StartsWith('a')) // UTF16 in SimdJsonN is expected to be slow for now (see https://github.com/lemire/simdjson/pull/101)
                                {
                                    wordsCount++;
                                }
                            }
                        }
                    }
            }

            return(wordsCount);
        }