简体   繁体   中英

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 . 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? 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..)

What can I do to get out of this situation?

UPDATE The exception happens when the " Properties " property of the JsonSchema has count = 0 . Properties is Dictionary<String,JsonSchema> . I have initialise it so it is not null. Eventually the code may or may not add elements to it . so , the count may remain 0.

By default, enums will be serialized to theirs corresponding integer value. You can change that easily by supplying StringEnumConverter in your serializer settings:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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