0

I have a class

public class Event{
[JsonProperty("event_type")] public string EventType { get; private set; }
}

when I deserialize it with

Event event = JsonConvert.DeserializeObject<Event>("{'event_type':'started'}"

Field EventType is feed with value "started". But I don't expect this cause it is a "get;private set" menber, which should not be deserialized but can be serialized as I expect.

How can I achieve this?

czh
  • 21
  • 3
  • There is the `JsonIgnoreAttribute` that you can use to mark your properties with. Is that what you are looking for? – Sweeper Aug 24 '19 at 02:52
  • 2
    What would be the purpose of serializing a field without intent to deserialize it? – Ted Brownlow Aug 24 '19 at 02:56
  • Take a look at [Serialize Property, but Do Not Deserialize Property in Json.Net](https://stackoverflow.com/q/31731320/3744182). – dbc Aug 24 '19 at 04:26

1 Answers1

0

You could use something like a JsonConverter (see here) That would let you override how the object was created so you could use a constructor or a static method to init the private set property. Although I have to say the use case for this is pretty rare. Perhaps elaborate on why you need that property to be a private set and we can provide more help.

Joshua G
  • 2,086
  • 3
  • 19
  • 22