Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deserialize data structures which type is not known ahead of time? #126

Open
Arugin opened this issue Sep 26, 2024 · 1 comment
Open

Comments

@Arugin
Copy link

Arugin commented Sep 26, 2024

I have the following data structure:

archetypes:
- types:
  - Type.Position
  - Type.Intensity
  - Type.Guid  
  components:
  - - value:
        x: 3
        y: 2
        z: 12
    - value:
        x: -3
        y: 73
        z: 1
    - value:
        x: 1
        y: 1
        z: 1
    - value:
        x: 5
        y: 5
        z: 3    
  - - value: 0.5
    - value: 0.0015
    - value: 0.001
    - value: 0.1  
  - - value: 80f91c84-504b-444f-b328-730ada871d22
    - value: 0c84c341-257f-4c90-9b02-eb224c5be9f4
    - value: c3410c84-257f-4c90-9b02-eb224c5be9f4
    - value: 0c84c341-257f-4c90-9b02-eb224c5be9f4
  otherData:
  ...
- types:
    ...
  components:
    ...
  otherData:
    ...

Types array can contain any type implementing a specific interface, that can't be known ahead of time. It can also cantain any number of types. Each array in the components contains data corresponding to that type. The sizes of components arrays will always match.

I need to deserialize this data using custom deserializer and get a set of arrays with that data.

When using Newtosoft.Json I can do something like this:

Array[] arrays = new Array[types.Length];
for(int i = 0; i < currTypes.Length; i++) {
 var currType = types[i].ToObject<Type>(); 

 arrays[i] = (Array)components[sourceIndex].ToObject(currType.type.MakeArrayType());
 if (array[i].Length == 0) {
	 arrays[i] = Array.CreateInstance(currType.type, count);
 }
}

And then I can pass this data further and work with that. Context: it is scene serialization for game engine, where scene structure and set of used components depends on user and can't be known ahead of time.

It looks like there is no way to deserialize something using VYaml when data types are not known ahead of time, or am I missing something? Do you plan to add overloads of context.Deserialize methods, that will get Type as a second parameter?

@IXLLEGACYIXL
Copy link

you would need to write a custom resolver which maps dynamically interfaces and abstract types onto the real type that is in it
and generate the code to register teh allowed mappings of the types

like i did on my fork
https://github.com/NexStandard/NexYamlSerializer/blob/main/NexYamlSerializer/NexYamlSerializerRegistry.cs
https://github.com/NexStandard/NexYamlSerializer/blob/main/NexYamlSerializer/NewYaml/YamlReader.cs#L46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants