¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

NewtonSoft Problem


 

I'm trying to convert some JSON into a series of C# classes.? My problem is that one of the classes is named Event and this is causing a conflict with SimplWindows because that name is already used.? I know that NewtonSoft allows you to use JsonProperty to perform a renaming of an element of a class to avoid a similar conflict but I can't find anything that will solve this problem for a class.

ERROR? (LGSPLS1000)?? 'Event' already defined

??? public class Root
??? {
??????? public int id { get; set; }
??????? public string type { get; set; }
??????? public Event @event { get; set; }

??? }

??? public class Event
??? {
??????? public string event_type { get; set; }
??????? public DateTime time_fired { get; set; }
??????? public string origin { get; set; }
??? }

Thanks in advance for the help

Jay


 

If you are deserializing to a typed object your class name does not have to match anything within the json.? Can you make it something other than "Event" and then make sure the property name matches with your json string.? Something like the following


? public class Root
???? {
???????? public int id { get; set; }
???????? public string type { get; set; }
???????? public EventItem event { get; set; }

???? }

???? public class EventItem
???? {
???????? public string event_type { get; set; }
???????? public DateTime time_fired { get; set; }
???????? public string origin { get; set; }
???? }



On Thu, May 15, 2025, 7:22?PM jbasen via <jay.m.basen=[email protected]> wrote:
I'm trying to convert some JSON into a series of C# classes.? My problem
is that one of the classes is named Event and this is causing a conflict
with SimplWindows because that name is already used.? I know that
NewtonSoft allows you to use JsonProperty to perform a renaming of an
element of a class to avoid a similar conflict but I can't find anything
that will solve this problem for a class.

ERROR? (LGSPLS1000)?? 'Event' already defined

???? public class Root
???? {
???????? public int id { get; set; }
???????? public string type { get; set; }
???????? public Event @event { get; set; }

???? }

???? public class Event
???? {
???????? public string event_type { get; set; }
???????? public DateTime time_fired { get; set; }
???????? public string origin { get; set; }
???? }

Thanks in advance for the help

Jay







 

¿ªÔÆÌåÓý

Thanks so much Jordan.? Because NewtonSoft is matching up names I thought I had to use something similar to the JsonProperty directive.? However, you are absolutely correct.? This works perfectly.

Thanks again

Jay

On 5/15/2025 5:38 PM, Jordan Elasky via groups.io wrote:

If you are deserializing to a typed object your class name does not have to match anything within the json.? Can you make it something other than "Event" and then make sure the property name matches with your json string.? Something like the following


? public class Root
???? {
???????? public int id { get; set; }
???????? public string type { get; set; }
???????? public EventItem event { get; set; }

???? }

???? public class EventItem
???? {
???????? public string event_type { get; set; }
???????? public DateTime time_fired { get; set; }
???????? public string origin { get; set; }
???? }



On Thu, May 15, 2025, 7:22?PM jbasen via <jay.m.basen=[email protected]> wrote:
I'm trying to convert some JSON into a series of C# classes.? My problem
is that one of the classes is named Event and this is causing a conflict
with SimplWindows because that name is already used.? I know that
NewtonSoft allows you to use JsonProperty to perform a renaming of an
element of a class to avoid a similar conflict but I can't find anything
that will solve this problem for a class.

ERROR? (LGSPLS1000)?? 'Event' already defined

???? public class Root
???? {
???????? public int id { get; set; }
???????? public string type { get; set; }
???????? public Event @event { get; set; }

???? }

???? public class Event
???? {
???????? public string event_type { get; set; }
???????? public DateTime time_fired { get; set; }
???????? public string origin { get; set; }
???? }

Thanks in advance for the help

Jay







 

Or use the decorator JsonProperty("Event") and JsonConvert will use that name from the Json to assign the class property (whatever it's called)
Or make the class internal and then S+ won't even see it.
?
All the best,
Oliver


 

¿ªÔÆÌåÓý

Thanks Oliver.? The first thing I tried was using a JsonProperty statement but I couldn't get it to work.? Fortunately, just changing the class name did.

Thanks

Jay

On 5/16/2025 2:06 AM, Oliver Hall via groups.io wrote:

Or use the decorator JsonProperty("Event") and JsonConvert will use that name from the Json to assign the class property (whatever it's called)
Or make the class internal and then S+ won't even see it.
?
All the best,
Oliver