❔ 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
JakenVeina
JakenVeina9mo ago
depends how it's structured
Will Pittenger
Will Pittenger9mo ago
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.
JakenVeina
JakenVeina9mo ago
public class Parent
{
public required string Name { get; init; }

public required IReadOnlyList<Child> Children { get; init; }
}

public class Child
{
public required string Name { get; init; }

public required Parent Parent { get; init; }
}
public class Parent
{
public required string Name { get; init; }

public required IReadOnlyList<Child> Children { get; init; }
}

public class Child
{
public required string Name { get; init; }

public required Parent Parent { get; init; }
}
this ought to work, if you enable reference handling wouldn't work if you were going to, say, use constructor initialization, instead of init properties
Will Pittenger
Will Pittenger9mo ago
OK.
Will Pittenger
Will Pittenger9mo ago
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.
JakenVeina
JakenVeina9mo ago
even if what value is only a string? not sure what you mean
Will Pittenger
Will Pittenger9mo ago
The first sample here would produce the same results as the second:
[
"ss",
{
"wrapped": "value",
"some other value": "value2"
}
]
[
"ss",
{
"wrapped": "value",
"some other value": "value2"
}
]
[
{
"wrapped": "ss",
"some other value": "default"
},
{
"wrapped": "value",
"some other value": "value2"
}
]
[
{
"wrapped": "ss",
"some other value": "default"
},
{
"wrapped": "value",
"some other value": "value2"
}
]
JakenVeina
JakenVeina9mo ago
in what regard? produce how? what would produce that?
Will Pittenger
Will Pittenger9mo ago
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.
Will Pittenger
Will Pittenger9mo ago
The string is a simplified version of the long form.
JakenVeina
JakenVeina9mo ago
okay, like what is the actual goal here are you trying to serialize JSON or deserialize?
Will Pittenger
Will Pittenger9mo ago
Deserialize.
JakenVeina
JakenVeina9mo ago
what's the JSON you want to deserialize?
Will Pittenger
Will Pittenger9mo ago
???? I showed you that above.
JakenVeina
JakenVeina9mo ago
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?
Will Pittenger
Will Pittenger9mo ago
Well, both could.
JakenVeina
JakenVeina9mo ago
k
Will Pittenger
Will Pittenger9mo ago
They're both valid.
JakenVeina
JakenVeina9mo ago
and you're using Newtonsoft? or System.Text.Json?
Will Pittenger
Will Pittenger9mo ago
Yes. Well, I see System.Text.Json, but I had to install a package to get it.
JakenVeina
JakenVeina9mo ago
uhh, same as you did for Newtonsoft anyway, so the most straightforward implementation is...
public class MyJsonRecord
{
[JsonProperty("wrapped")]
public required string Wrapped { get; init; }

[JsonProperty("some other value")]
public required string SomeOtherValue { get; init; }
}
public class MyJsonRecord
{
[JsonProperty("wrapped")]
public required string Wrapped { get; init; }

[JsonProperty("some other value")]
public required string SomeOtherValue { get; init; }
}
var json1 = @"[""ss"",{""wrapped"": ""value"",""some other value"": ""value2""}]";
var json2 = @"[{""wrapped"": ""ss"",""some other value"": ""default""},{""wrapped"": ""value"",""some other value"": ""value2""}]";

var result1 = JsonConvert.DeserializeObject<List<object>>(json1);
var result2 = JsonConvert.DeserializeObject<List<object>>(json2);
var json1 = @"[""ss"",{""wrapped"": ""value"",""some other value"": ""value2""}]";
var json2 = @"[{""wrapped"": ""ss"",""some other value"": ""default""},{""wrapped"": ""value"",""some other value"": ""value2""}]";

var result1 = JsonConvert.DeserializeObject<List<object>>(json1);
var result2 = JsonConvert.DeserializeObject<List<object>>(json2);
Will Pittenger
Will Pittenger9mo ago
What about the owner field?
JakenVeina
JakenVeina9mo ago
what is "the owner field"?
Will Pittenger
Will Pittenger9mo ago
I told you the class in the list needs a reference to the object that owns it.
JakenVeina
JakenVeina9mo ago
which is what? you said that, but where is that represented in the JSON?
Will Pittenger
Will Pittenger9mo ago
It's more like:
C#
class Y
{
// cooresponsds to the array in the file
System.Collections.Generic.List<X> list = new();
}
class X
{
public readonly Y owner;
public required string Wrapped {get; init;}

public required string SomeOtherValue{get; init;}
}
C#
class Y
{
// cooresponsds to the array in the file
System.Collections.Generic.List<X> list = new();
}
class X
{
public readonly Y owner;
public required string Wrapped {get; init;}

public required string SomeOtherValue{get; init;}
}
JakenVeina
JakenVeina9mo ago
okay
Will Pittenger
Will Pittenger9mo ago
It isn't. I'm needing a way to pass it in.
JakenVeina
JakenVeina9mo ago
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 layer
Will Pittenger
Will Pittenger9mo ago
Unfortunately, it needs to be set when the instance is created.
JakenVeina
JakenVeina9mo ago
why?
Will Pittenger
Will Pittenger9mo ago
I want it public readonly.
JakenVeina
JakenVeina9mo ago
ergo, it doesn't need to be
Will Pittenger
Will Pittenger9mo ago
It isn't valid without it.
JakenVeina
JakenVeina9mo ago
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
public class Y
{
public static Y ParseFromJson(string json)
{
var y = new Y()
{
Items = JsonConverter.Deserialize<List<object>>(sourceJson);
};

foreach(var item in items)
if (item is X x)
x.Owner = y;

return y;
}

private Y() {}

public required IReadOnlyList<object> Items { get; init; }
}

public class X
{
internal X() {}

public Y Owner { get; internal set; }
= null!;

public required string Wrapped {get; init;}

public required string SomeOtherValue{get; init;}
}
public class Y
{
public static Y ParseFromJson(string json)
{
var y = new Y()
{
Items = JsonConverter.Deserialize<List<object>>(sourceJson);
};

foreach(var item in items)
if (item is X x)
x.Owner = y;

return y;
}

private Y() {}

public required IReadOnlyList<object> Items { get; init; }
}

public class X
{
internal X() {}

public Y Owner { get; internal set; }
= null!;

public required string Wrapped {get; init;}

public required string SomeOtherValue{get; init;}
}
TheRanger
TheRanger9mo ago
well, would public Y Owner { get; private set; } be fine with you?
Will Pittenger
Will Pittenger9mo ago
Somewhat. What about the init accessor for the owner?
TheRanger
TheRanger9mo ago
well i found a weird way,
public class Y
{
public System.Collections.Generic.List<X> list = new();
public static Y LatestInstance {get; private set;}

public static Y Deserialize(string json)
{
return new Y()
{
list = JsonConvert.DeserializeObject<System.Collections.Generic.List<X>>(json),
};
}

public Y()
{
LatestInstance = this;
}
}

public class X
{
public required Y Owner {get; init;}
[JsonProperty("wrapped")]
public required string Wrapped { get; init; }

[JsonProperty("some other value")]
public required string SomeOtherValue { get; init; }

public static explicit operator X(string str) => Parse(str);

public static X Parse(string str)
{
return new X
{
Wrapped = str,
Owner = Y.LatestInstance,
SomeOtherValue = "default",
};
}

public X()
{
Owner = Y.LatestInstance;
}
}
public class Y
{
public System.Collections.Generic.List<X> list = new();
public static Y LatestInstance {get; private set;}

public static Y Deserialize(string json)
{
return new Y()
{
list = JsonConvert.DeserializeObject<System.Collections.Generic.List<X>>(json),
};
}

public Y()
{
LatestInstance = this;
}
}

public class X
{
public required Y Owner {get; init;}
[JsonProperty("wrapped")]
public required string Wrapped { get; init; }

[JsonProperty("some other value")]
public required string SomeOtherValue { get; init; }

public static explicit operator X(string str) => Parse(str);

public static X Parse(string str)
{
return new X
{
Wrapped = str,
Owner = Y.LatestInstance,
SomeOtherValue = "default",
};
}

public X()
{
Owner = Y.LatestInstance;
}
}
its probably dumb, but probably only the way with the required attribute
Will Pittenger
Will Pittenger9mo ago
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.
TheRanger
TheRanger9mo ago
ur json doesnt have a Owner field so it will be null as default you can put [JsonIgnore] in case
Will Pittenger
Will Pittenger9mo ago
Unfortunately, all instances need to have an valid owner--even if the full version of the class was used.
TheRanger
TheRanger9mo ago
what do u mean? isn't the owner valid in your json?
Will Pittenger
Will Pittenger9mo ago
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.
TheRanger
TheRanger9mo ago
ah so u have 2 forms of that in the same json?
Will Pittenger
Will Pittenger9mo ago
Yes.
TheRanger
TheRanger9mo ago
in the same list/array?
Will Pittenger
Will Pittenger9mo ago
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.
TheRanger
TheRanger9mo ago
i tested this
[
"ss",
{
"wrapped": "value",
"some other value": "value2"
},
{
"t":
{
"wrapped": "ss",
"some other value": "default"
}
},
{
"t":
{
"wrapped": "value",
"some other value": "value2"
}
}
]
[
"ss",
{
"wrapped": "value",
"some other value": "value2"
},
{
"t":
{
"wrapped": "ss",
"some other value": "default"
}
},
{
"t":
{
"wrapped": "value",
"some other value": "value2"
}
}
]
all have the same owner but how do you think the last 2 elements in the array would be converted with this code
Will Pittenger
Will Pittenger9mo ago
No.
[
"ss",
{
"wrapped": "value",
"some other value": "value2"
}
]
[
"ss",
{
"wrapped": "value",
"some other value": "value2"
}
]
Those are both elements in the array. That's equivalent to:
[
{
"wrapped": "ss",
"some other value": "default"
},
{
"wrapped": "value",
"some other value": "value2"
}
]
[
{
"wrapped": "ss",
"some other value": "default"
},
{
"wrapped": "value",
"some other value": "value2"
}
]
TheRanger
TheRanger9mo ago
so u dont have something like this in ur json anymore?
"t":
{
"wrapped": "value",
"some other value": "value2"
}
"t":
{
"wrapped": "value",
"some other value": "value2"
}
Will Pittenger
Will Pittenger9mo ago
I think that was a mistake in the other class.
TheRanger
TheRanger9mo ago
then what original short form were u talking about
Will Pittenger
Will Pittenger9mo ago
this
TheRanger
TheRanger9mo ago
Yes, that is what i used both have same owner
Will Pittenger
Will Pittenger9mo ago
So you didn't do anything special?
TheRanger
TheRanger9mo ago
did you use this code?
Will Pittenger
Will Pittenger9mo ago
Didn't try it yet. Been busy elsewhere while waiting on the other issues. Got started on those.
TheRanger
TheRanger9mo ago
🤷🏻‍♂️
Will Pittenger
Will Pittenger9mo ago
Sorry. I also still on the fence if your solution is acceptable. It does have a bit of code smell.
TheRanger
TheRanger9mo ago
well, i tested it, so it works fine for me
Will Pittenger
Will Pittenger9mo ago
OK.
Accord
Accord9mo ago
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.
Want results from more Discord servers?
Add your server
More Posts
❔ Is it correct to use a single instance of Azure Front Door for different types of resources?I have an AFD profile in a resource group which was intended to be used to server Web APIs and it ha✅ DB context injection not workinghi, i have app like this - ```cs DotNetEnv.Env.Load("../build/"); var builder = WebApplication.Crea❔ Trying to find a way so it shows the sum of all the numbers entered and the also the average.```cs Modifier votre code Q2 afin de calculer/afficher la somme et ❔ What would be the best way to dynamicly cut in a string>So for example I have this: "C:\Users\USERNAME\Documents\Paradox Interactive\Hearts of Iron IV\mod\H❔ Reloading MainWindow after some changesHello guys, im currently working on a project to improve my C#/programming skills. I have following❔ need some help with a microsoft visual studio 2022 project im doinghttps://dotnetfiddle.net/ got my fiddle here, just nothing aint working and im lost rnDoesnt have the permision to save the fileSo I have been working on a Window Application and start working on a way to save the settings in a ✅ Project A sees one class in Project B, but not both (C#)I have two C# projects. In one, I declare two classes. One just derives from the other and adds so✅ Can the NewtonSoft JSON package conditionally create classes when parse?I have a situation where a field in a class will sometimes have a string, but other times have a cla❔ SkiaSharp fast and efficient effectsIs it possible to do effects such as the twirl effect in SkiaSharp using the GPU? I've been searchin