BryanWilhite / SonghayCore

core reusable, opinionated concerns for *all* 🧐 of my C# projects
http://songhayblog.azurewebsites.net/
MIT License
1 stars 0 forks source link

consider adding `.EnsureProperty()` and `.ToTopLevelProperties()` JObject extension methods #91

Closed BryanWilhite closed 4 years ago

BryanWilhite commented 4 years ago

based on this:

            if (!jObject.HasProperty("input"))
            {
                var msg = $@"
The expected `input` property is not here.

{nameof(jObject)} top-level properties:
{jObject.Properties().Select(p => p.Name).Aggregate((a, name) => string.Concat(a, name, Environment.NewLine))}
".Trim();

                throw new InvalidOperationException(msg);
            } //TODO: next version of `SonghayCore` should centralize this routine (`.EnsureProperty()` and `.ToTopLevelProperties()`)
BryanWilhite commented 4 years ago

instead of ToTopLevel... this looks promising:

/// <summary>
/// Displays the top properties of <see cref="JObject"/>.
/// </summary>
/// <param name="jO">The <see cref="JObject"/>.</param>
/// <returns></returns>
public static string DisplayTopProperties(this JObject jObject) =>
    jObject?
        .Properties()
        .Select(p => p.Name)
        .Aggregate((a, name) => string.Concat(a, name, Environment.NewLine));