<?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 | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Sat, 03 Oct 2020 15:00:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>JSON | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>Actually going schemaless in C#</title>
		<link>https://learncgames.com/actually-going-schemaless-in-csharp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=actually-going-schemaless-in-csharp</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sat, 03 Oct 2020 23:00:39 +0000</pubDate>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[deserialize]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Serialize]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1530</guid>

					<description><![CDATA[<p>Back in August I talked about going schemaless and I&#8217;ve made some progress since then. I&#8217;ve created a number of C# classes, many of which include single and in one cases a 2D array of things I want persisted to disk. I use JSON (JavaScript Object Notation) files to hold the data.  So long as [&#8230;]</p>
The post <a href="https://learncgames.com/actually-going-schemaless-in-csharp/">Actually going schemaless in C#</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:484,&quot;href&quot;:&quot;https:\/\/www.json.org\/json-en.html&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260206152837\/https:\/\/www.json.org\/json-en.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-09 08:25:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 15:00:53&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-18 16:32:07&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-02-23 07:11:15&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-03-04 19:26:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-08 06:29:54&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-03 13:14:47&quot;,&quot;http_code&quot;:503}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-03 13:14:47&quot;,&quot;http_code&quot;:503},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:485,&quot;href&quot;:&quot;https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/serialization&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20220901143808\/https:\/\/docs.microsoft.com\/en-US\/dotnet\/standard\/serialization\/&quot;,&quot;redirect_href&quot;:&quot;https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/serialization&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-09 08:25:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 15:00:56&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-18 16:32:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-23 07:11:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-04 19:26:08&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-08 06:29:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-05 08:14:55&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-05 08:14:55&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_1533" aria-describedby="caption-attachment-1533" style="width: 300px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-1533" src="https://learncgames.com/wp-content/uploads/2020/10/JSON-image-300x211.png" alt="JSON Image" width="300" height="211" srcset="https://learncgames.com/wp-content/uploads/2020/10/JSON-image-300x211.png 300w, https://learncgames.com/wp-content/uploads/2020/10/JSON-image-1024x721.png 1024w, https://learncgames.com/wp-content/uploads/2020/10/JSON-image-768x541.png 768w, https://learncgames.com/wp-content/uploads/2020/10/JSON-image-1536x1082.png 1536w, https://learncgames.com/wp-content/uploads/2020/10/JSON-image.png 1836w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-1533" class="wp-caption-text">Image from <a href="https://www.json.org/json-en.html">json.org</a></figcaption></figure>
<p>Back in August <a title="Link to blog entry about going schemaless" href="https://learncgames.com/trying-to-go-schemaless/" target="_blank" rel="noopener noreferrer">I talked about going schemaless</a> and I&#8217;ve made some progress since then. I&#8217;ve created a number of C# classes, many of which include single and in one cases a 2D array of things I want persisted to disk.</p>
<p>I use JSON (<em>JavaScript Object Notation</em>) files to hold the data.  So long as I&#8217;ve defined those classes with Properties using Get and Set, the JSON converters in .NET handles it all very nicely.</p>
<p>For example, in my game I have a <strong>CityArea</strong> class which includes a <strong>List&lt;BuildingType&gt;</strong> as well as several public properties.</p>
<p>This saved out as JSON like this:</p>
<pre><code class="language-json" data-line="">[
  [
    {
      &quot;buildings&quot;: [
        {
          &quot;Name&quot;: &quot;College&quot;,
          &quot;Size&quot;: 2,
          &quot;Percent&quot;: 60,
          &quot;Cost&quot;: 50,
          &quot;Type&quot;: 11
        },
        {
          &quot;Name&quot;: &quot;Gallery&quot;,
          &quot;Size&quot;: 2,
          &quot;Percent&quot;: 40,
          &quot;Cost&quot;: 150,
          &quot;Type&quot;: 23
        },
and ends...</code></pre>
<pre><code class="language-json" data-line="">      ],
      &quot;State&quot;: &quot;R&quot;,
      &quot;Type&quot;: &quot;F&quot;,
      &quot;Density&quot;: &quot;L&quot;,
      &quot;Points&quot;: 46,
      &quot;AreaName&quot;: &quot;Newfane&quot;
    },</code></pre>
<p>The whole JSON file has 1500 buildings scattered amongst 100 areas (10 x 10) and is 11422 lines long. The file is 234Kb long. Though that&#8217;s with indentations. I could remove that but for now, during development, it makes the file easier to read.</p>
<p>These are the two class definitions (I&#8217;ve removed extraneous stuff from them),</p>
<pre><code class="language-csharp" data-line="">class BuildingType : ICloneable {
        public string Name { get; set; } = &quot;&quot;;
        public BuildingSize Size { get; set; } = BuildingSize.bsNone;
        public int Percent { get; set; }
        public int Cost { get; set; }
        public BuildType Type { get; set; }
}

class CityArea {
        public char State { get; set; }
        public char Type { get; set; }
        public char Density { get; set; }
        public int Points { get; set; }
        public string AreaName { get; set; }
        public static List AllBuildings = new List();		
}</code></pre>
<p>This is the code to save out <strong>areas</strong> (which is <strong>List&lt;CityArea&gt; areas</strong> to the file. I found this by reading the <a title="Link to Microsoft documentation on serialization and deserialization" href="https://docs.microsoft.com/en-us/dotnet/standard/serialization/" target="_blank" rel="noopener noreferrer">Microsoft documentation about Serialization and deserialization</a>. It&#8217;s pretty good stuff and the code for saving these structures are these pretty short methods.</p>
<pre><code class="language-csharp" data-line="">        public bool SaveToUTF8(string filename)
        {
            var options = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            var bytes = JsonSerializer.SerializeToUtf8Bytes(areas, options);
            File.WriteAllBytes(filename, bytes);
            return true;
        }</code></pre>
<p>and this is the code to Load it.</p>
<pre><code class="language-csharp" data-line="">        public bool Load(string filename)
        {
            try
            {
                var bytes = File.ReadAllText(filename);
                areas = JsonSerializer.Deserialize&lt;CityArea[][]&gt;(bytes);
            }catch(Exception e)
            {
                Error = $&quot;Failed to load {filename} because {e.Message}&quot;;
                return false;
            }
            return true;
        }</code></pre>
<p>I haven&#8217;t timed either Load or Save but they are fractions of a second.</p>The post <a href="https://learncgames.com/actually-going-schemaless-in-csharp/">Actually going schemaless in C#</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1530</post-id>	</item>
	</channel>
</rss>
