Exemple #1
0
        /// <summary>
        /// Returns the underlying <see cref="JsonPlusLiteralType"/> of a literal value.
        /// </summary>
        /// <exception cref="JsonPlusException">The <see cref="Type"/> property must be of type <see cref="JsonPlusType.Literal"/>.</exception>
        /// <returns>
        /// The underlying <see cref="JsonPlusLiteralType"/> of an instance of this <see cref="JsonPlusValue"/> which is a literal value.
        /// </returns>
        public virtual JsonPlusLiteralType GetLiteralType()
        {
            if (Type != JsonPlusType.Literal)
            {
                throw new JsonPlusException(RS.ExpectLiteralType);
            }

            if (Count > 1)
            {
                bool containsSubstitution = false;
                foreach (IJsonPlusNode node in Children)
                {
                    if (node is JsonPlusSubstitution)
                    {
                        containsSubstitution = true;
                        break;
                    }
                }

                if (containsSubstitution)
                {
                    return(JsonPlusLiteralType.String);
                }

                if (IsTimeSpan())
                {
                    return(JsonPlusLiteralType.TimeSpan);
                }

                if (IsByteSize())
                {
                    return(JsonPlusLiteralType.ByteSize);
                }

                return(JsonPlusLiteralType.String);
            }

            if (Count == 0)
            {
                return(JsonPlusLiteralType.Null);
            }

            // timespan and bytesize expressions will have at least 2 children, so here onwards node cannot be either.
            IJsonPlusNode firstChild = Children[0];

            if (firstChild is JsonPlusSubstitution)
            {
                JsonPlusValue substChild = ((JsonPlusSubstitution)firstChild).ResolvedValue;
                if (substChild.Type != JsonPlusType.Literal)
                {
                    throw new JsonPlusException(string.Format(RS.InternalErrorStopCode, "ONLY_SUBST_TYPE_NOT_LITERAL"));
                }

                return(substChild.GetLiteralType());
            }

            if (firstChild is NullValue)
            {
                return(JsonPlusLiteralType.Null);
            }
            else if (firstChild is BooleanValue)
            {
                return(JsonPlusLiteralType.Boolean);
            }
            else if (firstChild is DecimalValue)
            {
                return(JsonPlusLiteralType.Decimal);
            }
            else if (firstChild is IntegerValue)
            {
                return(JsonPlusLiteralType.Integer);
            }
            else if (firstChild is HexadecimalValue)
            {
                return(JsonPlusLiteralType.Hexadecimal);
            }
            else if (firstChild is OctetValue)
            {
                return(JsonPlusLiteralType.Octet);
            }
            else if (firstChild is UnquotedStringValue)
            {
                return(JsonPlusLiteralType.UnquotedString);
            }
            else if (firstChild is QuotedStringValue)
            {
                return(JsonPlusLiteralType.QuotedString);
            }
            else if (firstChild is TripleQuotedStringValue)
            {
                return(JsonPlusLiteralType.TripleQuotedString);
            }
            else if (firstChild is WhitespaceValue)
            {
                return(JsonPlusLiteralType.Whitespace);
            }
            else
            {
                throw new JsonPlusException(string.Format(RS.UnknownLiteralToken, firstChild.GetType().ToString()));
            }
        }