<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JSON &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:23:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>JSON &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Jackson 2.x: JSON Serialization Difference for Map.Entry between 2.4.x vs 2.5.x</title>
		<link>https://myshittycode.com/2015/03/28/jackson-2-x-json-serialization-difference-for-map-entry-between-2-4-x-vs-2-5-x/</link>
					<comments>https://myshittycode.com/2015/03/28/jackson-2-x-json-serialization-difference-for-map-entry-between-2-4-x-vs-2-5-x/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Sun, 29 Mar 2015 00:38:18 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Jackson]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JSON]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=686</guid>

					<description><![CDATA[<p>It appears Jackson 2.4.5 and 2.5.1 behave a little differently when handling Map.Entry. Let&#8217;s assume we have the following bean:- We have a simple Spring MVC rest controller that creates this bean and returns the JSON data back to the client:- Jackson 2.4.5 generates the following JSON:- Jackson 2.5.1 generates the following JSON:-</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/03/28/jackson-2-x-json-serialization-difference-for-map-entry-between-2-4-x-vs-2-5-x/">Jackson 2.x: JSON Serialization Difference for Map.Entry between 2.4.x vs 2.5.x</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>It appears Jackson 2.4.5 and 2.5.1 behave a little differently when handling <b>Map.Entry</b>.</p>



<p>Let&#8217;s assume we have the following bean:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public class MyBean {
    private Map.Entry&lt;String, String&gt; entry;

    public Map.Entry&lt;String, String&gt; getEntry() {
        return entry;
    }

    public void setEntry(Map.Entry&lt;String, String&gt; entry) {
        this.entry = entry;
    }
}
</pre></div>


<p>We have a simple Spring MVC rest controller that creates this bean and returns the JSON data back to the client:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@RequestMapping(value = &quot;/map&quot;, method = RequestMethod.GET)
public ResponseEntity map() {
    final MyBean myBean = new MyBean();
    myBean.setEntry(new AbstractMap.SimpleImmutableEntry&lt;String, String&gt;(&quot;a&quot;, &quot;1&quot;));

    return new ResponseEntity&lt;MyBean&gt;(myBean, HttpStatus.OK);
}
</pre></div>


<p>Jackson 2.4.5 generates the following JSON:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
   &quot;entry&quot;:
   {
       &quot;key&quot;: &quot;a&quot;,
       &quot;value&quot;: &quot;1&quot;
   }
}
</pre></div>


<p>Jackson 2.5.1 generates the following JSON:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
   &quot;entry&quot;:
   {
       &quot;a&quot;: &quot;1&quot;
   }
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2015/03/28/jackson-2-x-json-serialization-difference-for-map-entry-between-2-4-x-vs-2-5-x/">Jackson 2.x: JSON Serialization Difference for Map.Entry between 2.4.x vs 2.5.x</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2015/03/28/jackson-2-x-json-serialization-difference-for-map-entry-between-2-4-x-vs-2-5-x/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">686</post-id>	</item>
		<item>
		<title>Spring MVC: Handling Joda Data Types as JSON</title>
		<link>https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/</link>
					<comments>https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Sat, 28 Mar 2015 23:49:18 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Joda-Time]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring MVC]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=681</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we have the following bean that contains Joda&#8217;s LocalDate and LocalDateTime objects:- This simple Spring MVC rest controller creates this bean and returns the JSON data back to the client:- By default, the generated JSON looks like this:- How do we nicely format these values and still retain the correct data types [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/">Spring MVC: Handling Joda Data Types as JSON</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we have the following bean that contains Joda&#8217;s <b>LocalDate</b> and <b>LocalDateTime</b> objects:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public class MyBean {
    private LocalDate date;
    private LocalDateTime dateTime;

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public void setDateTime(LocalDateTime dateTime) {
        this.dateTime = dateTime;
    }
}
</pre></div>


<p>This simple Spring MVC rest controller creates this bean and returns the JSON data back to the client:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@RequestMapping(value = &quot;/joda&quot;, method = RequestMethod.GET)
public ResponseEntity joda() {
    MyBean myBean = new MyBean();
    myBean.setDate(LocalDate.now());
    myBean.setDateTime(LocalDateTime.now());

    return new ResponseEntity&lt;mybean&gt;(myBean, HttpStatus.OK);
}
</pre></div>


<p>By default, the generated JSON looks like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
   &quot;date&quot;:
   &#x5B;
       2015,
       3,
       28
   ],
   &quot;dateTime&quot;:
   &#x5B;
       2015,
       3,
       28,
       18,
       12,
       58,
       992
   ]
}
</pre></div>


<p>How do we nicely format these values and still retain the correct data types (<b>LocalDate</b> and <b>LocalDateTime</b>) in <b>MyBean</b> instead of writing our custom formatter and store the values as <b>String</b>?</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>First, add a dependency for <b>jackson-datatype-joda</b>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [16,17,18,19,20]; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupid&gt;com.fasterxml.jackson.core&lt;/groupid&gt;
    &lt;artifactid&gt;jackson-core&lt;/artifactid&gt;
    &lt;version&gt;2.5.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;com.fasterxml.jackson.core&lt;/groupid&gt;
    &lt;artifactid&gt;jackson-databind&lt;/artifactid&gt;
    &lt;version&gt;2.5.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;com.fasterxml.jackson.core&lt;/groupid&gt;
    &lt;artifactid&gt;jackson-annotations&lt;/artifactid&gt;
    &lt;version&gt;2.5.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;com.fasterxml.jackson.datatype&lt;/groupid&gt;
    &lt;artifactid&gt;jackson-datatype-joda&lt;/artifactid&gt;
    &lt;version&gt;2.5.1&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<p>Next, instruct <b>MappingJackson2HttpMessageConverter</b> to accept a custom <b>ObjectMapper</b>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;bean id=&quot;objectMapper&quot; class=&quot;org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean&quot; p:indentoutput=&quot;true&quot; p:simpledateformat=&quot;yyyy-MM-dd&#039;T&#039;HH:mm:ss.SSSZ&quot;&gt;
&lt;/bean&gt;

&lt;bean class=&quot;org.springframework.beans.factory.config.MethodInvokingFactoryBean&quot; p:targetobject-ref=&quot;objectMapper&quot; p:targetmethod=&quot;registerModule&quot;&gt;
    &lt;property name=&quot;arguments&quot;&gt;
        &lt;list&gt;
            &lt;bean class=&quot;com.fasterxml.jackson.datatype.joda.JodaModule&quot;&gt;
        &lt;/bean&gt;&lt;/list&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;mvc:annotation-driven&gt;
    &lt;mvc:message-converters&gt;
        &lt;bean class=&quot;org.springframework.http.converter.StringHttpMessageConverter&quot;&gt;
        &lt;bean class=&quot;org.springframework.http.converter.ResourceHttpMessageConverter&quot;&gt;
        &lt;bean class=&quot;org.springframework.http.converter.json.MappingJackson2HttpMessageConverter&quot;&gt;
            &lt;property name=&quot;objectMapper&quot; ref=&quot;objectMapper&quot;&gt;
        &lt;/property&gt;&lt;/bean&gt;
    &lt;/bean&gt;&lt;/bean&gt;&lt;/mvc:message-converters&gt;
&lt;/mvc:annotation-driven&gt;
</pre></div>


<p>The generated JSON output now looks like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
   &quot;date&quot;: &quot;2015-03-28&quot;,
   &quot;dateTime&quot;: &quot;2015-03-28T18:11:16.348&quot;
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/">Spring MVC: Handling Joda Data Types as JSON</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">681</post-id>	</item>
		<item>
		<title>Managing the Order of AJAX Calls on Input Field&#8217;s Keyup Event</title>
		<link>https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/</link>
					<comments>https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 19 Sep 2013 14:11:20 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Moment.js]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=54</guid>

					<description><![CDATA[<p>SCENARIO Consider the following code:- When user types an employee&#8217;s name, &#8220;Mike&#8221;, in the search field, a web service call is fired per character typed. In this example, the following web service calls are made:- Let&#8217;s assume this web service searches the input string against databases (or flat files, Facebook API, etc) and returns a [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/">Managing the Order of AJAX Calls on Input Field&#8217;s Keyup Event</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">SCENARIO</h2>



<p>Consider the following code:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
&#x5B;$(&#039;#employeeSearchField&#039;).keyup(function() {
	var query = $(this).val();

	$.get(&#039;/employees/api/search&#039;, { q: query }, function(data) {
		// do stuff
		...
	});

}).trigger(&#039;keyup&#039;);
</pre></div>


<p>When user types an employee&#8217;s name, &#8220;Mike&#8221;, in the search field, a web service call is fired per character typed. In this example, the following web service calls are made:-</p>



<ul class="wp-block-list">
<li>GET /employees/api/search?q=M</li>



<li>GET /employees/api/search?q=Mi</li>



<li>GET /employees/api/search?q=Mik</li>



<li>GET /employees/api/search?q=Mike</li>
</ul>



<p>Let&#8217;s assume this web service searches the input string against databases (or flat files, Facebook API, etc) and returns a list of employee JSON objects where their names match the given input string. The code above will take the result and display the employee list on the view.</p>



<h2 class="wp-block-heading">PROBLEM</h2>



<p>Since we can&#8217;t control how long each web service call takes to process the request, the order of the returned JSON objects might not match the order of the web service calls. As a result, we may have stale information being presented on the view. Further, we may have the annoying &#8220;flicker&#8221; problem where the old employee list overrides the new employee list on the view.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>To ensure the order of the returned JSON objects matches the order of the web service calls, we need to keep track each AJAX call&#8217;s timestamp. In this example, I&#8217;m using <a title="Moment.js" href="http://momentjs.com/" target="_blank" rel="noopener">Moment.js</a>, a date library, but you can also use the built-in <b>Date</b> object. For now, think of Moment.js as a Swiss Army Knife for date, or a Rambo Knife for date, or a MacGyver Knife for date&#8230; okay, maybe not MacGyver Knife since MacGyver can use a tooth pick to solve the date problem.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
// keep track latest AJAX call&#039;s timestamp
var latestAjaxCallDateTime;

$(&#039;#employeeSearchField&#039;).keyup(function() {
	var query = $(this).val();

	// before executing the AJAX call, store the latest timestamp
	latestAjaxCallDateTime = moment();

	// set the &quot;soon-to-be-executed&quot; AJAX call&#039;s timestamp to be the same
	// as the latest timestamp
	var currentAjaxCallDateTime = latestAjaxCallDateTime;

	$.get(&#039;/employees/api/search&#039;, { q: query }, function(data) {

		// if current timestamp is older than the latest timestamp, then
		// omit the request
		if (currentAjaxCallDateTime.isBefore(latestAjaxCallDateTime)) {
			return;
		}

		// do stuff
		...
	});

}).trigger(&#039;keyup&#039;);
</pre></div>


<p>Yes, this looks pretty hacky, but it works. The whole idea is we will not process the result if it is old. The <b>key</b> of making this whole thing work is to set <b>latestAjaxCallDateTime</b> as a global variable and set <b>currentAjaxCallDateTime</b> as a global variable WITHIN the anonymous function of the input field&#8217;s keyup event.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/">Managing the Order of AJAX Calls on Input Field&#8217;s Keyup Event</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">54</post-id>	</item>
		<item>
		<title>Pretty Print JSON in JavaScript</title>
		<link>https://myshittycode.com/2013/09/17/pretty-print-json-in-javascript/</link>
					<comments>https://myshittycode.com/2013/09/17/pretty-print-json-in-javascript/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 17 Sep 2013 13:34:53 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=51</guid>

					<description><![CDATA[<p>PROBLEM You want to display a JSON object in JavaScript. TAKE 1 While this works, I find this approach inconvenient when viewing the output in Firebug because I have to click on each generated link to view the details. TAKE 2 &#8230; will generate this:- This approach will display the entire JSON object as one [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/09/17/pretty-print-json-in-javascript/">Pretty Print JSON in JavaScript</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>You want to display a JSON object in JavaScript.</p>



<h3 class="wp-block-heading">TAKE 1</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
console.log(json);
</pre></div>


<p>While this works, I find this approach inconvenient when viewing the output in Firebug because I have to click on each generated link to view the details.</p>



<h3 class="wp-block-heading">TAKE 2</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
console.log(JSON.stringify(json));
</pre></div>


<p>&#8230; will generate this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&#x5B;{&quot;title&quot;:&quot;Holiday&quot;,&quot;id&quot;:&quot;a1&quot;,&quot;start&quot;:&quot;2014-02-03T09:00:00.000Z&quot;,&quot;allDay&quot;:true},{&quot;title&quot;:&quot;Pay Day&quot;,&quot;id&quot;:&quot;a2&quot;,&quot;start&quot;:&quot;2014-03-31T08:00:00.000Z&quot;,&quot;allDay&quot;:true}]
</pre></div>


<p>This approach will display the entire JSON object as one long string. This is better than me clicking on each generated link, but this is still fairly unreadable and cumbersome to me if I have a large JSON object.</p>



<h3 class="wp-block-heading">TAKE 3</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
console.log(JSON.stringify(json, null, &#039;\t&#039;));
</pre></div>


<p>&#8230; will generate this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
&#x5B;
	{
		&quot;title&quot;: &quot;Holiday&quot;,
		&quot;id&quot;: &quot;a1&quot;,
		&quot;start&quot;: &quot;2014-02-03T09:00:00.000Z&quot;,
		&quot;allDay&quot;: true
	},
	{
		&quot;title&quot;: &quot;Pay Day&quot;,
		&quot;id&quot;: &quot;a2&quot;,
		&quot;start&quot;: &quot;2014-03-31T08:00:00.000Z&quot;,
		&quot;allDay&quot;: true
	}
]
</pre></div>


<p>Aw snap&#8230; a nicely formatted JSON string.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/09/17/pretty-print-json-in-javascript/">Pretty Print JSON in JavaScript</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/09/17/pretty-print-json-in-javascript/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">51</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching 54/65 queries in 0.055 seconds using Disk

Served from: myshittycode.com @ 2026-02-18 00:22:47 by W3 Total Cache
-->