Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
0
        public void BeAnEmptyObject()
        {
            var instance = JApi.For();
            var result   = instance.ToString();

            result.Should().Be("{}");
        }
Пример #4
0
        public void ContainAVersionButWillIgnoreWhiteSpace()
        {
            var instance = JApi.For(version: " ");
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{}");
        }
Пример #5
0
        public void ContainAVersion()
        {
            var instance = JApi.For(version: "1.0");
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"version\":\"1.0\"}");
        }
Пример #6
0
        public void ContainAMetaObjectThatNests()
        {
            var instance = JApi.For(meta: new JObject(new JProperty("some", new JObject(new JProperty("nested", "thing")))));
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"meta\":{\"some\":{\"nested\":\"thing\"}}}");
        }
Пример #7
0
        public void ContainAMetaObjectThatIsEmpty()
        {
            var instance = JApi.For(meta: new JObject());
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"meta\":{}}");
        }
Пример #8
0
        /// <summary>
        /// Constructs the content of the JObject representation of this type
        /// </summary>
        /// <param name="version">an optional version string</param>
        /// <param name="meta">an optional meta object</param>
        /// <returns></returns>
        public static JApi For(string version = null, JObject meta = null)
        {
            var api = new JApi();

            if (!string.IsNullOrWhiteSpace(version))
            {
                api.Add(nameof(version), version);
            }
            if (meta != null)
            {
                api.Add(nameof(meta), meta);
            }
            return(api);
        }
Пример #9
0
 // <summary>
 /// Creates a JSON API document for a resource collection
 /// </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>
 public static JDocument For(
     IEnumerable <JResource> resources,
     JObject meta         = null,
     JApi jsonapi         = null,
     JLinks links         = null,
     JResource[] included = null)
 {
     if (resources == null)
     {
         throw new ArgumentNullException(nameof(resources));
     }
     return(For(
                data: resources.ToArray(),
                meta: meta,
                jsonapi: jsonapi,
                links: links,
                included: included
                ));
 }
Пример #10
0
 /// <summary>
 /// Creates a JSON API document that represents a single error
 /// </summary>
 /// <param name="error">an error object</param>
 /// <param name="meta"></param>
 /// <param name="jsonapi"></param>
 /// <param name="links"></param>
 /// <param name="included"></param>
 public JDocument For(
     JError error,
     JObject meta = null,
     JApi jsonapi = null,
     JLinks links = null,
     IEnumerable <JResource> included = null)
 {
     if (error == null)
     {
         throw new ArgumentNullException(nameof(error));
     }
     return(For(
                errors: new[] { error },
                meta: meta,
                jsonapi: jsonapi,
                links: links,
                included: included
                ));
 }
Пример #11
0
        /// <summary>
        /// Creates a JSON API document for a single resource
        /// </summary>
        /// <param name="resource">the document's "primary data" resource</param>
        /// <param name="meta">a meta object that contains non-standard meta-information</param>
        /// <param name="jsonapi">details about the api</param>
        /// <param name="links">the links relevant to the current resource context</param>
        /// <param name="included">an array of resource objects that are related to the primary data and/or each other</param>
        public static JDocument For(
            JResource resource,
            JObject meta         = null,
            JApi jsonapi         = null,
            JLinks links         = null,
            JResource[] included = null)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            return(For(
                       data: resource,
                       meta: meta,
                       jsonapi: jsonapi,
                       links: links,
                       included: included
                       ));
        }