hadashiA / VYaml

The extra fast, low memory footprint YAML library for C#, focued on .NET and Unity.
MIT License
295 stars 16 forks source link

[FeatureRequest] Support plymorphism with descriptor as well, not just by tag #73

Closed nicloay closed 2 months ago

nicloay commented 7 months ago

I have many configuration files, and then I generate Json chemas for them, it's more convenient when you have autocompletion in the editor, and it also allow to validate incoming files using packages like JsonSchema (paid) or NJsonSchema (free)

At the moment before deserialization, I replace descriptor like this content.Replace("- type: !", "- !");

Here is a small JSON schema from my project, it uses the constant field to describe different types ``` { "definitions": { "ModApplyDamage": { "type": "object", "properties": { "value": { "type": "integer" }, "type": { "const": "!damage" } }, "required": [ "value", "type" ] }, "ModCriticalHit": { "type": "object", "properties": { "chance": { "type": "number" }, "mult": { "type": "number" }, "type": { "const": "!crit" } }, "required": [ "chance", "mult", "type" ] }, "ModMaxDistance": { "type": "object", "properties": { "value": { "type": "number" }, "type": { "const": "!dist" } }, "required": [ "value", "type" ] }, "ModSpeed": { "type": "object", "properties": { "value": { "type": "number" }, "type": { "const": "!speed" } }, "required": [ "value", "type" ] }, "ProjectileConfig": { "type": [ "object", "null" ], "properties": { "prefabId": { "type": "string", "enum": [ "bolt", "roundHead" ] }, "modifiers": { "type": "array", "items": { "oneOf": [ { "$ref": "#/definitions/ModApplyDamage" }, { "$ref": "#/definitions/ModSpeed" }, { "$ref": "#/definitions/ModCriticalHit" }, { "$ref": "#/definitions/ModMaxDistance" } ] } } }, "required": [ "prefabId", "modifiers" ] } }, "type": "object", "properties": { "id": { "type": [ "string", "null" ] }, "icon": { "type": "string", "enum": [ "none", "renegade", "cyclone", "cascade", "firestone", "bullsEye" ] }, "prefabId": { "type": "string", "enum": [ "none", "doubleGun" ] }, "chargeTime": { "type": "integer" }, "coolDownTime": { "type": "integer" }, "projectileCount": { "type": "integer" }, "projectile": { "$ref": "#/definitions/ProjectileConfig" }, "radarMaxRadius": { "type": "number" } }, "required": [ "id", "icon", "prefabId", "chargeTime", "coolDownTime", "projectileCount", "projectile", "radarMaxRadius" ] } ```
and this is my yaml file ``` id: renegadeMK2 icon: renegade prefabId: doubleGun chargeTime: 3 coolDownTime: 2 projectileCount: 1 radarMaxRadius: 25 projectile: prefabId: bolt modifiers: - type: !dist value: 12 - type: !crit chance: 0 mult: 0 - type: !speed value: 0 - type: !damage value: 33 ```
hadashiA commented 2 months ago

In short, is it correct that you want to define the schema definition separately from the data, e.g. in a JSON Schema?

If so, there are currently no plans to support this in VYaml itself.

VYaml is a library that provides serialisation to C#, in which case it would be good to be able to use C# type definitions as schemas.

Also, I personally think that Json Schema itself is prehistoric. It is a schema definition using only what is accepted as a web standard. Alternatives to json schema include Google's cue and Apple's pkl.