Beispiel #1
0
        /// <summary>
        /// Parses the Json+ source code specified into structured objects.
        /// </summary>
        /// <param name="source">The source code that conforms to Json+ specification.</param>
        /// <param name="includeCallback">Callback used to resolve the `include` directive.</param>
        /// <param name="resolveSubstitutions">Resolve substitution directives.</param>
        /// <param name="resolveEnv">Try to resolve environment variables. Does nothing if <paramref name="resolveSubstitutions"/> is `false`.</param>
        /// <returns></returns>
        private JsonPlusRoot ParseSource(string source, bool resolveSubstitutions, bool resolveEnv, IncludeCallbackAsync includeCallback)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                throw new JsonPlusParserException(string.Format(RS.SourceEmptyError, nameof(source)));
            }

            if (includeCallback != null)
            {
                _includeCallback = includeCallback;
            }

            try
            {
                _tokens = new JPlusTokenizer(source).Tokenize();
                _root   = new JsonPlusValue(null);
                ParseTokens();
                if (resolveSubstitutions)
                {
                    ResolveAllSubstitution(resolveEnv);
                }
            }
            catch (JsonPlusTokenizerException e)
            {
                throw JsonPlusParserException.Create(e, null, string.Format(RS.TokenizeError, e.Message), e);
            }
            catch (JsonPlusException e)
            {
                throw JsonPlusParserException.Create(_tokens.Current, Path, e.Message, e);
            }

            return(new JsonPlusRoot(_root, _substitutions));
        }
Beispiel #2
0
 /// <summary>
 /// Parses the Json+ source code specified into structured objects.
 /// </summary>
 /// <param name="source">The source code that conforms to Json+ specification.</param>
 /// <param name="includeCallback">Callback used to resolve the `include` directive.</param>
 /// <param name="resolveEnv">Allow substitutions to access environment variables. Defaults to `false`.</param>
 /// <exception cref="JsonPlusParserException">An unresolved substitution has occured, or an error occured at the tokenizing or parsing stage.</exception>
 /// <returns>The root node from parsing <paramref name="source"/> and any included resources.</returns>
 public static JsonPlusRoot Parse(string source, IncludeCallbackAsync includeCallback = null, bool resolveEnv = false)
 {
     return(new JsonPlusParser().ParseSource(source, true, resolveEnv, includeCallback));
 }