Example #1
0
        /// <summary>
        /// Loads from json.
        /// </summary>
        /// <param name="jsonCollection">The json collection.</param>
        /// <param name="service">The service.</param>
        void IJsonCollectionDeserializer.CreateFromJsonCollection(object[] jsonCollection, ExchangeService service)
        {
            foreach (object jsonObject in jsonCollection)
            {
                JsonObject jsonProperty = jsonObject as JsonObject;

                if (jsonProperty != null)
                {
                    TComplexProperty complexProperty = null;

                    // If type property is present, use it. Otherwise create default property instance.
                    // Note: polymorphic collections (such as Attachments) need a type property so
                    // the CreateDefaultComplexProperty call will fail.
                    if (jsonProperty.HasTypeProperty())
                    {
                        complexProperty = this.CreateComplexProperty(jsonProperty.ReadTypeString());
                    }
                    else
                    {
                        complexProperty = this.CreateDefaultComplexProperty();
                    }

                    if (complexProperty != null)
                    {
                        complexProperty.LoadFromJson(jsonProperty, service);
                        this.InternalAdd(complexProperty, true);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Loads from json to update existing property.
        /// </summary>
        /// <param name="jsonCollection">The json collection.</param>
        /// <param name="service">The service.</param>
        void IJsonCollectionDeserializer.UpdateFromJsonCollection(object[] jsonCollection, ExchangeService service)
        {
            if (this.Count != jsonCollection.Length)
            {
                throw new ServiceLocalException(Strings.PropertyCollectionSizeMismatch);
            }

            int index = 0;

            foreach (object jsonObject in jsonCollection)
            {
                JsonObject jsonProperty = jsonObject as JsonObject;

                if (jsonProperty != null)
                {
                    TComplexProperty expectedComplexProperty = null;

                    if (jsonProperty.HasTypeProperty())
                    {
                        expectedComplexProperty = this.CreateComplexProperty(jsonProperty.ReadTypeString());
                    }
                    else
                    {
                        expectedComplexProperty = this.CreateDefaultComplexProperty();
                    }

                    TComplexProperty actualComplexProperty = this[index++];

                    if (expectedComplexProperty == null || !expectedComplexProperty.GetType().IsInstanceOfType(actualComplexProperty))
                    {
                        throw new ServiceLocalException(Strings.PropertyTypeIncompatibleWhenUpdatingCollection);
                    }

                    actualComplexProperty.LoadFromJson(jsonProperty, service);
                }
                else
                {
                    throw new ServiceLocalException();
                }
            }
        }