You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Json.Net反序列化抽象类时遇'EndObject'异常,求排查

Troubleshooting "Unexpected initial token 'EndObject'" When Deserializing Abstract Class with Json.Net

Let's break down this error and figure out why your custom converter is throwing this exception specifically for CreateCRMIntegrationDirectPromptBaseBindingModel, even when other similar converters work fine. This error typically means Json.Net hits an unexpected } (EndObject token) when it was expecting to read a JSON object or array to populate your abstract class instance.

Common Causes & Fixes

1. Your Converter Doesn't Handle Empty JSON Objects

If the JSON being deserialized is an empty object ({}), your converter might not be checking for this case before trying to populate the abstract class. Json.Net will throw this error when it tries to populate an object but the reader is already at the end of the object.

Fix: Add a check at the start of your ReadJson method to handle empty objects:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    // Handle empty JSON object case
    if (reader.TokenType == JsonToken.EndObject)
    {
        return null; // Or return a default instance if your logic requires it
    }

    // Rest of your converter logic...
}

2. Missing or Incorrect Concrete Type Resolution

Abstract classes can't be instantiated directly, so your converter needs to correctly identify which concrete subclass to create. If your converter fails to resolve the right type (or the JSON lacks type metadata), Json.Net can't create an instance and will fail when trying to populate.

Checklist:

  • Does your JSON include a type identifier (like $type if you're using TypeNameHandling)? Compare it to working examples with other abstract classes.
  • Verify your converter's CanConvert method correctly targets CreateCRMIntegrationDirectPromptBaseBindingModel and its subclasses:
    public override bool CanConvert(Type objectType)
    {
        return typeof(CreateCRMIntegrationDirectPromptBaseBindingModel).IsAssignableFrom(objectType);
    }
    
  • Ensure your ReadJson method properly instantiates a concrete subclass before calling serializer.Populate. For example:
    // Get the concrete type from JSON or your business logic
    Type concreteType = DetermineConcreteType(reader, serializer);
    var instance = Activator.CreateInstance(concreteType);
    
    // Make sure the reader is positioned at the start of the object before populating
    if (reader.TokenType != JsonToken.StartObject)
    {
        reader.Read();
    }
    
    serializer.Populate(reader, instance);
    return instance;
    

3. Mismanaged JsonReader Position

JsonReader is forward-only, so if your converter moves the reader past the object's start (or to its end) before calling serializer.Populate, Json.Net will hit the EndObject token unexpectedly.

Fix:

  • Avoid over-reading with reader.Read() loops that might advance the reader past the object you need to populate.
  • Before calling serializer.Populate, confirm the reader is at JsonToken.StartObject. If not, call reader.Read() to move to the correct position.

4. Global JsonSerializerSettings Conflicts

Even if other converters work, global settings (like the order of converters in JsonSerializerSettings.Converters, or TypeNameHandling values) might be interfering with your custom converter for this specific abstract class.

Fix:

  • Explicitly apply your converter to the abstract class using the [JsonConverter] attribute to ensure it's prioritized over global converters:
    [JsonConverter(typeof(YourCustomConverter))]
    public abstract class CreateCRMIntegrationDirectPromptBaseBindingModel
    {
        // Class members...
    }
    
  • Double-check global settings like MissingMemberHandling or NullValueHandling—if these are set to strict, they might trigger errors if the JSON is missing expected properties.

5. Mismatched JSON Structure

Compare the JSON for this abstract class to the JSON that works with other converters. If this JSON is missing required properties, or has a different structure (e.g., a nested object where a flat property is expected), your converter might fail to populate the object correctly.

Test: Try deserializing a manually constructed, complete JSON for CreateCRMIntegrationDirectPromptBaseBindingModel to see if the error persists. If it works, the issue is likely with the original JSON's structure.

Quick Debugging Steps

  1. Log or inspect the exact JSON being deserialized to confirm it's not an empty object or malformed.
  2. Step through your converter's ReadJson method in debug mode to track the reader's token type at each step.
  3. Compare your converter's code line-by-line with a working converter for another abstract class—look for differences in how empty objects, type resolution, or reader positioning are handled.

内容的提问来源于stack exchange,提问作者MrChrisOsburn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:57:46