#TypeSerializer example

1 messages · Page 1 of 1 (latest)

worn hamlet
#

Hey guys, I'm currently playing around with serializing existing C# classes to reuse in needle engine. I stumbled upon the mention of TypeSerializer in the docs but the example in the class comment does not help a lot. Is there any other source of wisdom for this?

bitter yacht
#

Hi, what exactly are you trying to do that doesnt work already? Serializing/deserializing existing classes of whatever type should just work (if annotated with @serializable)

worn hamlet
#

In my C# class there's a derived property (non-serialized) and I want to transfer its value to TS.

#

I love finding niche cases for you guys! (Sorry!)

bitter yacht
#

No problem. Can you share a little example? Just to make sure we talk about the same thing 🙂

worn hamlet
#

sure thing

#
class MyClass : MonoBehaviour
{
  [SerializeField]
  private string _foo = "foo";

  public string ExtremeFoo => _foo + "!!!";
}
class MyClass extends Behaviour {
  @serializable()
  _extremeFoo : string; // i want ExtremeFoo's value to land here
}
bitter yacht
#

Then just declare ExtremeFoo :string

worn hamlet
#

🤦‍♂️

#

Hmm having a problem as I've declared MyClassTypeSerializer extends TypeSerialize before and now removed it:
Uncaught SyntaxError: The requested module '/src/scripts/MyClass.ts' does not provide an export named 'MyClassTypeSerializer' (at register_types.ts:19:53)

Unfortunately, even a clean install does not get rid of it.

#

Had to manually clean it from register_types.ts

#

Ok, back to the original issue. Now I get warnings like this:
vconsole.min.js:10 Please use lowercase for field: "ExtremeFoo" in MyClass foo!!! and even though it logs the value there for some reason (foo!!!) the value does not arrive at the property.

worn hamlet
#

Haha, and on top of that I also have a name clash. My real-life property is string Context in C# so I'd have to declare context: string in TS but that'd overwrite/hide(?) Behaviour.context...

Luckily you're using Newtonsoft.Json so I can use [JsonProperty("MyContext"] string Context and myContext: string 😄

bitter yacht
worn hamlet
#

Where is that warning coming from?
does that help?

bitter yacht
#

Ah ok thx.

Habe you tried assigning it with ISerializable yourself? There are callbacks per member

worn hamlet
#

nope not so far, but good to know about the interface. i guess something like this is what i wanted all along after all 🙂

#

still: TypeSerializer probably needs a (better) example if it is a public API

#

thanks, @bitter yacht !

bitter yacht
#

It is but also not really 😅 (i mean it is but quite low level/core and it shouldn't be necessary to be used)

#

Im glad it helps

worn hamlet