Facepunch / sbox-issues

176 stars 12 forks source link

Serialization and Deserialization for System.Type #5762

Closed yuberee closed 2 months ago

yuberee commented 3 months ago

Describe the bug

My component has a property of List, which is useful because of the widget it comes with, when saving it doesn't serialize and deserialize as it's not supported.

System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported.
   at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter`1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
   at System.Text.Json.Serialization.Converters.ListOfTConverter`2.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)

To Reproduce

  1. Create a [Property] public List
  2. Try to save

Expected behavior

Properly serialize and deserialize

Media/Files

No response

Additional context

It's useful!

aylaylay commented 3 months ago

Technically the type control widget was made for use in actiongraph but works on anything. I have this ready to go in a safe way using TypeLibrary but I want to check what @garrynewman thinks first incase we really would rather avoid Type properties.

garrynewman commented 3 months ago

As long as we're fetching them using TypeLibrary, no real harm as far as I can see. It's probably a sign that you're doing something dumb, but I don't see why we would block it.

yuberee commented 3 months ago

Technically the type control widget was made for use in actiongraph but works on anything. I have this ready to go in a safe way using TypeLibrary but I want to check what @garrynewman thinks first incase we really would rather avoid Type properties.

Yea that widget was too good not to use, I wanted to use it only for selecting Component Types but I'm not sure how to do that exactly as having the property be of Component requires a reference to a component not a type.

My use case is for a custom trigger so I can have the trigger detect and pass any gameobject with any of those components, so I can have a Player trigger, an ItemComponent trigger, or an NPC trigger.

aylaylay commented 3 months ago

Done

aylaylay commented 3 months ago

Actiongraph is not allowing us to serialize types. We either need to fix actiongraph or remove the type control widget. @Metapyziks

Metapyziks commented 3 months ago

Let's just use the Type converter included in ActionGraph.