Newtonsoft序列化和反序列化linq2sql实体会抛出异常

时间:2018-03-20 16:59:20

标签: c# linq-to-sql json.net

从ASPNET Core WebApi应用程序我返回

List<Profile>

其中Profile是包含

的linq2sql对象
EntitySet<Location> Locations

ASPNET Core默认使用Newtonsoft Json序列化程序,这是方法的签名:

[HttpPost]
public List<Profile> FindProfile([FromBody] QueryData queryObject)
{
   ...
   return list;
}

在客户端,我使用Newtonsoft反序列化Json,如下所示:

TResult result = JsonConvert.DeserializeObject<TResult>(resultString,
                new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });

但我收到以下例外:

“指定的参数超出了有效值范围。”

Json对我来说很好看:

[
  {
    "$id": "1",
    "birthday": "1980-10-10T00:00:00",
    "canBeRecommended": false,
    "civilStatus": 0,
    "countryOfOriginId": 0,
    "sex": 2,    
    "userId": 123,
    "locations": [
      {
        "$id": "2",
        "address": "Street 1",
        "city": "New York",
        "countryId": 1,
        "profile": {
          "$ref": "1"
        }
      }
    ]
  }
]

如果删除

,实际上可以反序列化
    "profile": {
      "$ref": "1"
    }

我尝试过使用设置

PreserveReferencesHandling.None

PreserveReferencesHandling.All

当然在服务器和客户端同步,但没有成功。

有什么想法吗?

更新例外信息

ExceptionType:System.ArgumentOutOfRangeException 没有内在的例外。 ToString() - 对不起,用德语:

"System.ArgumentOutOfRangeException: Das angegebene Argument liegt außerhalb des gültigen Wertebereichs.
Parametername: value
   bei System.Data.Linq.EntitySet`1.System.Collections.IList.Add(Object value)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadMetadataProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadMetadataProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   bei Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   bei Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   bei Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   bei <anonymized>

最小的例子 https://github.com/copiltembel/JSonDeserializeExample/tree/master/JSonDeserializeExample

1 个答案:

答案 0 :(得分:0)

我放弃并切换到使用DataContractSerializer,它似乎能够处理这类对象。