Пример #1
0
    static int Main(string[] args)
    {
        try
        {
            var(close, reader)
                = args.Length > 0
                ? (true, File.OpenText(args[0]))
                : (false, Console.In);

            try
            {
                var checker = new JsonChecker();
                for (var ch = reader.Read(); ch != -1; ch = reader.Read())
                {
                    checker.Check(ch);
                }
                checker.FinalCheck();
            }
            finally
            {
                if (close)
                {
                    reader.Close();
                }
            }

            return(0);
        }
        catch (Exception e)
        {
            Console.Error.WriteLine(e.GetBaseException().Message);
            Trace.WriteLine(e.ToString());
            return(1);
        }
    }
Пример #2
0
        public LiteralStringToken(string rawText)
            : base(TokenType.T_LSTRING, rawText)
        {
            System.Diagnostics.Debug.Assert(rawText.Length >= 2);
            System.Diagnostics.Debug.Assert(rawText.StartsWith("`"));
            System.Diagnostics.Debug.Assert(rawText.EndsWith("`"));

            var literal = StringUtil.UnescapeLiteral(rawText);
            var checker = new JsonChecker();
            var lws     = true;
            var scalar  = false;

            foreach (var ch in literal)
            {
                if (lws) // leading white space?
                {
                    switch (ch)
                    {
                    case ' ':
                    case '\t':
                    case '\r':
                    case '\n':
                        break;     // ignore leading white space

                    default:       // first non-white-space
                        lws = false;
                        // if it's a scalar then embed in an array
                        if (scalar = ch != '[' && ch != '{')
                        {
                            checker.Check('[');
                        }
                        break;
                    }
                }
                checker.Check(ch);
            }
            if (scalar)
            {
                checker.Check(']');
            }
            checker.FinalCheck();
            value_ = literal;
        }
Пример #3
0
        static void Check(string name, int depth = 20)
        {
            string json;

            using (var stream = typeof(Tests).Assembly.GetManifestResourceStream(name)
                                ?? throw new Exception("Resource not found: " + name))
            {
                using (var reader = new StreamReader(stream))
                    json = reader.ReadToEnd();
            }

            var checker = new JsonChecker(depth);

            foreach (var ch in json)
            {
                checker.Check(ch);
            }
            checker.FinalCheck();
        }