I really like the new Library.Search() functionality as it returns a DynamicNodeList.
My problem is that I can't seem to serialize the result as JSON efficiently.
The way I got it to work was to loop through the results and assign the data I wanted to a new List<dynamic> and serialize that, but this really slowed the performance down, so that's a no go.
This is really a shot in the dark since I haven't used the Library.Search myself ;-) But have you tried serializing the results as a Listinstead of an IEnumerable?
List<DynamicNode> tester = Library.Search(query, Searcher).Items; string tson = JsonConvert.SerializeObject(tester.ToList()); --- ( also tried without ToList() with same result ) output = [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}............................. Just lots of emptyness :(
Using System.Web.Script.Serialization.JavaScriptSerializer string ttson = serializer.Serialize(tester.ToList()); --- ( also tried without ToList() with same result ) Error: A circular reference was detected while serializing an object of type 'umbraco.MacroEngines.DynamicNode'.
For some reason you cannot serialize a node or dynamicnode because of dependencies (I guess), so you have to simply create an anonymous object, assign the properties to it and then serialize that object instead of the actual node object. Should give you a clean JSON output after.
Code for reference:
var myAnonymousNode = new { YourProperty1 = myDynamicNodeObj.Property1 }; string json = serializer.Serialize(myAnonymousNode);
I can get it to show data now thanks to your help, but that is only from a single node. I have a list of several DynamicNodes, so i guess I have to loop through all the results.
var tester = Library.Search(query, Searcher).Items; Dictionary<string, string> dicTest = new Dictionary<string, string>(); foreach (var item in tester) { dicTest.Add(item.Name, item.Url); }
I'm just afraid that looping through maybe 500-1000 nodes will decrease performance :/
Is there some way to convert the results - List<DynamicNode> to a dictionary or an anonymous object where i select the properties without looping through all the results?
Cool! Glad it works :-) If you want a more strongly typed behavior, you could also use the .ToDictionary<int, dynamic>() method instead of the LookUp() method. Performance wise I don't think there's a difference though.
I tried ToDictionary at first, but the output wasn't quite how i wanted it. It had al the data inside an id object like this: ["1053": {"Ex1": "1","Ex2": "2","Ex3": "3"} ] when converted to json, so it wasn't compatible with my grid.
ToLookUp gave me prettier json like this: [{"Ex1": "1","Ex2": "2","Ex3": "3"}], so that's what i went for :)
JSON Serializing Library.Search results
Hey!
I really like the new Library.Search() functionality as it returns a DynamicNodeList.
My problem is that I can't seem to serialize the result as JSON efficiently.
The way I got it to work was to loop through the results and assign the data I wanted to a new List<dynamic> and serialize that, but this really slowed the performance down, so that's a no go.
This is how I intended it to work, but doesn't:
IEnumerable<DynamicNode> results = Library.Search(query, Searcher).Items;
string json = JsonConvert.SerializeObject(results); // I'm using Json.Net to serialize (tried with JavaScriptSerializer which didn't work either)
Hope anyone can solve this.
Cheers
Hi Stefan,
This is really a shot in the dark since I haven't used the Library.Search myself ;-) But have you tried serializing the results as a Listinstead of an IEnumerable?
Something like this:
Listresults = Library.Search(query, searcher).Items.ToList();
Reason for this is, that I don't think you are able to serialize an interface (IEnumerable<T>)
Not sure if the Items collection contains a ToList() method though? :-)
- Bo
Ok i tried som different things:
string tson = JsonConvert.SerializeObject(tester.ToList()); --- ( also tried without ToList() with same result )
output = [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}............................. Just lots of emptyness :(
string ttson = serializer.Serialize(tester.ToList()); --- ( also tried without ToList() with same result )
Error: A circular reference was detected while serializing an object of type 'umbraco.MacroEngines.DynamicNode'.
I'm kinda lost :(
Hi Stefan,
Sorry for the confusion here - I had the same problem some time ago and created a topic on it here: http://our.umbraco.org/forum/developers/api-questions/33465-Getting-a-nodedocument-as-JSON
For some reason you cannot serialize a node or dynamicnode because of dependencies (I guess), so you have to simply create an anonymous object, assign the properties to it and then serialize that object instead of the actual node object. Should give you a clean JSON output after.
Code for reference:
Hope that helps.
All the best,
Bo
Hi Bo
Thanks for the replies.
I can get it to show data now thanks to your help, but that is only from a single node.
I have a list of several DynamicNodes, so i guess I have to loop through all the results.
Dictionary<string, string> dicTest = new Dictionary<string, string>();
foreach (var item in tester)
{
dicTest.Add(item.Name, item.Url);
}
Is there some way to convert the results - List<DynamicNode> to a dictionary or an anonymous object where i select the properties without looping through all the results?
Finally!
I managed to solve it :)
List<DynamicNode> tester = Library.Search(query, Searcher).Items;
var testObj = tester.Skip(skip).Take(take).ToLookup(x => x.Id, x => new
{
x.Id,
x.Name,
x.UpdateDate,
});
I'm not exactly sure how this works, but it does ;)
Thanks for all your help :)
Hi Stefan,
Cool! Glad it works :-) If you want a more strongly typed behavior, you could also use the .ToDictionary<int, dynamic>() method instead of the LookUp() method. Performance wise I don't think there's a difference though.
All the best,
Bo
I tried ToDictionary at first, but the output wasn't quite how i wanted it. It had al the data inside an id object like this: ["1053": {"Ex1": "1","Ex2": "2","Ex3": "3"} ] when converted to json, so it wasn't compatible with my grid.
ToLookUp gave me prettier json like this: [{"Ex1": "1","Ex2": "2","Ex3": "3"}], so that's what i went for :)
Cheers
is working on a reply...