Beispiel #1
0
        public void MayContainAVersionButWillIgnoreWhiteSpace()
        {
            var instance = new JApiObject(version: " ");
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{}");
        }
Beispiel #2
0
        public void MustBeAnObject()
        {
            var instance = new JApiObject();
            var result   = instance.ToString();

            result.Should().Be("{}");
        }
Beispiel #3
0
        public void MayContainAVersion()
        {
            var instance = new JApiObject(version: "1.0");
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"version\":\"1.0\"}");
        }
Beispiel #4
0
        public void MayContainAMetaObjectThatNests()
        {
            var instance = new JApiObject(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\"}}}");
        }
Beispiel #5
0
        public void MayContainAMetaObjectThatIsEmpty()
        {
            var instance = new JApiObject(meta: new JObject());
            var result   = instance.ToString(Newtonsoft.Json.Formatting.None);

            result.Should().Be("{\"meta\":{}}");
        }
Beispiel #6
0
 /// <summary>
 /// Creates a JSON API document for a resource collection
 /// </summary>
 /// <param name="data">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="self">the link that generated the current response document</param>
 /// <param name="related">a related resource link when the primary data represents a resource relationship</param>
 /// <param name="first">the first page of data</param>
 /// <param name="last">the last page of data</param>
 /// <param name="prev">the prev page of data</param>
 /// <param name="next">the next page of data</param>
 /// <param name="links">additional custom links</param>
 /// <param name="included">an array of resource objects that are related to the primary data and/or each other</param>
 public JDocumentObject(
     JResourceObject[] data,
     JObject meta               = null,
     JApiObject jsonapi         = null,
     string self                = null,
     string related             = null,
     string first               = null,
     string last                = null,
     string prev                = null,
     string next                = null,
     JLinkProperty[] links      = null,
     JResourceObject[] included = null) : base(
         ToContent(
             data: data,
             meta: meta,
             jsonapi: jsonapi,
             self: self,
             related: related,
             first: first,
             last: last,
             prev: prev,
             next: next,
             links: links,
             included: included))
 {
 }
Beispiel #7
0
        /// <summary>
        /// Builds the JObject content for the common parameters
        /// </summary>
        /// <param name="data">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="self">the link that generated the current response document</param>
        /// <param name="related">a related resource link when the primary data represents a resource relationship</param>
        /// <param name="first">the first page of data</param>
        /// <param name="last">the last page of data</param>
        /// <param name="prev">the prev page of data</param>
        /// <param name="next">the next page of data</param>
        /// <param name="links">additional custom links</param>
        /// <param name="included">an array of resource objects that are related to the primary data and/or each other</param>
        /// <returns>The JObject content</returns>
        public static IEnumerable <object> ToContent(
            object data,
            JObject meta,
            JApiObject jsonapi,
            string self,
            string related,
            string first,
            string last,
            string prev,
            string next,
            JLinkProperty[] links,
            JResourceObject[] included)
        {
            if (data != null)
            {
                yield return(new JProperty(nameof(data), data));
            }
            if (meta != null)
            {
                yield return(new JProperty(nameof(meta), meta));
            }
            if (jsonapi != null)
            {
                yield return(new JProperty(nameof(jsonapi), jsonapi));
            }

            links = JLinkProperty
                    .Concat(
                self: self,
                related: related,
                first: first,
                last: last,
                prev: prev,
                next: next,
                links: links)
                    .ToArray();

            if (links.Any())
            {
                yield return(new JProperty(nameof(links), new JObject(links)));
            }
            if (included != null)
            {
                yield return(new JProperty(nameof(included), new JArray(included)));
            }
        }