Example #1
0
        // <summary>
        /// Creates a JSON API document for the specified data
        /// </summary>
        /// <param name="resources">the document's "primary data" resource collection</param>
        /// <param name="meta">a meta object that contains non-standard meta-information</param>
        /// <param name="jsonapi">details about the api</param>
        /// <param name="included">an array of resource objects that are related to the primary data and/or each other</param>
        internal static JDocument For(
            object data,
            JObject meta         = null,
            JApi jsonapi         = null,
            JLinks links         = null,
            JResource[] included = null)
        {
            var document = new JDocument(new JProperty(nameof(data), data));

            if (meta != null)
            {
                document.Add(nameof(meta), meta);
            }
            if (jsonapi != null)
            {
                document.Add(nameof(jsonapi), jsonapi);
            }
            if (links != null && links.HasValues)
            {
                document.Add(nameof(links), links);
            }
            if (included != null)
            {
                document.Add(nameof(included), new JArray(included));
            }
            return(document);
        }
Example #2
0
        /// <summary>
        /// Creates a JSON API document that represents one or more errors
        /// </summary>
        /// <param name="errors">one or more error objects</param>
        /// <param name="meta"></param>
        /// <param name="jsonapi"></param>
        /// <param name="links"></param>
        /// <param name="included"></param>
        public JDocument For(
            IEnumerable <JError> errors,
            JObject meta = null,
            JApi jsonapi = null,
            JLinks links = null,
            IEnumerable <JResource> included = null)
        {
            var validErrors = errors?.Where(JError.IsValid)?.ToArray();

            if (validErrors == null || !validErrors.Any())
            {
                throw new ArgumentNullException(nameof(errors));
            }

            var document = new JDocument(new JProperty(nameof(errors), validErrors));

            if (meta != null)
            {
                document.Add(nameof(meta), meta);
            }
            if (jsonapi != null)
            {
                document.Add(nameof(jsonapi), jsonapi);
            }
            if (links != null && links.HasValues)
            {
                document.Add(nameof(links), links);
            }
            if (included != null)
            {
                document.Add(nameof(included), new JArray(included));
            }
            return(document);
        }