#TypeSerializer example
1 messages · Page 1 of 1 (latest)
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.
I love finding niche cases for you guys! (Sorry!)
No problem. Can you share a little example? Just to make sure we talk about the same thing 🙂
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
}
Then just declare ExtremeFoo :string
🤦♂️
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.
It really has to be
extremeFoo: string
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 😄
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?
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 🙂
still: TypeSerializer probably needs a (better) example if it is a public API
thanks, @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
Completely understand. Consider removing the brief mention of it here and adding a small example for ISerializable (and/or Newtonsoft's Json*Attributes or even NeedleConverter) 🙂