📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 105行目: | 105行目: | ||
<br> | <br> | ||
==== JSONファイルの書き込み ==== | ==== JSONファイルの書き込み ==== | ||
以下の例では、非同期処理およびストリーミング処理を使用して、JSONファイルを書き込んでいる。<br> | |||
<br> | <br> | ||
具体的には、JsonSerializer. | 具体的には、JsonSerializer.SerializeAsyncメソッドを使用して、オブジェクトをJSONに非同期でシリアライズして、ファイルに書き込んでいる。<br> | ||
また、FileStreamクラスを使用してストリーミング処理することにより、大きなJSONファイルを扱う場合にもメモリ効率が向上している。<br> | また、FileStreamクラスを使用してストリーミング処理することにより、大きなJSONファイルを扱う場合にもメモリ効率が向上している。<br> | ||
<br> | <br> | ||
| 163行目: | 163行目: | ||
await JsonFileWriter.WriteJsonFileAsync(writeFilePath, data); | await JsonFileWriter.WriteJsonFileAsync(writeFilePath, data); | ||
} | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== 静的な方法 : JSONデータ構造に対応するC#のクラスの定義 == | |||
==== JSONデータ構造に対応するクラス ==== | |||
<syntaxhighlight lang="c#"> | |||
using System.Text.Json.Serialization; | |||
public class Person | |||
{ | |||
[JsonPropertyName("name")] | |||
public string Name { get; set; } | |||
[JsonPropertyName("age")] | |||
public int Age { get; set; } | |||
[JsonPropertyName("hobbies")] | |||
public List<string> Hobbies { get; set; } | |||
public Person() | |||
{ | |||
Hobbies = new List<string>(); | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
静的な方法を使用することにより、型安全性が向上して、IDEの補完機能を活用できる等のメリットがある。<br> | |||
また、JSONデータの構造が変更された場合でも、Personクラスを修正するだけで対応できるため、保守性も高くなる。<br> | |||
<br> | |||
==== JSONファイルの読み込み ==== | |||
以下の例では、非同期処理およびストリーミング処理を使用して、JSONファイルを読み込んでいる。<br> | |||
<br> | |||
具体的には、JsonSerializer.DeserializeAsync<Person>メソッドを使用して、JSONデータを非同期でPersonオブジェクトにデシリアライズしている。<br> | |||
また、FileStreamクラスを使用してストリーミング処理することにより、大きなJSONファイルを扱う場合にもメモリ効率が向上している。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
using System.Text.Json; | |||
using System.Threading.Tasks; | |||
public class StaticJsonFileReader | |||
{ | |||
public static async Task<Person> ReadJsonFileAsync(string filePath) | |||
{ | |||
try | |||
{ | |||
using FileStream fileStream = File.OpenRead(filePath); | |||
var options = new JsonSerializerOptions | |||
{ | |||
PropertyNameCaseInsensitive = true | |||
}; | |||
Person person = await JsonSerializer.DeserializeAsync<Person>(fileStream, options); | |||
if (person != null) | |||
{ | |||
Console.WriteLine($"名前: {person.Name}"); | |||
Console.WriteLine($"年齢: {person.Age}"); | |||
Console.WriteLine("趣味:"); | |||
foreach (var hobby in person.Hobbies) | |||
{ | |||
Console.WriteLine($"- {hobby}"); | |||
} | |||
} | |||
return person; | |||
} | |||
catch (FileNotFoundException) | |||
{ | |||
Console.WriteLine("エラー: 指定されたファイルが存在しない"); | |||
} | |||
catch (JsonException ex) | |||
{ | |||
Console.WriteLine($"JSONパースエラー: {ex.Message}"); | |||
} | |||
catch (Exception ex) | |||
{ | |||
Console.WriteLine($"予期せぬエラーが発生: {ex.Message}"); | |||
} | |||
return null; | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
以下の例では、上記のクラスを使用してJSONファイルを読み込んでいる。<br> | |||
<syntaxhighlight lang="c#"> | |||
class Program | |||
{ | |||
static async Task Main(string[] args) | |||
{ | |||
string readFilePath = "sample.json"; | |||
// JSONファイルの読み込み | |||
Person person = await StaticJsonFileReader.ReadJsonFileAsync(readFilePath); | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== JSONファイルの書き込み ==== | |||
以下の例では、非同期処理およびストリーミング処理を使用して、JSONファイルを書き込んでいる。<br> | |||
<br> | |||
具体的には、JsonSerializer.SerializeAsyncメソッドを使用して、PersonオブジェクトをJSONに非同期でシリアライズして、ファイルに書き込んでいる。<br> | |||
また、FileStreamクラスを使用してストリーミング処理することにより、大きなJSONファイルを扱う場合にもメモリ効率が向上している。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
using System.Text.Json; | |||
using System.Threading.Tasks; | |||
public class StaticJsonFileWriter | |||
{ | |||
public static async Task WriteJsonFileAsync(string filePath, Person person) | |||
{ | |||
try | |||
{ | |||
using FileStream createStream = File.Create(filePath); | |||
var options = new JsonSerializerOptions | |||
{ | |||
WriteIndented = true // 整形されたJSONを出力 | |||
}; | |||
await JsonSerializer.SerializeAsync(createStream, person, options); | |||
Console.WriteLine("JSONファイルの書き込みが完了"); | |||
} | |||
catch (UnauthorizedAccessException) | |||
{ | |||
Console.WriteLine("エラー: ファイルへの書き込み権限がない"); | |||
} | |||
catch (JsonException ex) | |||
{ | |||
Console.WriteLine($"JSONシリアライズエラー: {ex.Message}"); | |||
} | |||
catch (Exception ex) | |||
{ | |||
Console.WriteLine($"予期せぬエラーが発生: {ex.Message}"); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
以下の例では、上記のクラスを使用してJSONファイルを書き込んでいる。<br> | |||
<syntaxhighlight lang="c#"> | |||
class Program | |||
{ | |||
static async Task Main(string[] args) | |||
{ | |||
string writeFilePath = "sample.json"; | |||
// JSONファイルの読み込み | |||
Person person = await StaticJsonFileReader.ReadJsonFileAsync(readFilePath); | |||
if (person != null) | |||
{ | |||
// 読み込んだデータを修正 | |||
person.Age++; | |||
person.Hobbies.Add("プログラミング"); | |||
// 修正したデータをJSONファイルに書き込み | |||
await StaticJsonFileWriter.WriteJsonFileAsync(writeFilePath, person); | |||
} | |||
} | } | ||
} | } | ||