title: Grails LinkedHashMap to JSON Preserve Order on RoundTrip
body: <h1>tl;dr</h1>
  
  <p>I need to send the data from a LinkedHashMap in a GSP template to a Controller and preserve the order of the elements. </p>
  
  <p>I'm assuming a structured data format like JSON is the ideal way to do this, but Grails' JSON converter doesn't create an ordered JSON object from a LinkedHashMap.</p>
  
  <p>What is the best way to send a LinkedHashMap data structure from a GSP to a Controller so that I can preserve order, but do minimal work in parsing the data?</p>
  
  <h1>Long version</h1>
  
  <p>I'm developing a taglib to render search results in a table.</p>
  
  <p>In the taglib, I construct a LinkedHashMap that specifies the data columns and the labels that the user wants to show for the column names. For example:</p>
  
  <pre><code>def tableFields = [firstName: "First Name", lastName: "Surname", unique_id: "Your Whizbang ID"]
  </code></pre>
  
  <p>That map gets sent to a view, which will then send it back to a controller to retrieve the search results from the database. I need to preserve the order of the elements (hence the use of a LinkedHashMap).</p>
  
  <p>My first thought was to turn the LinkedHashMap into a JSON string, and then send it to the controller via a hidden form element. So,</p>
  
  <pre><code>import grails.converters.JSON
  //taglib class and other code
  def tableFields = [firstName: "First Name", lastName: "Surname", unique_id: "Your Whizbang ID"] as JSON
  </code></pre>
  
  <p>However, that creates a JSON Object like this in the HTML. I'm putting this in a hidden field's value attribute.</p>
  
  <pre><code>&lt;input type="hidden" name="columns" value="{"firstName": "First Name", "lastName": "Surname", "unique_id": "Your Whizbang ID"}" id="columns"&gt;
  </code></pre>
  
  <p>Here's the JSON object by itself.</p>
  
  <pre><code>{"firstName": "First Name", "lastName": "Surname", "unique_id": "Your Whizbang ID"}
  </code></pre>
  
  <p>You can see that the JSON string's properties are in the same order as the LinkedHashMap in the JSON string. However, JSON Objects aren't really supposed to the preserve order of their properties. Thus, when my controller receives the columns parameter, and I use the <code>JSON.parse()</code> method on it, it creates a plain ol' unordered HashMap instead of a LinkedHashMap. As a result, the columns in my search results display in the wrong order when I render them into an HTML table.</p>
  
  <p><a href="http://grails.1312388.n4.nabble.com/How-to-make-grails-convert-a-map-sent-to-a-method-into-a-linkedHashMap-td4646683.html" rel="nofollow">At least one fellow</a> has had a similar problem. Adding <code>as LinkedHashMap</code> after running JSON.parse() doesn't cut it, since the .parse() method screws up the order from the get go.</p>
  
  <p>Daniel Woods, in his response to the above post, noted:</p>
  
  <blockquote>
    <p>If it's a matter of the grails data binder not working for you, you should be able to override the implicit property setter to cast the object to your favorite Map implementation.</p>
  </blockquote>
  
  <p>I assume that he's saying I could write my own parser, which would honor the order of the JSON elements (even though it technically shouldn't). I imagine I could also write my own converter so that the resulting JSON element would be something like:</p>
  
  <pre><code>{[{firstName: "First Name"}, {lastName: "Surname"}, {unique_id "Your Whizbang ID"}]}
  </code></pre>
  
  <p>I'm just about terrified of how the JSON parser would handle that, though. Would I get back a list of HashMaps?</p>
  
  <p>Again, my real question is <strong>What is the best way to send a LinkedHashMap data structure from a GSP to a Controller so that I can preserve order, but do minimal work in parsing the data?</strong> I'm assuming that's JSON, but I'm more than happy to be told, "Why not just..."</p>