Passing an array as a parameter to an event
hello, i have these arrays, which needs to get passed towards the clientside
problem is that when they get passed, they are received as
object[] args
the username remains a string type, but the arrays become newtonsoft.json.linq.jarray
, and i can't find a version of this plugin which is compatible with .net core 3.1
If you can help me either find a version for the newtonsoft which works, or help me get my arrays into my clientside handler, thank you!19 Replies
Use System.Text.Json
Stop using Newtonsoft
Newtonsoft is purely for legacy reasons
yeah but my framework uses it idk
my workaround was to put all names into one string and then split it at the compiler
but idk if its the best solution
you should still be able to introduce system text json and leave newtonsoft in its place
how are you deserializing this
I'm 99.999% sure MSFT hired James Newton-King (the guy who wrote Newtonsoft.Json), so - as @Buddy said, you should move to using
System.Text.Json
.
So, server-side would be JsonSerializer.Serialize(obj)
and client-side would be JsonSerializer.Deserialize<ObjType>(objString)
.FYI Newtonsoft is the correct way in Unity. Considering this appears to be game code, it could very well be Unity.
STJ is not supported unless you step into hoops to get it working. Nothing a beginner should do
FYI, Newtonsoft correctly deserialized your array, but since it is unaware of the type is used
JArray
which is an abstract of their library so that you can still work with arrays
There's also JObject and JToken and the idea is that these are used in the event there is no known or suitable type for your object
Now, idk what your code is. Maybe you can share that. But if you have this then there is a method in JArray
that allows you to map it into an object using reflection, assuming it is the correct object. You can probably omit this altogether if you properly deserialize it to begin with, assuming you have control over thatThat probably means/infers the client side doesn't have the full object definition, then, yeah? So, like object <ThisObject> has property <myProperty> server-side but client-side, that doesn't exist?
Lots of guesswork if client code is not shared. If a library handles this, not much can be done. If they made it themselves, they should fix their code.
Leading off with Unity-->Newtonsoft.Json would've helped, too. 😅
It's not even about not having the full object definition BTW, because then it would still deserialize into the object type rather than
JArray
. This is intentional deserialization into JArray
due to not specifying a target type.
To be specific, I believe it actually deserializes into JObject
question, i read online that system.text.json is integrated in .net core 3.1, but when i try to use it in my client, i get this
.NET Core 3.0
So yes, it should exist
well it doesnt
weird
I suggest you just use Newtonsoft instead of refactoring everything now. One thing at a time
i ve found my api has some methods to handle json like this
RAGE.Util.Json.Deserialize<>()
and to use this i had to reference a newtonsoft.json dll
but i had those in my runtime folder which copes with my apiNice, looks like that one deserializes to a target type
so i'm good now thank you
yes it's cool
Great 👍
another question
like i have this project on .net core 3.1
and i would like to use .net 8 what should i do when switching frameworks
should i just use updated references for .net 8 and i should be good?
because this server of mine, it's meant to be used in .net core 3.1 but i see a lot of people that put it on .net 8
but when i tried to i had really weird errors
i mean with mysql.data, and mysqlconnection, neither worked even though they were versions compatible with .net 8
.NET 8 is the latest LTS
Definitely try to update because your version is very old
.NET barely has breaking changes so it should not be too hard
Just remember to also update all packages to the latest, or to a version that supports it
Third party libraries might not be as easy though, but it depends on the library