Ejemplo n.º 1
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.º 2
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);
        }