Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Stefan Bohlin 46 posts 168 karma points
    Aug 10, 2012 @ 12:16
    Stefan Bohlin
    0

    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

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 12, 2012 @ 00:03
    Bo Damgaard Mortensen
    0

    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

  • Stefan Bohlin 46 posts 168 karma points
    Aug 12, 2012 @ 23:33
    Stefan Bohlin
    0

    Ok i tried som different things:

    1. .Items of DynamicNode does not have a .ToList()
    2. 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 :(
    3. 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'. 

    I'm kinda lost :(

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 13, 2012 @ 01:58
    Bo Damgaard Mortensen
    0

    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:

    var myAnonymousNode = new { YourProperty1 = myDynamicNodeObj.Property1 };
    string json =  serializer.Serialize(myAnonymousNode);

    Hope that helps.

    All the best,

    Bo

  • Stefan Bohlin 46 posts 168 karma points
    Aug 13, 2012 @ 08:53
    Stefan Bohlin
    0

    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. 

    •     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 :/
    Cheers
  • Stefan Bohlin 46 posts 168 karma points
    Aug 13, 2012 @ 08:56
    Stefan Bohlin
    0

    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?

  • Stefan Bohlin 46 posts 168 karma points
    Aug 13, 2012 @ 11:08
    Stefan Bohlin
    0

    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 :)

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 13, 2012 @ 13:24
    Bo Damgaard Mortensen
    0

    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

  • Stefan Bohlin 46 posts 168 karma points
    Aug 13, 2012 @ 13:30
    Stefan Bohlin
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft