简体   繁体   English

无效的操作异常,序列中没有元素

[英]Invalid Operation Exception , No elements in the sequence

private static void WriteJson(string filepath, 
                              string filename, 
                              JsonSchema jsonschema)
        {
        using (TextWriter writer = File.CreateText(
                       @"C:\Users\ashutosh\Desktop\Output\" + filename + ".js"))
        using (var jtw = new JsonTextWriter(writer))
            {
            jtw.Formatting = Formatting.Indented;
            jsonschema.WriteTo(jtw);
            }
        //var json = JsonConvert.SerializeObject(
        //        jsonschema, Formatting.Indented, 
        //        new JsonSerializerSettings { 
        //                 NullValueHandling = NullValueHandling.Ignore });
        //    File.WriteAllText(
        //       @"C:\Users\ashutosh\Desktop\Output\" + filename + ".js", json);
        }

I am creating a JSONSchema from JSON.net , and then writing it out . 我正在从JSON.net创建一个JSONSchema,然后将其写出。 I get a 我得到一个

Invalid Operation Exception Sequence contains no matching element

But when I use the commented code instead of the usual stuff. 但是,当我使用注释的代码而不是通常的东西时。 No such exception appears. 没有出现此类异常。

1) What is causing this exception? 1)是什么导致此异常? 2) I would have used the second method happily but it doesn't feel intuitive and it will print out the integer value of the JsonType for schema.Type instead of the (array,integer,bool etc..) 2)我本来会愉快地使用第二种方法,但是它并不直观,它将打印出用于模式的JsonType的整数值。Type而不是(数组,整数,布尔等)。

What can I do to get out of this situation? 我要怎么做才能摆脱这种情况?

UPDATE The exception happens when the " Properties " property of the JsonSchema has count = 0 . 更新JsonSchema的“ Properties ”属性具有count = 0时,将发生异常。 Properties is Dictionary<String,JsonSchema> . PropertiesDictionary<String,JsonSchema> I have initialise it so it is not null. 我已经对其进行了初始化,因此它不为null。 Eventually the code may or may not add elements to it . 最终,代码可能会或可能不会在其中添加元素。 so , the count may remain 0. 因此,计数可能保持为0。

By default, enums will be serialized to theirs corresponding integer value. 默认情况下,枚举将被序列化为其对应的整数值。 You can change that easily by supplying StringEnumConverter in your serializer settings: 您可以通过在序列化程序设置中提供StringEnumConverter轻松更改此设置:

var json = JsonConvert.SerializeObject(jsonschema, Formatting.Indented,
    new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore,
        Converters = new List<JsonConverter> { new StringEnumConverter() }
    });

Edit : 编辑

I run this simple test code: 我运行以下简单的测试代码:

var schema = new JsonSchemaGenerator().Generate(typeof(CustomType));
Debug.Assert(schema.Properties.Count == 0);
using (TextWriter textWriter = File.CreateText(@"schema.json"))
using (var jsonTextWriter = new JsonTextWriter(textWriter))
{
    jsonTextWriter.Formatting = Formatting.Indented;
    schema.WriteTo(jsonTextWriter);
}

// CustomType is a class without any fields/properties
public class CustomType { }

Code above serializes schema correctly to: 上面的代码将架构正确序列化为:

{
    "type": "object",
    "properties": {}
}

Is the schema you are generating correct? 您生成的架构正确吗? It seems as if serializer was "thinking" it should deal with some properties when there are actually none. 似乎串行器正在“思考”,实际上应该没有属性。 Can you show type you generate schema from? 您可以显示生成架构的类型吗? There might be a problem with type which causes invalid schema to generate - but still, I cannot reproduce it. 类型可能存在问题,导致生成无效的架构-但仍然无法重现它。

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

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