{"id":1514,"date":"2013-01-24T14:39:10","date_gmt":"2013-01-24T22:39:10","guid":{"rendered":"http:\/\/balafon.net\/?p=1514"},"modified":"2013-01-24T21:51:03","modified_gmt":"2013-01-25T05:51:03","slug":"functional-programming-and-lazy-evaluation-ftw","status":"publish","type":"post","link":"https:\/\/balafon.net\/?p=1514","title":{"rendered":"Reflection and Lazy Evaluation FTW"},"content":{"rendered":"<p>For when you have a gigabyte of JSON data to process:<br \/>\n<!--more--><\/p>\n<pre lang=\"csharp\">\r\n    public static class LazyJson\r\n    {\r\n        public static IEnumerable<T> DeserializeEnumerable<T>(Stream inputStream)\r\n            where T: class, new()\r\n        {\r\n            var type = typeof(T);\r\n            var properties = type.GetMembers(BindingFlags.Instance | BindingFlags.Public)\r\n                .OfType<PropertyInfo>()\r\n                .Where(pi => !pi.GetCustomAttributes(typeof(JsonIgnoreAttribute), true)\r\n                                .OfType<JsonIgnoreAttribute>().Any())\r\n                .ToDictionary(pi =>\r\n                    {\r\n                        var jsonProperty = \r\n                            pi.GetCustomAttributes(typeof(JsonPropertyAttribute), true)\r\n                            .OfType<JsonPropertyAttribute>().FirstOrDefault();\r\n                        return jsonProperty != null ? jsonProperty.PropertyName : pi.Name;\r\n                    });\r\n\r\n            using (var sr = new StreamReader(inputStream))\r\n            using (var jr = new JsonTextReader(sr))\r\n            {\r\n                jr.DateParseHandling = DateParseHandling.DateTime;\r\n\r\n                T currentObject = null;\r\n                PropertyInfo currentProperty = null;\r\n\r\n                while (jr.Read())\r\n                {\r\n                    switch (jr.TokenType)\r\n                    {\r\n                        case JsonToken.StartObject:\r\n                            currentObject = new T();\r\n                            break;\r\n                        case JsonToken.PropertyName:\r\n                            currentProperty = properties[Convert.ToString(jr.Value)];\r\n                            break;\r\n                        case JsonToken.String:\r\n                            currentProperty.SetValue(currentObject, \r\n                                Convert.ChangeType(jr.Value, currentProperty.PropertyType), null);\r\n                            break;\r\n                        case JsonToken.Integer:\r\n                            currentProperty.SetValue(currentObject, \r\n                                Convert.ChangeType(jr.Value, currentProperty.PropertyType), null);\r\n                            break;\r\n                        case JsonToken.Date:\r\n                            currentProperty.SetValue(currentObject, jr.Value, null);\r\n                            break;\r\n                        case JsonToken.EndObject:\r\n                            yield return currentObject;\r\n                            break;\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For when you have a gigabyte of JSON data to process:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[46,173,172],"class_list":["post-1514","post","type-post","status-publish","format-standard","hentry","category-computing","tag-c","tag-json","tag-linq"],"_links":{"self":[{"href":"https:\/\/balafon.net\/index.php?rest_route=\/wp\/v2\/posts\/1514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/balafon.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/balafon.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/balafon.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/balafon.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1514"}],"version-history":[{"count":9,"href":"https:\/\/balafon.net\/index.php?rest_route=\/wp\/v2\/posts\/1514\/revisions"}],"predecessor-version":[{"id":1523,"href":"https:\/\/balafon.net\/index.php?rest_route=\/wp\/v2\/posts\/1514\/revisions\/1523"}],"wp:attachment":[{"href":"https:\/\/balafon.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/balafon.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/balafon.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}