예제 #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var linksToken = JToken.Load(reader);

            if (linksToken.Type == JTokenType.String)
            {
                return(new JsonApiLink
                {
                    Href = (string)linksToken,
                    Meta = null
                });
            }

            if (linksToken.Type != JTokenType.Object)
            {
                throw new JsonApiFormatException("The 'link' member was not a simple string or object");
            }

            var jsonApiLink = new JsonApiLink
            {
                Href = (string)linksToken["href"],
            };

            if (linksToken["meta"] != null)
            {
                jsonApiLink.Meta = linksToken["meta"].ToObject <Dictionary <string, object> >();
            }

            return(jsonApiLink);
        }
예제 #2
0
        public void TestLinkSerializationMetaOnly()
        {
            JsonApiLink l_h = new JsonApiLink()
            {
                Meta = meta
            };

            Assert.ThrowsException <JsonException>(() => JsonConvert.SerializeObject(l_h));
        }
예제 #3
0
        public void TestLinkSerialization()
        {
            JsonApiLink l_h = new JsonApiLink()
            {
                Href = testUrl, Meta = meta
            };
            JObject reference = new JObject(new JProperty("href", testUrl), new JProperty("meta", meta));

            var result          = JsonConvert.SerializeObject(l_h);
            var referenceResult = JsonConvert.SerializeObject(reference);

            Assert.IsTrue(string.Equals(result, referenceResult));
        }
예제 #4
0
        public void TestLinkSerializationHrefOnly()
        {
            JsonApiLink l_h = new JsonApiLink()
            {
                Href = testUrl
            };
            JValue reference = new JValue(testUrl);

            var result          = JsonConvert.SerializeObject(l_h);
            var referenceResult = JsonConvert.SerializeObject(reference);

            Assert.IsTrue(string.Equals(result, referenceResult));
        }
        public void TestLinkObjectDeserializationEmtyLinks()
        {
            var link1 = new JsonApiLink {
                Href = testUrl, Meta = meta
            };
            var link2 = new JsonApiLink {
                Href = testUrl
            };
            JsonApiLinksObject l = new JsonApiLinksObject
            {
                Self    = link1,
                Related = link2
            };

            var json   = JsonConvert.SerializeObject(l, Formatting.Indented);
            var result = JsonConvert.DeserializeObject <JsonApiLinksObject>(json);

            Assert.IsTrue(string.Equals(result.Self.Href, link1.Href));
            Assert.IsTrue(string.Equals(result.Related.Href, link2.Href));
        }
예제 #6
0
 public JsonApiErrorLinkObject(JsonApiLink about)
 {
     About = about;
 }