.Net 6: C# で JSON の Update

.Net のバージョン

```text
$ dotnet --version
6.0.302
```

プロジェクトの作成

```text
mkdir Update01
cd Update01
dotnet new console
```

```cs:Program.cs
﻿// --------------------------------------------------------------------
//	Program.cs
//
//						Aug/07/2022
// --------------------------------------------------------------------
using System;  
using System.IO;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

// --------------------------------------------------------------------
namespace Update01
{
	class Program
	{
	static void Main(string[] args)
	{
		Console.Error.WriteLine("*** 開始 ***");

		string file_json = args[0];

		string	key_in = args[1];
		int	population_in = int.Parse (args[2]);
		Console.WriteLine (key_in + "\t" + population_in);

		string str_json = file_io.file_to_str_proc (file_json);

		Dictionary <string,Object> dict_aa = JsonSerializer.Deserialize
					  <Dictionary <string,Object>> (str_json)!;

		if (dict_aa.ContainsKey (key_in))
		{
		dict_aa = dict_update_proc (dict_aa,key_in,population_in);
		string str_json_out = JsonSerializer.Serialize(dict_aa);

		file_io.file_write_proc (file_json,str_json_out);
		}

	Console.Error.WriteLine("*** 終了 ***");
	}

// --------------------------------------------------------------------
static Dictionary <string,Object> dict_update_proc
	(Dictionary <string,Object> dict_aa,String key_in,int population_in)
{
	Console.WriteLine ("*** update_proc bbb *** key_in = " + key_in);

	String json_unit = JsonSerializer.Serialize(dict_aa[key_in]);
	Console.WriteLine(json_unit);

	Dictionary <string,Object> unit_aa = JsonSerializer.Deserialize
				<Dictionary <string,Object>> (json_unit)!;

	String str_population = population_in.ToString ();

	DateTime dateNow = DateTime.Now;
	string str_date = dateNow.ToString ();

	unit_aa["population"] = str_population;
	unit_aa["date_mod"] = str_date;

	dict_aa[key_in] = unit_aa;

	return	dict_aa;
}

// --------------------------------------------------------------------
	}
}

// --------------------------------------------------------------------
```

```cs:file_io.cs
// --------------------------------------------------------------------
/*
	file_io.cs

				Aug/07/2022


*/
// --------------------------------------------------------------------
using   System;
using   System.IO;
using   System.Text;

// --------------------------------------------------------------------
public class    file_io
{
// --------------------------------------------------------------------
public static string file_to_str_proc (string file_in)
{
	StreamReader fp_in = new StreamReader (file_in);

	string  buff;

	StringBuilder stb = new StringBuilder ();

	while ((buff = fp_in.ReadLine ()!) != null)
		{
		stb.Append (buff);
		}

	fp_in.Close();

	string  str_in = stb.ToString ();

	return  str_in;
}

// --------------------------------------------------------------------
public static void file_write_proc (string file_name,string str_out)
{
	try
		{
	StreamWriter fp_out = new StreamWriter (file_name);

	fp_out.Write (str_out);

	fp_out.Close ();
		}
	catch
		{
		Console.Error.WriteLine ("*** error *** file_write_proc ***");
		}
}
// --------------------------------------------------------------------

}
// --------------------------------------------------------------------
```

コンパイル

```text
dotnet build
```

実行

```bash
bin/Debug/net6.0/Update01 /var/tmp/json/tochigi.json t0922 3569800
```