示例#1
0
 public DebugView(JsonArray node)
 {
     _node = node;
 }
示例#2
0
    /// <summary>
    /// Returns the typed collection.
    /// </summary>
    /// <typeparam name="T">The type of each instance in the collection.</typeparam>
    /// <param name="creator">The instance factory.</param>
    /// <param name="propertyNames">The optional property names to map.</param>
    /// <returns>A typed collection based on the data parsed.</returns>
    public IEnumerable <T> ConvertTo <T>(Func <IReadOnlyList <string>, T> creator, IEnumerable <string> propertyNames = null)
    {
        var type  = typeof(T);
        var props = propertyNames?.Select(ele =>
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(ele))
                {
                    return(type.GetProperty(ele));
                }
            }
            catch (AmbiguousMatchException)
            {
            }

            return(null);
        })?.ToList();

        if (props != null && props.Count == 0)
        {
            props = null;
        }
        if (creator == null)
        {
            if (props == null)
            {
                if (type == typeof(JsonArrayNode))
                {
                    foreach (var item in this)
                    {
                        var instance = new JsonArrayNode();
                        instance.AddRange(item);
                        yield return((T)(object)instance);
                    }
                }
                else if (type == typeof(System.Text.Json.Nodes.JsonArray))
                {
                    foreach (var item in this)
                    {
                        var instance = new System.Text.Json.Nodes.JsonArray();
                        foreach (var ele in item)
                        {
                            instance.Add(ele);
                        }

                        yield return((T)(object)instance);
                    }
                }

                yield break;
            }
            else
            {
                if (type == typeof(JsonObjectNode))
                {
                    foreach (var item in this)
                    {
                        var instance = new JsonObjectNode();
                        instance.SetRange(item, propertyNames);
                        yield return((T)(object)instance);
                    }

                    yield break;
                }
            }
        }

        foreach (var item in this)
        {
            var instance = ObjectConvert.Invoke(item, creator, props);
            if (instance == null)
            {
                continue;
            }
            yield return(instance);
        }
    }