<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>JSON on 100 Exercises to Learn Go</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/</link><description>Recent content in JSON on 100 Exercises to Learn Go</description><generator>Hugo</generator><language>en</language><atom:link href="https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/index.xml" rel="self" type="application/rss+xml"/><item><title>Marshaling Tickets</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/marshal/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/marshal/</guid><description>&lt;h1 id="marshaling-tickets"&gt;Marshaling Tickets&lt;a class="anchor" href="#marshaling-tickets"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Somewhere your ticket must become bytes — to cross HTTP, to rest on disk.
&lt;code&gt;encoding/json&lt;/code&gt; performs that translation, and for structs it needs almost
no instruction: point at the value, collect the bytes. The subtleties hide
in what gets encoded and how the test proves it.&lt;/p&gt;
&lt;h2 id="values-out-bytes-back"&gt;Values out, bytes back&lt;a class="anchor" href="#values-out-bytes-back"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;encoding/json&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;MarshalTicket&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;t&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt;) (&lt;span style="color:#66d9ef"&gt;string&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;error&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;out&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;json&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Marshal&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;t&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#f92672"&gt;!=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;nil&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; string(&lt;span style="color:#a6e22e"&gt;out&lt;/span&gt;), &lt;span style="color:#66d9ef"&gt;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;json.Marshal&lt;/code&gt; returns &lt;code&gt;[]byte&lt;/code&gt;, not a string — JSON is bytes on the wire,
and Go keeps the distinction honest. &lt;code&gt;string(out)&lt;/code&gt; converts for callers
who traffic in text. Errors are rare here (only unencodable values like
channels or cyclic structures fail) but never impossible, so the signature
carries &lt;code&gt;(string, error)&lt;/code&gt; and failures propagate like any other.&lt;/p&gt;</description></item><item><title>Unmarshaling and Validating</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/unmarshal/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/unmarshal/</guid><description>&lt;h1 id="unmarshaling-and-validating"&gt;Unmarshaling and Validating&lt;a class="anchor" href="#unmarshaling-and-validating"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Encoding trusts your structs. Decoding trusts the &lt;em&gt;network&lt;/em&gt; — and the
network lies by omission. Missing fields, misspelled keys, half-formed
payloads: &lt;code&gt;json.Unmarshal&lt;/code&gt; accepts them all without complaint, filling
what it recognizes and zeroing the rest. This exercise adds the skepticism
decoding requires.&lt;/p&gt;
&lt;h2 id="decode-needs-an-address"&gt;Decode needs an address&lt;a class="anchor" href="#decode-needs-an-address"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;t&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;json&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Unmarshal&lt;/span&gt;([]byte(&lt;span style="color:#a6e22e"&gt;data&lt;/span&gt;), &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;t&lt;/span&gt;); &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt; &lt;span style="color:#f92672"&gt;!=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;nil&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt;{}, &lt;span style="color:#a6e22e"&gt;err&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;&amp;amp;&lt;/code&gt; is load-bearing. Unmarshal &lt;em&gt;writes into&lt;/em&gt; the value, so it needs
the address — passing &lt;code&gt;t&lt;/code&gt; by value would decode into a copy the caller
never sees, compiling cleanly and achieving nothing. Pointer-to-struct is
the decode target, always; the compiler can&amp;rsquo;t catch the omission because a
value is a perfectly legal (if useless) argument.&lt;/p&gt;</description></item><item><title>Struct Tags: Hiding and Omitting</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/tags/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/tags/</guid><description>&lt;h1 id="struct-tags-hiding-and-omitting"&gt;Struct Tags: Hiding and Omitting&lt;a class="anchor" href="#struct-tags-hiding-and-omitting"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The marshaling lesson encoded every exported field under a renamed key.
Real wire formats are pickier: secrets must never leave the process,
sparse updates shouldn&amp;rsquo;t ship oceans of empty strings. Both are handled
without touching the Go code that uses the struct — by annotations &lt;em&gt;on&lt;/em&gt;
the fields.&lt;/p&gt;
&lt;h2 id="metadata-in-backticks"&gt;Metadata in backticks&lt;a class="anchor" href="#metadata-in-backticks"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;ID&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#e6db74"&gt;`json:&amp;#34;id&amp;#34;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Title&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt; &lt;span style="color:#e6db74"&gt;`json:&amp;#34;title&amp;#34;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Description&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt; &lt;span style="color:#e6db74"&gt;`json:&amp;#34;description,omitempty&amp;#34;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Status&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt; &lt;span style="color:#e6db74"&gt;`json:&amp;#34;status&amp;#34;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;InternalNote&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt; &lt;span style="color:#e6db74"&gt;`json:&amp;#34;-&amp;#34;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A &lt;strong&gt;struct tag&lt;/strong&gt; is a string literal after the field — conventionally
&lt;code&gt;key:&amp;quot;value&amp;quot;&lt;/code&gt; pairs inside backticks — read via reflection by libraries.
&lt;code&gt;encoding/json&lt;/code&gt; claims the &lt;code&gt;json&lt;/code&gt; key. Its anatomy: a wire name, then
comma-separated options. &lt;code&gt;json:&amp;quot;description,omitempty&amp;quot;&lt;/code&gt; renames &lt;em&gt;and&lt;/em&gt;
conditions; &lt;code&gt;json:&amp;quot;-&amp;quot;&lt;/code&gt; means &amp;ldquo;never on the wire, in either direction.&amp;rdquo;&lt;/p&gt;</description></item><item><title>Strict Decoding</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/strict/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/strict/</guid><description>&lt;h1 id="strict-decoding"&gt;Strict Decoding&lt;a class="anchor" href="#strict-decoding"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;json.Unmarshal&lt;/code&gt; greets unknown fields with a shrug: not in the struct?
Discarded, no error. That leniency is a deliberate policy — old clients
survive servers that grew new fields — but policies have victims. A typo&amp;rsquo;d
&lt;code&gt;&amp;quot;titel&amp;quot;&lt;/code&gt; decodes into nothing, the title stays empty, and validation
blames the &lt;em&gt;content&lt;/em&gt; for a &lt;em&gt;spelling&lt;/em&gt; mistake three layers away. This
exercise flips the policy.&lt;/p&gt;
&lt;h2 id="leniency-is-a-choice-not-a-default-of-nature"&gt;Leniency is a choice, not a default of nature&lt;a class="anchor" href="#leniency-is-a-choice-not-a-default-of-nature"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Default decoding optimizes for evolution: producers may add fields freely,
consumers ignore what they don&amp;rsquo;t know. The cost lands on typos and version
skew — misspellings vanish silently, and a client talking to a newer API
never learns what it&amp;rsquo;s missing. Neither behavior is &amp;ldquo;correct&amp;rdquo;; each serves
a different trust relationship:&lt;/p&gt;</description></item><item><title>Custom Marshalers</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/custom/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/11_json/custom/</guid><description>&lt;h1 id="custom-marshalers"&gt;Custom Marshalers&lt;a class="anchor" href="#custom-marshalers"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Tags rename and omit, but they can&amp;rsquo;t &lt;em&gt;translate&lt;/em&gt;. This exercise&amp;rsquo;s &lt;code&gt;Status&lt;/code&gt;
is an &lt;code&gt;int&lt;/code&gt; underneath — &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; — while the API speaks &lt;code&gt;&amp;quot;open&amp;quot;&lt;/code&gt; and
&lt;code&gt;&amp;quot;closed&amp;quot;&lt;/code&gt;. No tag bridges that gap. So the type takes over its own
rendering, and the encoder steps aside.&lt;/p&gt;
&lt;h2 id="taking-over-the-rendering"&gt;Taking over the rendering&lt;a class="anchor" href="#taking-over-the-rendering"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Syntax: method on the type, exact signature required&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;s&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Status&lt;/span&gt;) &lt;span style="color:#a6e22e"&gt;MarshalJSON&lt;/span&gt;() ([]&lt;span style="color:#66d9ef"&gt;byte&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;error&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;switch&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;s&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;case&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;StatusOpen&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; []byte(&lt;span style="color:#a6e22e"&gt;strconv&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Quote&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;open&amp;#34;&lt;/span&gt;)), &lt;span style="color:#66d9ef"&gt;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;case&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;StatusClosed&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; []byte(&lt;span style="color:#a6e22e"&gt;strconv&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Quote&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;closed&amp;#34;&lt;/span&gt;)), &lt;span style="color:#66d9ef"&gt;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;default&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;nil&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;fmt&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Errorf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;unknown status %d&amp;#34;&lt;/span&gt;, int(&lt;span style="color:#a6e22e"&gt;s&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;json.Marshaler&lt;/code&gt; is an interface with one method — &lt;code&gt;MarshalJSON() ([]byte, error)&lt;/code&gt; — and the encoder checks for it on &lt;em&gt;every&lt;/em&gt; value it touches
(another implicit satisfaction, in the Stringer tradition: define the
method, join the club, no registration). Once &lt;code&gt;Status&lt;/code&gt; implements it,
&lt;code&gt;json.Marshal&lt;/code&gt; on the whole &lt;code&gt;Ticket&lt;/code&gt; calls it automatically for the status
field. Nothing at the call site changes; &lt;code&gt;MarshalTicket&lt;/code&gt; stays a plain
&lt;code&gt;json.Marshal&lt;/code&gt;, and the custom rendering flows through.&lt;/p&gt;</description></item></channel></rss>