ä»æ¥ããã¤ã¯ãã½ããæ¬ç¤¾ããæ¥ã David ããã¨ãæ¼ã飯ãããã¨ãã«æãã¦ããããå½¼ã®ãããã¯ããåãã£ãã®ã§ç´¹ä»ãããã¨æãã¾ãã
quicktype
JSON ããã¼ã¹ãããã¨ã·ãªã¢ã©ã¤ãºã»ãã·ãªã¢ã©ã¤ãºããããã®ã³ã¼ããåºåãã¦ããããµã¼ãã¹ã§ãã
ä¾ãã°ã以ä¸ã®ãã㪠JSON ãããã¨ãã¾ãã
{ "name": "Kazuki Ota", "age": 36, "pets": [ { "name": "hoge" }, { "name": "foo" } ] }
ãã¼ã¸ã«å¼µãä»ããã¨ããããã³ã¼ããçæããã¾ãã
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: // // using Sample.Models; // // var data = Person.FromJson(jsonString); // namespace Sample.Models { using System; using System.Net; using System.Collections.Generic; using Newtonsoft.Json; public partial class Person { [JsonProperty("age")] public long Age { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("pets")] public Pet[] Pets { get; set; } } public partial class Pet { [JsonProperty("name")] public string Name { get; set; } } public partial class Person { public static Person FromJson(string json) => JsonConvert.DeserializeObject<Person>(json, Converter.Settings); } public static class Serialize { public static string ToJson(this Person self) => JsonConvert.SerializeObject(self, Converter.Settings); } public class Converter { public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore, DateParseHandling = DateParseHandling.None, }; } }
ç»é¢ã¯ãããªæãã§ãã使ãæ¹ã«è¿·ããã¨ã¯ãªãã¨æãããã
Visual Studio ã«ãçµã¿è¾¼ã¿ã§ JSON ããã³ã¼ããçæããæ©è½ãããã®ã§ãããã«ã¼ãã®ã¯ã©ã¹åã決ãããªãã£ãããããããã£åã JSON ã®ããããã£åã¨åãã«ãªãã®ã§å°æåããå§ã¾ãããããã£ã«ãªã£ãããã¦ããã«ããããã®ã§ããããããã¸ãããã£ã¡ãèãããã¦ãï¼ï¼
ä»åã¯ãC# ã®åºåã³ã¼ãä¾ã示ãã¾ãããä»ã®è¨èªã«ã対å¿ãã¦ãã¾ãã
- Swift
- TypeScript
- Go
- C#
- Java
- etc...
ä¾ãã° TypeScript ã ã¨ããããæãã«ãªãã¾ãã
// To parse this data: // // import { Convert, Person } from "./file"; // // const person = Convert.toPerson(json); export interface Person { age: number; name: string; pets: Pet[]; } export interface Pet { name: string; } // Converts JSON strings to/from your types export module Convert { export function toPerson(json: string): Person { return JSON.parse(json); } export function personToJson(value: Person): string { return JSON.stringify(value, null, 2); } }
quicktype ã¯æ¬å½ã«è³¢ãã¦ã以ä¸ã®ããã«è¤æ°ã®åãå ¥ãæ··ãã£ãé åã® JSON ã渡ãã¨â¦
{ "items": [ "a", "b", 3 ] }
ã¡ããã¨åãããæãã«ãã¦ããã¾ãã
// To parse this data: // // import { Convert, Person } from "./file"; // // const person = Convert.toPerson(json); export interface Person { items: Array<number | string>; } // Converts JSON strings to/from your types export module Convert { export function toPerson(json: string): Person { return JSON.parse(json); } export function personToJson(value: Person): string { return JSON.stringify(value, null, 2); } }
ãã®ãã¯ã¼ããã¼ã«ã«ã§
ãµã¤ãã®ä¸ã®æ¹ã«æ°ã«ãªã1æãæ¸ãã¦ããã¾ãã
npm i -g quicktype
ã¨ãããã¨ã§ nodejs ã¯å°å ¥æ¸ã¿ãªã®ã§ãã¿ã¼ããã«ãéãã¦ãã£ã¦ã¿ã¾ããã
C:\Users\xxxxx>npm i -g quicktype C:\Users\xxxxx\AppData\Roaming\npm\quicktype -> C:\Users\xxxxx\AppData\Roaming\npm\node_modules\quicktype\quicktype.js + [email protected] updated 1 package in 2.711s
å ¥ã£ãï¼quicktype ã¨æã¡è¾¼ãã¨ä½¿ãæ¹ãåºã¦ãã¾ããå ±éã®ãªãã·ã§ã³ã¨ C# ã®é¨åã ãæç²ãã¦ã¿ã¾ããã
Synopsis $ quicktype [--lang cs|go|c++|java|ts|swift|elm|schema|types] FILE|URL ... Description Given JSON sample data, quicktype outputs code for working with that data in C#, Go, C++, Java, TypeScript, Swift, Elm, JSON Schema, Simple Types. Options -o, --out FILE The output file. Determines --lang and --top-level. -t, --top-level NAME The name for the top level type. -l, --lang cs|go|c++|java|ts|swift|elm|schema|types The target language. -s, --src-lang json|schema The source language (default is json). --src FILE|URL|DIRECTORY The file, url, or data directory to type. --src-urls FILE Tracery grammar describing URLs to crawl. --no-combine-classes Don't combine similar classes. --no-maps Don't infer maps, always use classes. --quiet Don't show issues in the generated code. -h, --help Get some help. Options for C# --namespace NAME Generated namespace --csharp-version 6|5 C# version --density normal|dense Property density --array-type array|list Use T[] or List<T> --features complete|attributes-only|just-types Output features
ãªãã¨ãªãããããã³ãã³ãããåãã°ãã£ã¡ã®ããã§ããããä¾ãã° Visual Studio 2017 ã§é©å½ãªããã¸ã§ã¯ããä½ãã¾ãã
ããã¦ãé©å½ãª JSON ãæºåãã¾ããããã辺ã®ã使ã£ã¦ã¿ããããªãSwagger(Open API) ã®ãã¡ã¤ã«ã£ã½ãã
ããã¸ã§ã¯ãã®ä¸ã« datasources ãã©ã«ããä½ã£ã¦ petstore.url ã¨ãããã¡ã¤ã«ãä½ã£ã¦ä¸è¨ãµã¤ãã® JSON ã® Raw ãã¼ã¿ã¸ã® URL ããºãã£ã¨è²¼ãã¾ããããã¸ã§ã¯ãã¯ãããªæãã«ãªãã¾ããã
ããã¸ã§ã¯ãã®ããããã£ã®ãã«ãåã¤ãã³ãã®ã³ãã³ãã©ã¤ã³ã«ä»¥ä¸ã®ãããªã³ãã³ããæ¸ãã¦ããã¾ããåç´ã« qicktype ã³ãã³ããæ¸ãã¦ãã ãã§ããããã©ã«ããæå®ããã ãã§ãããã«ãã json ã url ãã¡ã¤ã«ã対象ã«ãã¦ããã¾ãã
quicktype $(ProjectDir)datasources -o $(ProjectDir)Entities\Petstore.cs --namespace QuickTypeDemo.Entities --csharp-version 6 --features complete
ãã«ããã¦ãããã¸ã§ã¯ãã§ãã¹ã¦ã®ãã¡ã¤ã«ã表示ããã¨ç¢ºãã«åºæ¥ä¸ãã£ã¦ããã¨ã確èªã§ãã¾ãã
ããã¸ã§ã¯ãã«è¿½å ãã¦ããã¾ãããããããªã«ããããï¼ã¾ã ä¸ã«ç¶ãã¦ã¾ãï¼ã¯ã©ã¹ãçæãã¦ããã¾ããã
JSON.NET ãåç §ã«è¿½å ãã¦ã以ä¸ã®ãããªã³ã¼ããæ¸ãã¦ã¿ã¾ããã
using QuickTypeDemo.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace QuickTypeDemo { class Program { static void Main(string[] args) { var json = new HttpClient().GetStringAsync("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore.json").Result; var petstore = Petstore.FromJson(json); Console.WriteLine(petstore.Info.Title); } } }
ãã¬ã¼ã¯ãã¤ã³ãã§æ¢ãã¦ã¿ãããã¡ããã¨èªã¿è¾¼ãã¦ããã¨ããããã¾ããã
ä»ã«ã
Xcode 㨠VSCode ã§ä½¿ãæ¹æ³ã¯å ¬å¼ã® Blog ã«è¨äºãããã¾ãã
ããã«ãä»åã¯ã³ãã³ãããã«ãã¢ã¯ã·ã§ã³ã§å©ãæãã§ããã¾ããããIDE ãªã©ã®ãã¼ã«ã¨ã®ã¤ã³ãã°ã¬ã¼ã·ã§ã³ã¯èãããã¦ãã¿ããã§ããç´ æµï¼ï¼
IDE/editor integration
ã¾ã¨ã
JSON ãæ ¼ç´ããããã®åå®ç¾©ã«ãæ©ã¿ã®äººã¯è©¦ãã¦ã¿ãã¨ããã¨æãã¾ããï¼åãï¼è³¢ãï¼ å人çã«ãä»ã¾ã§è§¦ã£ã¦ãã JSON ããã¼ã¹ãã¦ã¯ã©ã¹å®ç¾©ãåºåãã¦ãããã»ãã®ãµã¼ãã¹ã¨æ¯ã¹ã¦æç¶ä½¿ããããã¨æãã¾ããããã¼ã«ã«ã³ãã³ããæä¾ããã¦ãã®ããã¤ãã§ããã
IDE ãã¨ãã£ã¿ã¨ã®é£æºãæ¬å½ã«ã·ã¼ã ã¬ã¹ã«ãªãã¨ãã°ããã¨ã«ãªããããªæ°ããã¾ãã