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

Global toggle for quoting strings when serializing #791

Closed
jhm-ciberman opened this issue Mar 29, 2023 · 7 comments
Closed

Global toggle for quoting strings when serializing #791

jhm-ciberman opened this issue Mar 29, 2023 · 7 comments

Comments

@jhm-ciberman
Copy link

jhm-ciberman commented Mar 29, 2023

Is your feature request related to a problem? Please describe.
I am making a mod loader system for a game and I would prefer every string (and maybe enums) to be quoted in double quotes to avoid any confusion for moders (and for myself) when authoring yaml files about special characters.

A typical manifest.yaml file for my game would look like this:

!Package
id: "medieval_life"
name: "Medieval Life"
description: "Built-in content for the Medieval Life game."
author: "Ciberman"
version: "1.0.0"
dependencies: []
content:
  - !Image
    id: "textures.water"
    path: "res/water.png"
  - !Image
    id: "textures.cliff"
    path: "res/tilemap/cliff.png"
  - !Image
    id: "textures.particles.smoke"
    path: "res/particles/smoke.png"
  - !GltfAnimation
    id: "animations.idle"
    path: "res/avatar/Character.glb"
    animation_name: "Idle"
  - !GltfAnimation
    id: "animations.walk"
    path: "res/avatar/CharacterWalk.glb"
    animation_name: "Walk"
  - !Image
    id: "textures.luts.hair"
    path: "res/avatar/LutHair.png"
  - !Image
    id: "textures.luts.eyes"
    path: "res/avatar/LutEyes.png"
  - !Object
    id: "furniture.bed"
    name: "Bed"
    description: "A simple bed"
    size: [1, 1]
    collision_shape: !Rectangle
      min: [0, 0]
      max: [2, 1]
    texture: !Image
      path: "res/furniture/bed.png"
    model: !GltfPrefab
      path: "res/furniture/bed.glb"
      model_scale: [1, 1, 1]
  - !Image
    id: "textures.luts.skin"
    path: "res/avatar/LutSkin.png"
  - !Sprite
    id: "gui.button_primary"
    path: "res/gui/button_primary.png"
    size: [32, 32]
    frames: 4
    nine_slice_margin: [8, 2, 6, 8]
    
# More content definitions here...

Currently YamlDotNet serializes every string without quotes.

Describe the solution you'd like
I would want a simple togle or simple way to force every string to be quoted. I'm not sure about what the ideal API would be.

Describe alternatives you've considered
I know I can force quoting via [YamlMember] attribute, but I would want a global toggle for every serialized string, because It's very cumbersome and easy to forget to add a YamlMemberAttribute to every DTO object.

I also spent a lot of time trying to use the solution in this comment but I cannot get it to only quote string values. It quotes everything including property names, which looks ugly to me.

Additional context
I'm very happy with YamlDotNet. And Writting custom YamlTypeConverters is very fun. Thanks for your amazing work! ❤️

@EdwardCooke
Copy link
Collaborator

This has been a feature I e seen requested a couple of times. It wouldn’t be hard to implement. I may have time to do it tonight after I work through at least one other pull request. We also do accept community pull requests if I can’t get to it. I would do it as another method on the serializer builder, passing an option to the required class that does the serialization. It’s where it specifies the type of scalar based on a reflex.

@EdwardCooke
Copy link
Collaborator

I think this is the line that needs to be changed.

@jhm-ciberman
Copy link
Author

jhm-ciberman commented Mar 30, 2023

Great!
Question: Does it makes sense to let users customize the quoting behaviour? As far as I know, double quotes let the user use escape characters (\n, \t) whereas single quotes don't.

  1. Should the TypeAssigningEventEmmiter receive in it's constructor a bool doubleQuoteStrings and bool doubleQuoteEnums?
  2. Or it's better to have something like QuotingMode stringQuotingMode and QuotingMode enumQuotingMode?

EDIT: @EdwardCooke I cannot get to get the unit test working. Is something special that I should be doing? I'm using dotnet test

EDIT2: I was missing cloning the submodules. Sorry hehe. But now a lot of the tests fail. Maybe I am missing something more. :/

EDIT3: Opened this #792

@EdwardCooke
Copy link
Collaborator

EdwardCooke commented Mar 30, 2023 via email

@EdwardCooke
Copy link
Collaborator

Passing in the scalar style as a default scalar style would be the route I would go.

@EdwardCooke
Copy link
Collaborator

This PR is open and awaiting a comment to be resolved. It will allow specifying the default quote style

#827

@EdwardCooke
Copy link
Collaborator

That pr is approved. You can now set a default quoting policy. I’m closing this issue since it’s resolved now.

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

No branches or pull requests

2 participants