TypeSerializer example

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?

Original Post on Discord

by user 395602247196737546

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)

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

by user 395602247196737546

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

by user 395602247196737546

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

sure thing

by user 395602247196737546

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
}

by user 395602247196737546

Then just declare ExtremeFoo :string

:man_facepalming:

by user 395602247196737546

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.

by user 395602247196737546

Had to manually clean it from register_types.ts

by user 395602247196737546

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.

by user 395602247196737546

It really has to be
extremeFoo: string

by user 395602247196737546

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 :smile:

by user 395602247196737546

Where is that warning coming from? Note that you can declare it as a propery in typescript as well with just a setter (set ExtremeFoo(...). Also there are callbacks when you implement ISerializable on your class (see the methods available)

Where is that warning coming from?
does that help?

by user 395602247196737546

Ah ok thx.

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

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

by user 395602247196737546

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

by user 395602247196737546

thanks, @marcel :cactus: !

by user 395602247196737546