3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rider 2022.3から、JSONからC#のクラスやレコードを作る機能が追加された

Last updated at Posted at 2022-12-15

Rider 2022.3から、JSONをペーストして、C#のクラスやレコードを作る機能が追加されました。便利!

json_to_records.gif

こんなJSONをコピーまたはペーストした状態で、

{
  "results": [
    {
      "id": "C6C5D44C-E814-4740-BED1-535D18C58831",
      "score": 123.0,
      "name": "taro",
      "grade": "A",
      "birth_day": "1988-12-15T00:00:00+09:00",
      "languages": [
        {
          "id": "F73D0191-7DC1-4C82-BEA2-FE0697C4484C",
          "name": "Java"
        },
        {
          "id": "0C4F8C56-7529-4288-864E-F2A1C8908CDA",
          "name": "Kotlin"
        },
        {
          "id": "AAB44313-86AF-4C15-B0D4-8F8F2F0BE8EF",
          "name": "JavaScript"
        },
        {
          "id": "DFF25EEF-A829-45D3-BB5E-B9A6F3BD6AD7",
          "name": "TypeScript"
        }
      ]
    },
    {
      "id": "030EDCF4-E8D3-4AF1-8999-26C3EB6C74E3",
      "score": null,
      "name": "jiro",
      "grade": null,
      "birth_day": "1995-01-01T00:00:00+09:00",
      "languages": [
        {
          "id": "6D327248-6840-4A12-A563-4037A4D39472",
          "name": "C#"
        },
        {
          "id": "345CFF94-2F10-4D7E-88F4-BDC4C79FF29E",
          "name": "Golang"
        }
      ]
    },
    {
      "id": "E965CE53-1EC9-4656-B462-16C0C8FA7340",
      "name": "saburo",
      "birth_day": "2000-01-01T00:00:00+09:00",
      "languages": []
    }
  ]
}

Find Actionで「Paste Special: JSON as Records」を探して選択します。
もしくはEditorで右クリックからメニューを開き「Copy / Paste Special」 > 「Paste Special: JSON as Records」を選択します。
そうすると、次のようなレコードが生成されます。

public record RootObject(
    Results[] results
);

public record Results(
    string id,
    double? score,
    string name,
    string grade,
    string birth_day,
    Languages[] languages
);

public record Languages(
    string id,
    string name
);

取り急ぎJSONのパーサーをががーっと書きたい時などに便利そう。

もちろんパース用の型としては、これで完璧じゃないし、修正したい場所もあるけれど、とっかかりとしては十分な気がします。

「Paste Special: JSON as Classes」だと、クラスも作れます。

リンク集

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?