/// <summary>
        /// Resolves any subschemas during resolution of a `$ref` during validation.
        /// </summary>
        /// <param name="pointer">A <see cref="JsonPointer"/> to the target schema.</param>
        /// <param name="baseUri">The current base URI.</param>
        /// <param name="supportedVersions">Indicates the root schema's supported versions.</param>
        /// <returns>The referenced schema, if it exists; otherwise null.</returns>
        public JsonSchema?ResolveSubschema(JsonPointer pointer, Uri baseUri, JsonSchemaVersion supportedVersions)
        {
            var first = pointer.FirstOrDefault();

            if (first == null)
            {
                return(null);
            }

            var keyword = this.FirstOrDefault(k => k.PropertyName == first);

            return(keyword?.ResolveSubschema(new JsonPointer(pointer.Skip(1)), baseUri, supportedVersions));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resolves any subschemas during resolution of a `$ref` during validation.
        /// </summary>
        /// <param name="pointer">A <see cref="JsonPointer"/> to the target schema.</param>
        /// <param name="baseUri">The current base URI.</param>
        /// <returns>The referenced schema, if it exists; otherwise null.</returns>
        public JsonSchema ResolveSubschema(JsonPointer pointer, Uri baseUri)
        {
            var first = pointer.FirstOrDefault();

            if (first == null)
            {
                return(null);
            }

            if (!TryGetValue(first, out var schema))
            {
                return(null);
            }

            return(schema.ResolveSubschema(new JsonPointer(pointer.Skip(1)), baseUri));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Resolves any subschemas during resolution of a <code>$ref</code> during validation.
        /// </summary>
        /// <param name="pointer">A <see cref="JsonPointer"/> to the target schema.</param>
        /// <param name="baseUri">The current base URI.</param>
        /// <returns>The referenced schema, if it exists; otherwise null.</returns>
        public JsonSchema ResolveSubschema(JsonPointer pointer, Uri baseUri)
        {
            var first = pointer.FirstOrDefault();

            if (first == null)
            {
                return(null);
            }

            if (!int.TryParse(first, out var index) || index < 0 || index >= Count)
            {
                return(null);
            }

            return(this[index].ResolveSubschema(new JsonPointer(pointer.Skip(1)), baseUri));
        }
Exemplo n.º 4
0
        public static void SetMember(this object obj, JsonPointer pointer, object value)
        {
            if (obj == null)
            {
                return;
            }
            if (pointer.Count == 0)
            {
                throw new ArgumentException("Pointer must have at least one segment.");
            }

            SerializationInfo member;
            var segment = pointer[0];

            if (int.TryParse(segment, out var index))
            {
                member = ReflectionCache.GetMembers(obj.GetType(), PropertySelectionStrategy.ReadWriteOnly, false)
                         .Single(m => m.MemberInfo is PropertyInfo pi && pi.GetIndexParameters().Length == 1);
                if (member == null)
                {
                    return;
                }

                var indexer = (PropertyInfo)member.MemberInfo;
                if (pointer.Count == 1)
                {
                    indexer.SetValue(obj, value, new object[] { index });
                    return;
                }

                var local = indexer.GetValue(obj, new object[] { index });
                SetMember(local, new JsonPointer(pointer.Skip(1)), value);
                return;
            }

            member = ReflectionCache.GetMembers(obj.GetType(), PropertySelectionStrategy.ReadWriteOnly, true)
                     .Single(m => m.SerializationName == segment);
            if (member == null)
            {
                return;
            }

            if (member.MemberInfo is PropertyInfo asProperty)
            {
                if (pointer.Count == 1)
                {
                    asProperty.SetValue(obj, value);
                    return;
                }

                var local = asProperty.GetValue(obj);
                SetMember(local, new JsonPointer(pointer.Skip(1)), value);
                return;
            }

            if (member.MemberInfo is FieldInfo asField)
            {
                if (pointer.Count == 1)
                {
                    asField.SetValue(obj, value);
                    return;
                }

                var local = asField.GetValue(obj);
                SetMember(local, new JsonPointer(pointer.Skip(1)), value);
                return;
            }
        }