JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.71k stars 3.24k forks source link

Can't deserialize object just serialized. #2911

Open PaulStSmith opened 10 months ago

PaulStSmith commented 10 months ago

At some moment in my code, I had to do a variation of this:

class Foo
{
    public readonly IList<Bar> MyProperty = new List<Bar>();
}

store = JsonConvert.DeserializeObject<Foo>(JsonConvert.SerializeObjct(new Foo()));

Then I get the following exception:

Error converting value "Foo" to type 'Foo'. Path '', line 1, position 42.

Which in turn had the following source:

Could not cast or convert from System.String to Foo.

Debugging the serialized string it had the value "Foo" instead of the expected:

{
  MyProperty : []
}

StackTrace for the first Exception:

   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
   at MyCode . . . 

StackTrace for the InnerException:

   at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
   at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
elgonzo commented 10 months ago

Not reproducible.

Please make sure that the example code you include in your report actually demonstrates the issue you are reporting. This includes verifying that the provided example code exhibits the issue when being run in dotnetfiddle or in a standalone console app (so as to make sure that everyone else can reproduce the problem with the example code given).

Also make sure that the issue occurs when using the most recent version of Newtonsoft.Json. Make sure the Newtonsoft.Json library you are using is not some 3rd-party modification but the original Newtonsoft.Json build as provided by the author of the library. (This is relevant especially for (old-ish) Unity projects, which sometimes rely on modified versions of Newtonsoft.Json. If this would be the case for your project, then the issue has to be reported not here but reported to the maintainers of that 3rd-party modification of Newtonsoft.Json instead.)

If the problem still occurs with the most recent version of Newtonsoft.Json and with the code in your report, then provide details about build setting and the environment in which you are running the code, such as but not limited to:


Debugging the serialized string it had the value "Foo" instead of the expected:

JsonConvert.SerializeObject(new Foo()) producing the string "Foo" as a result of serializing a Foo instance should not be possible, given the code in your report. I suspect either the code in your report is not equivalent to your real code exhibiting the issue, or you have somewhere somehow configured the serializer to use some custom JsonConverter or custom ContractResolver that causes this issue. (Configuring the serializer to use custom JsonConverters or ContractResolvers can be done in several ways. To name a few examples, it could be done with the help of attribute annotations, or through the JsonConvert.DefaultSettings delegate property as far as the JsonConvert.* methods are concerned.)

BuslikDrev commented 10 months ago

Может readonly не позволяет?