計算機科学のブログ

reading and writing files - Save the last byte for me! - JSON, JsonSerializer, Serialize method

Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 10(reading and writing files - Save the last byte for me!)、p.559(Sharpen your pencil)の解答を求めてみる。

コード

Program.cs

using System.Text.Json;

List<string> strings = [
    JsonSerializer.Serialize(3),
    JsonSerializer.Serialize((long)-3),
    JsonSerializer.Serialize((byte)0),
    JsonSerializer.Serialize(float.MaxValue),
    JsonSerializer.Serialize(float.MinValue),
    JsonSerializer.Serialize(true),
    JsonSerializer.Serialize("Elephant"),
    JsonSerializer.Serialize("Elephant".ToCharArray()),
    JsonSerializer.Serialize("🐘"),
];
// 3
// -3
// 0
// 3.4028235E+38
// -3.4028235E+38
// true
// "Elephant"
// ["E", "l", "e", "p", "h", "a", "n", "t"]
// "🐘"
foreach (var item in strings)
{
    Console.WriteLine(item);
}

入出力結果(Terminal, Zsh)

% dotnet run
3
-3
0
3.4028235E+38
-3.4028235E+38
true
"Elephant"
["E","l","e","p","h","a","n","t"]
"\uD83D\uDC18"
%