public static T ToObject <T>(this Query.Result result)
        {
            T obj = default;

            if (result != null)
            {
                var settings = new JsonSerializerSettings
                {
                    ContractResolver = new ExcludeStreamPropertiesResolver()
                };

                settings.Converters?.Add(new BlobToBytesJsonConverter());

                JObject rootJObj = new JObject();

                foreach (var key in result.Keys)
                {
                    var value = result[key]?.Value;

                    if (value != null)
                    {
                        JObject jObj = null;

                        if (value.GetType() == typeof(DictionaryObject))
                        {
                            var json = JsonConvert.SerializeObject(value, settings);

                            if (!string.IsNullOrEmpty(json))
                            {
                                jObj = JObject.Parse(json);
                            }
                        }
                        else
                        {
                            jObj = new JObject
                            {
                                new JProperty(key, value)
                            };
                        }

                        if (jObj != null)
                        {
                            rootJObj.Merge(jObj, new JsonMergeSettings
                            {
                                // Union array values together to avoid duplicates (e.g. "id")
                                MergeArrayHandling = MergeArrayHandling.Union
                            });
                        }

                        if (rootJObj != null)
                        {
                            obj = rootJObj.ToObject <T>();
                        }
                    }
                }
            }

            return(obj);
        }
Example #2
0
        /*
         * public static T ToObject<T>(this Query.Result result)
         * {
         *  return DictionaryExtensions.ToObject<T>(result);
         * }
         */

        public static object ToObject(this Query.Result result, Type fallbackType)
        {
            object obj = default;

            var  dbObject = result.GetDictionary("Database");
            Type type     = null;

            if (dbObject != null)
            {
                string typeName = dbObject.GetString("$type");

                if (typeName != null)
                {
                    type = Type.GetType(typeName);
                }
            }

            if (type == null && fallbackType != null)
            {
                if (fallbackType != null)
                {
                    type = fallbackType;
                }
                else
                {
                    // todo:
                    throw new Exception();
                }
            }

            var serializer = new JsonSerializer()
            {
                TypeNameHandling         = TypeNameHandling.All,
                MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
            };

            if (result != null)
            {
                var settings = Constants.JsonSettings;

                JObject rootJObj = new JObject();

                foreach (var key in result.Keys)
                {
                    var value = result[key]?.Value;

                    if (value != null)
                    {
                        JObject jObj = null;

                        string json;

                        if (value.GetType() == typeof(DictionaryObject))
                        {
                            json = JsonConvert.SerializeObject(value, settings);

                            if (!string.IsNullOrEmpty(json))
                            {
                                jObj = JObject.Parse(json);
                            }
                        }
                        else
                        {
                            jObj = new JObject
                            {
                                new JProperty(key, value)
                            };
                        }

                        if (jObj != null)
                        {
                            rootJObj.Merge(jObj, new JsonMergeSettings
                            {
                                // Union array values together to avoid duplicates (e.g. "id")
                                MergeArrayHandling = MergeArrayHandling.Union
                            });
                        }

                        if (rootJObj != null)
                        {
                            obj = rootJObj.ToObject(type, serializer);
                        }
                    }
                }
            }

            return(obj);
        }
Example #3
0
 public static T ToObject <T>(this Query.Result result, Type fallbackType = null)
 {
     return((T)ToObject(result, typeof(T)));
 }