无法将枚举从 c# 代码生成为 json 字符串



我正在使用JsonForm在我的MVC核心Web应用程序中生成动态表单。我在视图模型中使用以下代码中的System.Text.Json.JsonSerializer.Serialize来生成一个简单的单字段表单。我的目标是最终将此 json 存储在数据库中并从那里检索它。

public TestsViewModel GetFormControls1()
{
var myJsonModel = new
{
schema = new
{
client = new
{
type = "object",
title = "Client",
properties = new
{
forename = new
{
type = "string",
title = "Forename",
minLength = 3,
maxLength = 10,
}
}
}
},
form = new List<Object>
{
new {
key = "client.forename",
title = "Forename"
}
},
value = new
{
client = new
{
forename = "",
}
}
};
TestsViewModel homeVm = new TestsViewModel();
homeVm.FormControls = System.Text.Json.JsonSerializer.Serialize(myJsonModel);
return homeVm;
}

上面的代码工作正常,并生成以下 json 架构,然后用于创建表单。

{
"schema": {
"client": {
"type": "object",
"title": "Client",
"properties": {
"forename": {
"type": "string",
"title": "Forename",
"minLength": 3,
"maxLength": 10
}
}
}
},
"form": [
{
"key": "client.forename",
"title": "Forename"
}
],
"value": {
"client": {
"forename": ""
}
}
}

我现在需要为我的 json 生成一个枚举,以便表单中可以显示用于选择性别的下拉列表。但是,我无法通过 c# 代码做到这一点。有人可以帮忙吗?我希望我的 c# 代码产生以下 json(请注意架构和形式的性别两个条目(。

{
"schema": {
"client": {
"type": "object",
"title": "Client",
"properties": {
"forename": {
"type": "string",
"title": "Forename",
"minLength": 3,
"maxLength": 10
},
"gender": {
"type": "string",
"title": "Gender",
"enum": [
"male",
"female",
"alien"
]
}
}
}
},
"form": [
{
"key": "client.forename",
"title": "Forename"
},
{
"key": "client.gender",
"titleMap": {
"male": "Dude",
"female": "Dudette",
"alien": "I'm from outer space!"
}
}
],
"value": {
"client": {
"forename": ""
}
}
}

我尝试使用以下代码,但enum是 c# 中的一个关键字,所以我得到错误。

gender = new
{
type = "string",
title = "Gender",
enum = "[male, female, alien]"
}

此外,Enum = "[male, female, alien]"产生"Enum": "[male, female, alien]"而不是"enum": [ "male", "female", "alien" ]

我有一个性别查找表,我最终将用它来以某种方式生成枚举以上,因此任何有关此的想法也会有所帮助。

UPDATE@dbc的评论为我的大部分问题提供了解决方案。但是,如果我尝试将字符串映射到整数,我仍在努力生成 titleMap json。

var gender3 = new
{
type = "string",
title = "Gender",
titleMap = new List<string> { new string("1" + ":" + "Male"), new string("2" + ":" + "Female")}
};

上面的代码产生

{
"type": "string",
"title": "Gender",
"titleMap": [
"1:Male",
"2:Female"
]
}

但是,我需要 1 和 Male 在 { } 内自己的双引号中,而不是 [ ],如下所示。

{
"type": "string",
"title": "Gender",
"titleMap": {
"1": "Male",
"2": "Female"
}
}

枚举是 C# 中的一个关键字,因此您需要在枚举前面加上 @

gender = new
{
type = "string",
title = "Gender",
@enum = new string[3]{"male", "female", "alien"}
}

据我了解,您正在尝试将枚举值数组序列化为JSON的文本名称。最简单的解决方案是将枚举属性的数组或列表添加到TestViewModel,并使用JsonStringEnumConverter转换器将它们序列化为字符串而不是数字。

以下是如何实现所需目标的示例。

假设您有一些带有一组值的枚举:

//This is an arbitrary enum, this could be 'Gender', in your case
public enum TestEnum
{
value1,
value2,
value3,
value4,
}

你想写下这些枚举值的数组。将枚举属性列表(或枚举数组(添加到模型中。如果您希望 JSON 中的属性名称是保留字之一(如enum(,请使用JsonPropertyName属性覆盖该名称(并以编程方式保留最有意义的名称(:

public class TestsViewModel_Option1
{
// In your case, this property could be called 'Genders' to be self-documenting
[JsonPropertyName("enum")]
public List<TestEnum> ListOfEnums { get; set; }
}

或者,如果您确实希望属性名称成为保留关键字,并且由于某种原因不想/不能使用该属性,请使用@符号。

public class TestsViewModel_Option2
{
// Or use fixed-size array, TestEnum[], if needed
public List<TestEnum> @enum { get; set; }
}

现在这就是您的代码在JsonSerializer中的样子:

private static void SerializeListOfEnums()
{
var model1 = new TestsViewModel_Option1
{
ListOfEnums = { TestEnum.value1, TestEnum.value3 }
};
var options = new JsonSerializerOptions
{
Converters = { new JsonStringEnumConverter() }
};
// {"enum":["value1","value3"]}
Console.WriteLine(JsonSerializer.Serialize(model1, options));
var model2 = new TestsViewModel_Option2
{
@enum = { TestEnum.value1, TestEnum.value3 }
};
// {"enum":["value1","value3"]}
Console.WriteLine(JsonSerializer.Serialize(model2, options));
}

最新更新