/// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name = "reader">The <see cref = "T:Newtonsoft.Json.JsonReader"/> to read from.</param>
        /// <param name = "objectType">Type of the object.</param>
        /// <param name = "existingValue">The existing value of object being read.</param>
        /// <param name = "serializer">The calling serializer.</param>
        /// <returns>
        /// The object value.
        /// </returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            object target = null;

            if (reader.TokenType != JsonToken.Null)
            {
                JObject jObject = JObject.Load(reader);
                var     session = new Session();
                serializer.Populate(jObject.CreateReader(), session);
                if (jObject[Session.SCHEME_KEY] != null)
                {
                    var  authenticationScheme = jObject[Session.SCHEME_KEY].ToObject <AuthenticationScheme>();
                    Type authenticationType;
                    if (TypeUtil.TryGetTypeForAuthenticationScheme(authenticationScheme, out authenticationType))
                    {
                        session.Authentication = (Authentication)Activator.CreateInstance(authenticationType);
                        if (jObject[Session.AUTHENTICATION_KEY] != null)
                        {
                            serializer.Populate(jObject[Session.AUTHENTICATION_KEY].CreateReader(), session.Authentication);
                        }
                    }
                }

                target = session;
            }

            return(target);
        }
Example #2
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, global::Newtonsoft.Json.JsonSerializer serializer)
            {
                if (objectType.IsAbstract)
                {
                    // The serialization is made by the
                    // container class (Message or Command)
                    return(null);
                }
                else if (objectType == typeof(DocumentCollection))
                {
                    var instance = new DocumentCollection();

                    var jObject = JObject.Load(reader);

                    if (jObject[DocumentCollection.ITEM_TYPE_KEY] != null)
                    {
                        instance.ItemType = jObject[DocumentCollection.ITEM_TYPE_KEY].ToObject <MediaType>();
                    }

                    if (jObject[DocumentCollection.ITEMS_KEY] != null &&
                        instance.ItemType != null)
                    {
                        Type itemType;

                        if (TypeUtil.TryGetTypeForMediaType(instance.ItemType, out itemType))
                        {
                            var items = jObject[DocumentCollection.ITEMS_KEY];
                            if (items.Type == JTokenType.Array)
                            {
                                var itemsArray = (JArray)items;


                                instance.Items = new Document[itemsArray.Count];

                                for (int i = 0; i < itemsArray.Count; i++)
                                {
                                    var item = itemsArray[i];
                                    instance.Items[i] = (Document)Activator.CreateInstance(itemType);
                                    serializer.Populate(item.CreateReader(), instance.Items[i]);
                                }
                            }
                        }
                    }

                    if (jObject[DocumentCollection.TOTAL_KEY] != null)
                    {
                        instance.Total = jObject[DocumentCollection.TOTAL_KEY].ToObject <int>();
                    }

                    return(instance);
                }
                else
                {
                    var instance = Activator.CreateInstance(objectType);
                    serializer.Populate(reader, instance);
                    return(instance);
                }
            }
Example #3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            object target = null;

            if (reader.TokenType != JsonToken.Null)
            {
                // Initialize and populate the initial object
                target = Activator.CreateInstance(objectType);
                var jObject = JObject.Load(reader);
                serializer.Populate(jObject.CreateReader(), target);

                // Check if the 'type' property is present to the JSON
                JToken mediaTypeJToken;
                if (jObject.TryGetValue(DocumentContainer.TYPE_KEY, out mediaTypeJToken))
                {
                    // Find the document property
                    var documentProperty =
                        objectType
                        .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                        .First(p => p.PropertyType == typeof(Document));

                    var documentPropertyName = documentProperty.Name.ToCamelCase();
                    var mediaType            = mediaTypeJToken.ToObject <MediaType>();

                    // Create the document instance
                    var document = jObject[documentPropertyName].ToDocument(mediaType, serializer);
                    documentProperty.SetValue(target, document);
                }
            }

            return(target);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            if (objectType.IsAbstract)
            {
                // The serialization is made by the container class (Message or Command)
                return(null);
            }

            var instance = Activator.CreateInstance(objectType);

            serializer.Populate(reader, instance);
            return(instance);
        }
Example #5
0
            /// <summary>
            /// Reads the JSON representation of the object.
            /// </summary>
            /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
            /// <param name="objectType">Type of the object.</param>
            /// <param name="existingValue">The existing value of object being read.</param>
            /// <param name="serializer">The calling serializer.</param>
            /// <returns>
            /// The object value.
            /// </returns>
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, global::Newtonsoft.Json.JsonSerializer serializer)
            {
                object target = null;

                if (reader.TokenType != JsonToken.Null)
                {
                    JObject jObject = JObject.Load(reader);
                    var     command = new Command();
                    serializer.Populate(jObject.CreateReader(), command);

                    if (jObject[Command.TYPE_KEY] != null)
                    {
                        var resourceMediaType = jObject[Command.TYPE_KEY].ToObject <MediaType>();

                        Type documentType;

                        if (TypeUtil.TryGetTypeForMediaType(resourceMediaType, out documentType))
                        {
                            if (resourceMediaType.IsJson)
                            {
                                command.Resource = (Document)Activator.CreateInstance(documentType);
                                if (jObject[Command.RESOURCE_KEY] != null)
                                {
                                    var resourceReader = jObject[Command.RESOURCE_KEY].CreateReader();
                                    command.Resource = (Document)serializer.Deserialize(resourceReader, documentType);
                                }
                            }
                            else if (jObject[Command.RESOURCE_KEY] != null)
                            {
                                var parseFunc = TypeUtil.GetParseFuncForType(documentType);
                                command.Resource = (Document)parseFunc(jObject[Command.RESOURCE_KEY].ToString());
                            }
                            else
                            {
                                command.Resource = (Document)Activator.CreateInstance(documentType);
                            }
                        }
                        else
                        {
                            if (resourceMediaType.IsJson)
                            {
                                var contentJsonObject = jObject[Command.RESOURCE_KEY] as IDictionary <string, JToken>;
                                if (contentJsonObject != null)
                                {
                                    var contentDictionary = contentJsonObject.ToDictionary(k => k.Key, v => (object)v.Value.ToString());
                                    command.Resource = new JsonDocument(contentDictionary, resourceMediaType);
                                }
                                else
                                {
                                    throw new ArgumentException("The property is not a JSON");
                                }
                            }
                            else
                            {
                                command.Resource = new PlainDocument(
                                    jObject[Command.RESOURCE_KEY].ToString(),
                                    resourceMediaType);
                            }
                        }
                    }

                    target = command;
                }

                return(target);
            }
Example #6
0
            /// <summary>
            /// Reads the JSON representation of the object.
            /// </summary>
            /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
            /// <param name="objectType">Type of the object.</param>
            /// <param name="existingValue">The existing value of object being read.</param>
            /// <param name="serializer">The calling serializer.</param>
            /// <returns>
            /// The object value.
            /// </returns>
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, global::Newtonsoft.Json.JsonSerializer serializer)
            {
                object target = null;

                if (reader.TokenType != JsonToken.Null)
                {
                    JObject jObject = JObject.Load(reader);

                    if (jObject[Message.CONTENT_KEY] != null &&
                        jObject[Message.TYPE_KEY] != null)
                    {
                        var message = new Message(Guid.Empty);
                        serializer.Populate(jObject.CreateReader(), message);

                        var contentMediaType = jObject[Message.TYPE_KEY].ToObject <MediaType>();

                        Type documentType;

                        if (TypeUtil.TryGetTypeForMediaType(contentMediaType, out documentType))
                        {
                            if (contentMediaType.IsJson)
                            {
                                message.Content = (Document)Activator.CreateInstance(documentType);
                                serializer.Populate(jObject[Message.CONTENT_KEY].CreateReader(), message.Content);
                            }
                            else
                            {
                                var parseFunc = TypeUtil.GetParseFuncForType(documentType);
                                message.Content = (Document)parseFunc(jObject[Message.CONTENT_KEY].ToString());
                            }
                        }
                        else
                        {
                            if (contentMediaType.IsJson)
                            {
                                var contentJsonObject = jObject[Message.CONTENT_KEY] as IDictionary <string, JToken>;
                                if (contentJsonObject != null)
                                {
                                    var contentDictionary = contentJsonObject.ToDictionary(k => k.Key, v => (object)v.Value.ToString());
                                    message.Content = new JsonDocument(contentDictionary, contentMediaType);
                                }
                                else
                                {
                                    throw new ArgumentException("The property is not a JSON");
                                }
                            }
                            else
                            {
                                message.Content = new PlainDocument(
                                    jObject[Message.CONTENT_KEY].ToString(),
                                    contentMediaType);
                            }
                        }

                        target = message;
                    }
                    else
                    {
                        throw new ArgumentException("Invalid Message JSON");
                    }
                }

                return(target);
            }