Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public void TestLinkSerializationMetaOnly()
        {
            JsonApiLink l_h = new JsonApiLink()
            {
                Meta = meta
            };

            Assert.ThrowsException <JsonException>(() => JsonConvert.SerializeObject(l_h));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
 public JsonApiErrorLinkObject(JsonApiLink about)
 {
     About = about;
 }