Пример #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);
        }
Пример #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);
        }
Пример #3
0
        public void RepresentASingleResourceObject()
        {
            var instance = JDocument.For(new JResource("dwarf", "gimly"));
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"data\":{\"type\":\"dwarf\",\"id\":\"gimly\"}}");
        }
Пример #4
0
        public void RepresentASingleNullResourceObject()
        {
            var instance = JDocument.For(resource: default(JResource));
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"data\":null}");
        }
Пример #5
0
        public void RepresentAnEmptyArrayOfResourceObjects()
        {
            var instance = JDocument.For(new JResource[] { });
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"data\":[]}");
        }
Пример #6
0
        public void RepresentAnArrayOfResourceObjects()
        {
            var instance = JDocument.For(new[] {
                new JResource("dwarf", "gimly"),
                new JResource("dwarf", "thorin")
            });
            var result = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"data\":[{\"type\":\"dwarf\",\"id\":\"gimly\"},{\"type\":\"dwarf\",\"id\":\"thorin\"}]}");
        }