简体   繁体   English

使用包含空格的枚举将 Json 反序列化为 NJsonSchema 生成的对象

[英]Deserialization of Json to NJsonSchema generated object with enums containing spaces

need your help.需要你的帮助。

So, i have json schema and i used NJsonSchema.CodeGeneration tool to generate .cs model.所以,我有 json 模式,我使用NJsonSchema.CodeGeneration工具来生成 .cs 模型。 I was able of using it to deserialize json into object successfully until the input json contained enum values with spaces in it.我能够使用它成功地将 json 反序列化为对象,直到输入的 json 包含带有空格的枚举值。

Here is what i am talking about这就是我要说的

In schema i have a property like:在架构中,我有一个属性,如:

...
prop1: {
                enum: [ 'with space', 'withoutspace' ],
            },
...

In generated .cs model i have a corresponding enum:在生成的 .cs 模型中,我有一个相应的枚举:

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "7.10.6235.25398")]
    public enum Some_prop1
    {
        [System.Runtime.Serialization.EnumMember(Value = "with space")]
        With_Space = 0,

        [System.Runtime.Serialization.EnumMember(Value = "withoutspace")]
        Withoutspace= 1,
    }

And here are two input examples: Json 1:这是两个输入示例:Json 1:

{
    ...
    "prop1": "with space",
    ...
} 

Json 2: JSON 2:

{
    ...
    "prop1": "withoutspace",
    ...
} 

And then i just deserialize it when i need it:然后我只是在需要时反序列化它:

var someModel = Some.FromJson(json);

It works the way it should for a Json 2, but it fails to recognize enum value with space with an error like this:它以 Json 2 应有的方式工作,但它无法识别带有空格的枚举值,并出现如下错误:

"Error converting value \\"with space\\" to type 'com.company.model.Some_prop1'. Path 'prop1', line 7, position 24." “将值\\”带空格\\”转换为类型'com.company.model.Some_prop1' 时出错。路径'prop1',第7 行,位置24。”

So here is my main question: is there any way to deal with such situation without changing the format of enum in schema?所以这是我的主要问题:有没有办法在不改变架构中枚举格式的情况下处理这种情况?

Small additional question: while investigating i faced one more problem - if i am to use serialization like some.ToJson() my enums will be generated in a way like "prop1": 0 , using int< and since json is used as user friendly way to change things, i need it in a way like "prop1": "with space" .额外的小问题:在调查时我遇到了另一个问题 - 如果我要使用像 some.ToJson() 这样的序列化,我的枚举将以类似于"prop1": 0的方式生成,使用 int< 并且因为 json 被用作用户友好改变事物的方式,我需要像"prop1": "with space" 这样的方式

Regards, Kanstantsin.问候,康斯坦丁。

I accidentally found answer myself and hope it would be useful to anyone else too.我不小心自己找到了答案,希望它对其他人也有用。

The thing is the default value for enum is int, so while deserializing it tries to have int value of enum or exact name of enum element.事情是 enum 的默认值是 int,因此在反序列化时,它尝试使用 enum 的 int 值或 enum 元素的确切名称。 But you can force it to understand enum as string first of all:但是您可以强制它首先将 enum 理解为字符串:

...
prop1: {
                type: "string",
                enum: [ 'with space', 'withoutspace' ],
            },
...

So in generated class StringEnumConverter attribute will be used and serialization\\deserialization will go the way i wanted it.因此,在生成的类中,将使用 StringEnumConverter 属性,序列化\\反序列化将按照我想要的方式进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM