programing

C# 클래스에서 JSON 스키마를 생성하는 중

telebox 2023. 2. 26. 09:34
반응형

C# 클래스에서 JSON 스키마를 생성하는 중

C# 클래스에서 JSON 스키마를 프로그래밍 방식으로 생성할 수 있는 방법이 있습니까?

http://www.jsonschema.net/을 사용하여 수동으로 할 수 있는 작업

JSON Schema v4 생성을 지원하는 다른 옵션은 NJsonSchema입니다.

var schema = JsonSchema.FromType<Person>();
var schemaJson = schema.ToJson();

라이브러리는 NuGet을 통해 설치할 수 있습니다.

NJsonSchema v9.4.3+ 업데이트:

using NJsonSchema;

var schema = await JsonSchema.FromTypeAsync<Person>();
var schemaJson = schema.ToJson();

이것은 Json에서 지원됩니다.뉴턴소프트 경유로 NET.Json.Schema NuGet 패키지.사용방법은 공식 매뉴얼에 기재되어 있습니다만, 아래에 간단한 예를 기재했습니다.

JSchemaGenerator generator = new JSchemaGenerator();
JSchema schema = generator.Generate(typeof(Person));
Console.WriteLine(schema.ToString());
//{
//  "type": "object",
//  "properties": {
//    "Name": {
//      "type": [ "string", "null" ]
//    },
//    "Age": { "type": "integer" }
//  },
//  "required": [ "Name", "Age" ]
//}
JsonSchemaGenerator js = new JsonSchemaGenerator();
var schema = js.Generate(typeof(Person));
schema.Title = typeof(Person).Name;
using (StreamWriter fileWriter = File.CreateText(filePath))
{
      fileWriter.WriteLine(schema);
}

구글에서 여기로 와서 그 반대쪽을 찾는 사람들을 위해
(JSON에서 C# 클래스를 생성) - 다음과 같은 훌륭한 온라인 도구를 사용합니다.

JSON:
http://json2csharp.com/
(출처 : http://jsonclassgenerator.codeplex.com/)

XML:
http://xmltocsharp.azurewebsites.net/
(출처 : https://github.com/msyoung/XmlToCSharp)

언급URL : https://stackoverflow.com/questions/15782982/generating-json-schema-from-c-sharp-class

반응형