Ejemplo n.º 1
0
        public void SerializeOdataOjectAsJsonTest()
        {
            // Arrange
            var expected = "{\"Id\":10,\"Object\":{\"Id\":10,\"CreateDate\":\"0001-01-01T00:00:00\",\"CreatedBy\":0,\"Entity\":null,\"EntityId\":null,\"LastUpdated\":null,\"LastUpdatedBy\":null,\"Property\":\"A\",\"Value\":\"B\"},\"RelatedEntityCollection\":[{\"Count\":2,\"RelatedEntity\":\"Fake\",\"RelatedEntities\":[{\"Id\":\"1\",\"Object\":{ \"Id\" : \"1\" },\"Uri\":null},{\"Id\":\"2\",\"Object\":{ \"Id\" : \"2\" },\"Uri\":null}]}],\"Uri\":null}";
            var relatedEntityCollection = new RelatedEntityCollection {
                Entity = "Addendum", EntityId = "10", RelatedEntity = "Fake"
            };
            var json1          = "{ \"Id\" : \"1\" }";
            var jObject1       = JObject.Parse(json1);
            var json2          = "{ \"Id\" : \"2\" }";
            var jObject2       = JObject.Parse(json1);
            var relatedEntity1 = new RelatedEntity {
                Id = jObject1.GetValue("Id").ToString(), Object = new JRaw(json1)
            };
            var relatedEntity2 = new RelatedEntity {
                Id = jObject2.GetValue("Id").ToString(), Object = new JRaw(json2)
            };

            relatedEntityCollection.Add(relatedEntity1);
            relatedEntityCollection.Add(relatedEntity2);
            var odata = new OdataObject <Addendum, long>()
            {
                Object = new Addendum {
                    Id = 10, Property = "A", Value = "B"
                }
            };

            odata.RelatedEntityCollection.Add(relatedEntityCollection);

            // Act
            var actual = new Serializer().Json(odata, ContractResolver.Instance);

            // Assert
            Assert.AreEqual(expected, Encoding.UTF8.GetString(actual));
        }
Ejemplo n.º 2
0
        public void ShouldSerializePopulatedRelatedEntityCollection()
        {
            // Arrange
            var relatedEntityCollection = new RelatedEntityCollection {
                Entity = "Addendum", EntityId = "10", RelatedEntity = "Fake"
            };
            var json1          = "{ \"Id\" : \"1\" }";
            var jObject1       = JObject.Parse(json1);
            var json2          = "{ \"Id\" : \"2\" }";
            var jObject2       = JObject.Parse(json1);
            var relatedEntity1 = new RelatedEntity {
                Id = jObject1.GetValue("Id").ToString(), Object = new JRaw(json1)
            };
            var relatedEntity2 = new RelatedEntity {
                Id = jObject2.GetValue("Id").ToString(), Object = new JRaw(json2)
            };

            relatedEntityCollection.Add(relatedEntity1);
            relatedEntityCollection.Add(relatedEntity2);
            var odata = new OdataObject <Addendum, long>()
            {
                Object = new Addendum {
                    Id = 10, Property = "A", Value = "B"
                }
            };

            odata.RelatedEntityCollection.Add(relatedEntityCollection);

            // Act & Assert
            Assert.IsTrue(ExcludeEmptyEnumerablesContractResolver.Instance.ShouldSerialize(odata, new JsonProperty {
                PropertyName = "RelatedEntityCollection", UnderlyingName = "RelatedEntityCollection"
            }));
        }
Ejemplo n.º 3
0
        public void RelatedEntityCollectionInObjectCanBeSerializedAndDeserialized()
        {
            RelatedEntityCollectionContainer relatedEntityCollectionContainer = new RelatedEntityCollectionContainer();
            RelatedEntityCollection          relatedEntityCollection          = new RelatedEntityCollection();

            relatedEntityCollectionContainer.RelatedEntityCollection = relatedEntityCollection;
            Relationship relationship = new Relationship("related_entity");

            relatedEntityCollection.Add(relationship, new EntityCollection());
            JsonSerializer serializer = new JsonSerializer();

            serializer.TypeNameHandling = TypeNameHandling.All;
            serializer.ContractResolver = new XrmContractResolver();
            MemoryStream memoryStream = new MemoryStream(new byte[9000], true);

            using (StreamWriter writer = new StreamWriter(memoryStream))
            {
                serializer.Serialize(new JsonTextWriter(writer), relatedEntityCollectionContainer);
            }

            RelatedEntityCollectionContainer deserializedRelatedEntityCollectionContainer;

            memoryStream = new MemoryStream(memoryStream.ToArray());
            using (StreamReader reader = new StreamReader(memoryStream))
            {
                deserializedRelatedEntityCollectionContainer = (RelatedEntityCollectionContainer)serializer.Deserialize(new JsonTextReader(reader));
            }

            Assert.Equal(relatedEntityCollectionContainer.GetType(), deserializedRelatedEntityCollectionContainer.GetType());
            RelatedEntityCollection deserializedRelatedEntityCollection = (RelatedEntityCollection)deserializedRelatedEntityCollectionContainer.RelatedEntityCollection;

            Assert.Equal(relatedEntityCollection.Count, deserializedRelatedEntityCollection.Count);
            Assert.Equal(relatedEntityCollection.Keys.First(), deserializedRelatedEntityCollection.Keys.First());
            Assert.Equal(relatedEntityCollection.Values.First().Entities.Count, deserializedRelatedEntityCollection.Values.First().Entities.Count);
        }
        public void SetValue(object target, object value)
        {
            RelatedEntityCollection attributeCollection = (RelatedEntityCollection)target;
            IEnumerable <KeyValuePair <Relationship, EntityCollection> > values = (IEnumerable <KeyValuePair <Relationship, EntityCollection> >)value;

            foreach (var item in values)
            {
                attributeCollection.Add(item);
            }
        }