Пример #1
0
        /// <summary>Parse a JSON structure which can contain tokens, and validate that it's valid.</summary>
        /// <param name="rawJson">The raw JSON structure which may contain tokens.</param>
        /// <param name="assumeModIds">Mod IDs to assume are installed for purposes of token validation.</param>
        /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param>
        /// <param name="parsed">The parsed value, which may be legitimately <c>null</c> even if successful.</param>
        public bool TryParseJsonTokens(JToken rawJson, InvariantHashSet assumeModIds, out string error, out TokenizableJToken parsed)
        {
            if (rawJson == null || rawJson.Type == JTokenType.Null)
            {
                error  = null;
                parsed = null;
                return(true);
            }

            // parse
            parsed = new TokenizableJToken(rawJson, this.Context);
            if (!this.Migrator.TryMigrate(parsed, out error))
            {
                return(false);
            }

            // validate tokens
            foreach (LexTokenToken lexToken in parsed.GetTokenStrings().SelectMany(p => p.GetTokenPlaceholders(recursive: false)))
            {
                if (!this.TryValidateToken(lexToken, assumeModIds, out error))
                {
                    return(false);
                }
            }

            // looks OK
            if (parsed.Value.Type == JTokenType.Null)
            {
                parsed = null;
            }
            error = null;
            return(true);
        }
Пример #2
0
        /// <summary>Migrate a tokenized JSON structure.</summary>
        /// <param name="tokenStructure">The tokenized JSON structure to migrate.</param>
        /// <param name="error">An error message which indicates why migration failed (if any).</param>
        /// <returns>Returns whether migration succeeded.</returns>
        public bool TryMigrate(TokenizableJToken tokenStructure, out string error)
        {
            foreach (IManagedTokenString str in tokenStructure.GetTokenStrings())
            {
                if (!this.TryMigrate(str, out error))
                {
                    return(false);
                }
            }

            error = null;
            return(true);
        }