❔ NewtownSoft.JSON and classes that need references to its owner
I have two classes. Class A owns instances of Class B. But each instance of Class B needs a reference back to the instance of Class A that created it. Can
System.Text.Json.JsonSerializer
do that?62 Replies
depends how it's structured
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
this ought to work, if you enable reference handling
wouldn't work if you were going to, say, use constructor initialization, instead of
init
propertiesOK.
So would that work even if the value is only a string? See the final example in https://discord.com/channels/143867839282020352/1157002592119947374.
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
even if what value is only a string?
not sure what you mean
The first sample here would produce the same results as the second:
in what regard?
produce how?
what would produce that?
Well, I was going to use an implicit typecast operator as was suggested in the thread I linked earlier, https://discord.com/channels/143867839282020352/1157002592119947374. But that operator can't work as it doesn't have a reference to the owner.
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
The string is a simplified version of the long form.
okay, like
what is the actual goal here
are you trying to serialize JSON or deserialize?
Deserialize.
what's the JSON you want to deserialize?
???? I showed you that above.
not really
you posted two snippets and said that "they produce the same result"
which one represents JSON that you receive and need to deserialize?
both of them?
Well, both could.
k
They're both valid.
and you're using Newtonsoft?
or System.Text.Json?
Yes.
Well, I see
System.Text.Json
, but I had to install a package to get it.uhh, same as you did for Newtonsoft
anyway, so the most straightforward implementation is...
What about the owner field?
what is "the owner field"?
I told you the class in the list needs a reference to the object that owns it.
which is what?
you said that, but where is that represented in the JSON?
It's more like:
okay
It isn't. I'm needing a way to pass it in.
that would be the most effective thing to do, yes
technically, you could build a whole custom JSON parser that is smart enough to populate it on construction
but it's really not the job of the JSON parser, anyway
that
owner
field isn't part of the data, it's part of your business model
populate it in your business layerUnfortunately, it needs to be set when the instance is created.
why?
I want it
public readonly
.ergo, it doesn't need to be
It isn't valid without it.
for one, it shouldn't be a field
for two, initialzing it after construction doesn't make it not valid
so long as you don't publish the instance for consumption before initialization is complete
well, would
public Y Owner { get; private set; }
be fine with you?Somewhat. What about the
init
accessor for the owner?well i found a weird way,
its probably dumb, but probably only the way with the required attribute
BTW: Your
X
doesn't declare owner
.
Oh it does.
I was expecting a [JsonIgnore]
on it as the owner isn't in the JSON.ur json doesnt have a
Owner
field so it will be null as default
you can put [JsonIgnore] in caseUnfortunately, all instances need to have an valid owner--even if the full version of the class was used.
what do u mean? isn't the owner valid in your json?
Go back to the original short form mentioned in https://discord.com/channels/143867839282020352/1157109068423507988/1157150777404571708. The first value in the array would end up with an owner using your code. But not the second. Both must have owners. (In this case, the same owner.)
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
ah so u have 2 forms of that in the same json?
Yes.
in the same list/array?
Yes.
Only the full form will ever be written. But the short form might be on the website as a way of specifying predefined entries.
i tested this
all have the same owner
but how do you think the last 2 elements in the array would be converted with this code
No.
Those are both elements in the array.
That's equivalent to:
so u dont have something like this in ur json anymore?
I think that was a mistake in the other class.
then what original short form were u talking about
Yes, that is what i used
both have same owner
So you didn't do anything special?
did you use this code?
Didn't try it yet. Been busy elsewhere while waiting on the other issues.
Got started on those.
🤷🏻♂️
Sorry.
I also still on the fence if your solution is acceptable. It does have a bit of code smell.
well, i tested it, so it works fine for me
OK.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.