/**
         * Reads the value form Json Reader for Array Types and cretaes instances
         */
        private static void ReadArray(object currentInst, HtmJsonTextReder reader, Type currentMemberType)
        {
            var  prop    = GetPropertyName(reader);
            Type lstType = Type.GetType(currentMemberType.AssemblyQualifiedName.Replace("[]", ""));

            List <object> elements = new List <object>();

            do
            {
                var val = reader.ReadValue(lstType);
                if (val == null)
                {
                    break;
                }
                else
                {
                    elements.Add(val);
                }
            } while (true);

            //Type genericList1 = typeof(List<>);
            //Type genericList2 = genericList1.MakeGenericType(lstType);
            dynamic arr;

            if (currentMemberType.Name.Contains("List"))
            {
                arr = Activator.CreateInstance(currentMemberType);
                for (int i = 0; i < elements.Count; i++)
                {
                    arr.Add(elements[i]);
                }
            }
            else
            {
                arr = Array.CreateInstance(lstType, elements.Count);
                for (int i = 0; i < elements.Count; i++)
                {
                    arr.SetValue(elements[i], i);
                }
            }


            SetMemberValue(currentInst, prop, arr);

            //Type t = typeof(List<>).MakeGenericType(lstType);
            //arr.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, currentInst, (object[]) arr);
            //var listOfElements = Activator.CreateInstance(t);
        }
        /**
         * Identifies the name of the property from the Json string and returns the same
         */
        private static string GetPropertyName(HtmJsonTextReder reader)
        {
            var tokens = reader.Path.Split('.');

            return(tokens[tokens.Length - 1]);
        }
 private void ReadArray(HtmJsonTextReder reader, Type currentMemberType, string propName, object currentInstance)
 {
 }
        /**
         * This method Deserializes the Json string based on the output of the Json Text Reader.
         * The type of the property is fetched through Reflection and Creates an instance of the specified type.
         * Lower level methods are called to set the values for the particular property
         */
        public static T Deserialize <T>(string fileName) where T : class
        {
            var settings = new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.Auto
            };
            var json = File.ReadAllText(fileName);

            T             res          = JsonConvert.DeserializeObject <T>(json, settings);
            List <object> instanceList = new List <object>();
            int           instanceCnt  = 0;
            object        currentInst  = null;

            HtmJsonTextReder reader = new HtmJsonTextReder(new StringReader(json));

            Type currentMemberType = null;

            while (reader.Read())
            {
                if (reader.Value != null)
                {
                    if (reader.IsProperty)
                    {
                        MemberInfo memberInfo;
                        currentMemberType = GetMemberType(GetPropertyName(reader), currentInst.GetType(), out memberInfo);

                        if (currentMemberType.IsPrimitive || currentMemberType == typeof(string))
                        {
                            var val = reader.ReadValue(currentMemberType);

                            SetMemberValue(currentInst, GetPropertyName(reader), val);
                        }
                    }
                    else if (reader.IsValue)
                    {
                    }
                    Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
                }
                else
                {
                    if (reader.IsStart)
                    {
                        if (instanceCnt == 0)
                        {
                            instanceList.Add(currentInst = Activator.CreateInstance <T>());
                            instanceCnt++;
                        }
                        else
                        {
                            string propName = GetPropertyName(reader);



                            MemberInfo membInf;
                            Type       memberType = GetMemberType(propName, currentInst.GetType(), out membInf);

                            if (memberType == null)
                            {
                                Debug.WriteLine($"The prop/field '{reader.Path}' cannot be found on the object '{currentInst.GetType().Name}'");
                            }



                            else
                            {
                                var propInst = Activator.CreateInstance(memberType);
                                SetMemberValue(currentInst, propName, propInst);
                                instanceList.Add(currentInst = propInst);
                                instanceCnt++;
                            }
                        }
                    }
                    else if (reader.IsArrayStart)
                    {
                        ReadArray(currentInst, reader, currentMemberType);
                    }
                    else if (reader.IsObjectEnd)
                    {
                        instanceCnt--;
                        instanceList.RemoveAt(instanceCnt);
                        currentInst = instanceList[instanceList.Count - 1];
                    }

                    else
                    {
                    }

                    Console.WriteLine("Token: {0}", reader.TokenType);
                }
            }

            return(default(T));
        }