Inheritance: IElasticCollection
        public static GenericJsonCollection TransformElasticSearchResponseToGenericJsonCollection(OpenSearchResponse <ISearchResponse <GenericJsonItem> > response)
        {
            ISearchResponse <GenericJsonItem> results = null;

            results = (ISearchResponse <GenericJsonItem>)response.GetResponseObject();

            if (results == null)
            {
                throw new NotImplementedException("GenericCollection only transforms from an ElasticOpenSearchResponse");
            }

            GenericJsonCollection collection = new GenericJsonCollection();

            collection.items = new List <GenericJsonItem>();

            foreach (var doc in results.Documents)
            {
                if (doc is GenericJsonItem)
                {
                    collection.items.Add(doc);
                }
                else
                {
                    throw new InvalidDataException("Result is not a GenericJson document.");
                }
            }
            collection.ShowNamespaces = true;
            collection.Date           = DateTime.UtcNow;
            collection.ElementExtensions.Add(new XElement(XName.Get("totalResults", "http://a9.com/-/spec/opensearch/1.1/"), results.Total).CreateReader());

            return(collection);
        }
        public static GenericJsonCollection DeserializeFromStream(System.IO.Stream stream)
        {
            var serializer = new Newtonsoft.Json.JsonSerializer();

            serializer.TypeNameHandling       = TypeNameHandling.All;
            serializer.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;

            Dictionary <string, object> obj = null;

            using (var sr = new StreamReader(stream))
                using (var jsonTextReader = new JsonTextReader(sr)) {
                    obj = serializer.Deserialize <Dictionary <string, object> >(jsonTextReader);
                }

            JContainer items = (JContainer)obj["items"];

            GenericJsonCollection collection = new GenericJsonCollection();

            collection.items = new List <GenericJsonItem>();

            foreach (var item in items)
            {
                GenericJsonItem it = new GenericJsonItem(item.ToObject <Dictionary <string, object> >());
                collection.items.Add(it);
            }

            return(collection);
        }
        public static string ToJson(GenericJsonCollection gjc)
        {
            Dictionary <string, object> json = new Dictionary <string, object>();

            json.Add("items", gjc.items);

            return(json.ToJson());
        }
        public static IElasticCollection CreateFromOpenSearchResultCollection(IOpenSearchResultCollection results)
        {
            if (results is GenericJsonCollection)
            {
                return((GenericJsonCollection)results);
            }

            GenericJsonCollection collection = new GenericJsonCollection();

            collection.links = results.Links;

            foreach (IOpenSearchResultItem result in results.Items)
            {
                var item = GenericJsonItem.FromOpenSearchResultItem(result);
                collection.items.Add(item);
            }
            return(collection);
        }
 public IElasticCollection FromOpenSearchResultCollection(IOpenSearchResultCollection results)
 {
     return(GenericJsonCollection.CreateFromOpenSearchResultCollection(results));
 }
        public static GenericJsonCollection TransformElasticSearchResponseToGenericJsonCollection(OpenSearchResponse<ISearchResponse<GenericJsonItem>> response)
        {
            ISearchResponse<GenericJsonItem> results = null;

            results = (ISearchResponse<GenericJsonItem>)response.GetResponseObject();

            if (results == null) {
                throw new NotImplementedException("GenericCollection only transforms from an ElasticOpenSearchResponse");
            }

            GenericJsonCollection collection = new GenericJsonCollection();
            collection.items = new List<GenericJsonItem>();

            foreach (var doc in results.Documents) {
                if (doc is GenericJsonItem) {
                    collection.items.Add(doc);
                } else
                    throw new InvalidDataException("Result is not a GenericJson document.");
            }
            collection.ShowNamespaces = true;
            collection.Date = DateTime.UtcNow;
            collection.ElementExtensions.Add(new XElement(XName.Get("totalResults", "http://a9.com/-/spec/opensearch/1.1/"), results.Total).CreateReader());

            return collection;
        }
        public static string ToJson(GenericJsonCollection gjc)
        {
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("items", gjc.items);

            return json.ToJson();
        }
        public static GenericJsonCollection DeserializeFromStream(System.IO.Stream stream)
        {
            var serializer = new Newtonsoft.Json.JsonSerializer();
            serializer.TypeNameHandling = TypeNameHandling.All;
            serializer.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;

            Dictionary<string, object> obj = null;
            using (var sr = new StreamReader(stream))
            using (var jsonTextReader = new JsonTextReader(sr)) {
                obj = serializer.Deserialize<Dictionary<string, object>>(jsonTextReader);
            }

            JContainer items = (JContainer)obj["items"];

            GenericJsonCollection collection = new GenericJsonCollection();
            collection.items = new List<GenericJsonItem>();

            foreach (var item in items) {
                GenericJsonItem it = new GenericJsonItem(item.ToObject<Dictionary<string, object>>());
                collection.items.Add(it);
            }

            return collection;
        }
        public static IElasticCollection CreateFromOpenSearchResultCollection(IOpenSearchResultCollection results)
        {
            if (results is GenericJsonCollection)
                return (GenericJsonCollection)results;

            GenericJsonCollection collection = new GenericJsonCollection();
            collection.links = results.Links;

            foreach (IOpenSearchResultItem result in results.Items) {
                var item = GenericJsonItem.FromOpenSearchResultItem(result);
                collection.items.Add(item);
            }
            return collection;
        }