Beispiel #1
0
        private IJsonMetadata GetMetadata(IMetadataBuilderContext context, MetadataIdentity identity)
        {
            MetadataIdentity  parentIdentity = identity.Parent();
            IJsonMetadata     parent         = context.GetMetadata <IJsonMetadata>(parentIdentity.Name) ?? this.GetMetadata(context, parentIdentity);
            IRelationMetadata relation       = identity.GetMetadata <IRelationMetadata>();

            if (parent == null || relation == null)
            {
                return(null);
            }

            JsonMetadata metadata = new JsonMetadata(relation);

            if (this.HasJsonAttribute(relation) || relation.HasFlag(RelationMetadataFlags.Item))
            {
                metadata.MemberOf = metadata;
                metadata.IsRoot   = true;
                metadata.Path     = "$";
            }
            else
            {
                metadata.MemberOf = parent.MemberOf;
                metadata.IsRoot   = false;
                metadata.Path     = $"{parent.Path}.{this.GetPropertyNameFromContract(metadata)}";
            }

            context.AddMetadata <IJsonMetadata>(metadata);

            return(metadata);
        }
Beispiel #2
0
        public string GetPropertyName(IJsonMetadata metadata)
        {
            PropertyInfo propertyInfo = metadata.Relation.Member as PropertyInfo;

            if (propertyInfo != null)
            {
                string propertyName = propertyInfo.Name;

                if (this.Settings.ContractResolver is DefaultContractResolver dcr)
                {
                    propertyName = dcr.NamingStrategy?.GetPropertyName(propertyName, false) ?? propertyName;
                }

                JsonPropertyAttribute propAttr = propertyInfo.GetCustomAttribute <JsonPropertyAttribute>();

                if (propAttr?.PropertyName != null)
                {
                    propertyName = propAttr.PropertyName;
                }

                return(propertyName);
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Appends the current JSON path literal, e.g. <c>'$.my.value'</c>, to the attribute buffer.
        /// </summary>
        /// <param name="attribute">The current attribute.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute JsonPath(this IProjectionAttribute attribute)
        {
            IJsonMetadata metadata = ProjectionHelper.GetJsonMetadata(attribute.Metadata);

            string literal = attribute.Context.Domain.Dialect.String(metadata.Path);

            return(attribute.Append(literal));
        }
        public string GetPropertyName(IJsonMetadata metadata)
        {
            PropertyInfo propertyInfo = metadata.Relation.Member as PropertyInfo;

            if (propertyInfo != null)
            {
                return(this.Options?.PropertyNamingPolicy?.ConvertName(propertyInfo.Name) ?? propertyInfo.Name);
            }

            return(null);
        }
Beispiel #5
0
        /// <summary>
        /// Appends the current JSON path literal, e.g. <c>'$.my.value'</c>, to the attribute buffer.
        /// </summary>
        /// <param name="attribute">The current attribute.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute JsonPath(this IProjectionAttribute attribute)
        {
            IJsonMetadata metadata = attribute.Metadata.Identity.GetMetadata <IJsonMetadata>();

            if (metadata == null)
            {
                throw ProjectionException.FromProjection(attribute, "JSON metadata not found.");
            }

            string literal = attribute.Context.Domain.Dialect.String(metadata.Path);

            return(attribute.Append(literal));
        }
Beispiel #6
0
        /// <summary>
        /// Appends a call to <c>JSON_EXTRACT</c> from the current column and JSON path, e.g. <c>JSON_EXTRACT(T0."MyJson", '$.my.value')</c>, to the attribute buffer.
        /// </summary>
        /// <param name="attribute">The current attribute.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute Json(this IProjectionAttribute attribute)
        {
            IJsonMetadata json = attribute.Metadata.Identity.Require <IJsonMetadata>();

            IProjectionMetadata valueMetadata = attribute.Metadata;
            IProjectionMetadata rootMetadata  = json.MemberOf.Identity.Lookup <IProjectionMetadata>();

            attribute = attribute.Append("JSON_EXTRACT(");
            attribute = attribute.With(metadata: rootMetadata).Col();
            attribute = attribute.Append(",");
            attribute = attribute.With(metadata: valueMetadata).JsonPath();
            attribute = attribute.Append(")");

            return(attribute);
        }
Beispiel #7
0
        /// <summary>
        /// Appends a call to <c>JSON_VALUE</c> from the current column and JSON path, e.g. <c>JSON_VALUE(T0."MyJson", '$.my.value')</c>, to the attribute buffer.
        /// </summary>
        /// <param name="attribute">The current attribute.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute Json(this IProjectionAttribute attribute)
        {
            IJsonMetadata json = ProjectionHelper.GetJsonMetadata(attribute);

            IProjectionMetadata metadata     = attribute.Metadata;
            IProjectionMetadata rootMetadata = json.MemberOf.Identity.GetMetadata <IProjectionMetadata>();

            attribute = attribute.Append("JSON_VALUE(");
            try
            {
                attribute = attribute.With(metadata: rootMetadata).Col();
            }
            catch (ProjectionException ex)
            {
                throw ProjectionException.FromProjection(attribute, "JSON value requires a column to read from.", innerException: ex);
            }
            attribute = attribute.Append(",");
            attribute = attribute.With(metadata: metadata).JsonPath();
            attribute = attribute.Append(")");

            return(attribute);
        }
Beispiel #8
0
        private string GetPropertyNameFromContract(IJsonMetadata metadata)
        {
            string propertyName = this.Reverse().NotNull(c => c.GetPropertyName(metadata)).FirstOrDefault();

            return(propertyName ?? metadata.Relation.Member?.Name ?? throw new InvalidOperationException("No member found."));
        }
Beispiel #9
0
 public bool Equals(IJsonMetadata other) => Equality.Combine(this.Identity, other?.Identity);