public static JToken?ToJToken(this HoconValue hoconValue, Func <JValue, JValue>?jValueHandler = null)
        {
            if (hoconValue == null)
            {
                throw new ArgumentNullException(nameof(hoconValue));
            }

            switch (hoconValue.Type)
            {
            case HoconType.Object:
                return(hoconValue.GetObject().ToJObject(jValueHandler));

            case HoconType.Array:
                return(hoconValue.GetArray().ToJArray(jValueHandler));

            case HoconType.String:
            case HoconType.Boolean:
            case HoconType.Number:
                if (hoconValue.Count == 1)
                {
                    var hoconElement = hoconValue[0];
                    if (hoconElement is HoconSubstitution hoconSubstitution)
                    {
                        return(hoconSubstitution.ResolvedValue.ToJToken(jValueHandler));
                    }

                    if (hoconElement is HoconLiteral hoconLiteral)
                    {
                        return(hoconLiteral.ToJValue(jValueHandler));
                    }

                    throw new InvalidOperationException($"Invalid Hocon element type when hocon type is Literal and has only one element: {hoconElement.GetType().FullName}");
                }
                else
                {
                    var jValue = JValue.CreateString(hoconValue.GetString());
                    if (jValueHandler != null)
                    {
                        return(jValueHandler(jValue));
                    }
                    else
                    {
                        return(jValue);
                    }
                }

            case HoconType.Empty:
#pragma warning disable S1168 // Empty arrays and collections should be returned instead of null
                return(null);

#pragma warning restore S1168 // Empty arrays and collections should be returned instead of null
            default:
                throw new InvalidOperationException($"Unknown Type: {hoconValue.Type}");
            }
        }
예제 #2
0
        /// <summary>
        /// Retrieves a string value from the specified path in the configuration.
        /// </summary>
        /// <param name="path">The path that contains the value to retrieve.</param>
        /// <param name="default">The default value to return if the value doesn't exist.</param>
        /// <exception cref="InvalidOperationException">This exception is thrown if the current node is undefined.</exception>
        /// <returns>The string value defined in the specified path.</returns>
        public virtual string GetString(string path, string @default = null)
        {
            HoconValue value = GetNode(path);

            if (value == null)
            {
                return(@default);
            }

            return(value.GetString());
        }
        public static JValue ToJValue(this HoconLiteral hoconLiteral, Func <JValue, JValue>?jValueHandler = null)
        {
            if (hoconLiteral == null)
            {
                throw new ArgumentNullException(nameof(hoconLiteral));
            }

            var hoconValue = new HoconValue(null)
            {
                hoconLiteral,
            };

            JValue jValue;

            switch (hoconLiteral.LiteralType)
            {
            case HoconLiteralType.Null:
                jValue = JValue.CreateNull();
                break;

            case HoconLiteralType.Whitespace:
            case HoconLiteralType.UnquotedString:
            case HoconLiteralType.QuotedString:
            case HoconLiteralType.TripleQuotedString:
                jValue = JValue.CreateString(hoconValue.GetString());
                break;

            case HoconLiteralType.Bool:
                jValue = new JValue(hoconValue.GetBoolean());
                break;

            case HoconLiteralType.Long:
            case HoconLiteralType.Hex:
            case HoconLiteralType.Octal:
                jValue = new JValue(hoconValue.GetLong());
                break;

            case HoconLiteralType.Double:
                jValue = new JValue(hoconValue.GetDouble());
                break;

            default:
                throw new InvalidOperationException($"Unknown LiteralType: {hoconLiteral.LiteralType}");
            }

            if (jValueHandler != null)
            {
                return(jValueHandler(jValue));
            }
            else
            {
                return(jValue);
            }
        }
예제 #4
0
        private void VisitHoconValue(string path, HoconValue value)
        {
            if (value.IsEmpty)
            {
                return;
            }

            if (value.IsString())
            {
                Data.Add(path, value.GetString());
            }
            else if (value.IsObject())
            {
                VisitHoconObject(path, value.GetObject());
            }
            else if (value.IsArray())
            {
                VisitHoconArray(path, value.GetArray());
            }
        }
예제 #5
0
        private void VisitHoconValue(string path, HoconValue value)
        {
            if (value.IsEmpty)
            {
                return;
            }

            if (value.IsObject())
            {
                VisitHoconObject(path, value.GetObject());
            }
            else if (value.IsArray())
            {
                VisitHoconArray(path, value.GetArray());
            }
            else if (value.IsString())
            {
                Output.WriteLine($"{path} = {value.GetString()}");
            }
        }