Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't deserialize object just serialized. #2911

Open
PaulStSmith opened this issue Oct 31, 2023 · 2 comments
Open

Can't deserialize object just serialized. #2911

PaulStSmith opened this issue Oct 31, 2023 · 2 comments

Comments

@PaulStSmith
Copy link

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)
@PaulStSmith PaulStSmith changed the title Can't deserialized object just serialized. Can't deserialize object just serialized. Oct 31, 2023
@elgonzo
Copy link

elgonzo commented Oct 31, 2023

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:

  • .NET version your program is targeting
  • Build settings that have an impact on the output the compiler produces (such as settings that govern code trimming or native AOT compiling)
  • the runtime environment the program is being executed in (including the version of the .NET/Mono/Xamarin/iOS/etc runtime, if different from the build target)

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants