示例#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

            using (ParsedJson doc = SimdJson.build_parsed_json(bytes))
            {
                Console.WriteLine("Is json valid:" + doc.isValid());

                // open iterator:
                using (iterator iterator = new iterator(&doc))
                {
                    while (iterator.move_forward())
                    {
                        switch (iterator.GetTokenType())
                        {
                        case JsonTokenType.Number:
                            Console.WriteLine("integer: " + iterator.get_integer());
                            break;

                        case JsonTokenType.String:
                            Console.WriteLine("string: " + iterator.GetUtf16String());
                            break;
                        }
                    }
                }
            }
        }
        public void ValidateAllFiles()
        {
            string currentDir  = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string testDataDir = Path.Combine(currentDir, @"../../../../../external/simdjson/jsonexamples");

            string[] files = Directory.GetFiles(testDataDir, "*.json", SearchOption.AllDirectories);
            // 20 files, ~15Mb of JSON
            Assert.NotEmpty(files);
            foreach (string file in files)
            {
                ReadOnlySpan <byte> fileData = File.ReadAllBytes(file);
                using (ParsedJson doc = SimdJson.build_parsed_json(fileData))
                {
                    Assert.True(doc.isValid());
                }
            }
        }