Giter Club home page Giter Club logo

Comments (18)

gabriel-samfira avatar gabriel-samfira commented on August 24, 2024 2

Nevertheless, if someone wants @gabriel-samfira 's behaviour, it is just a matter of registering a implementation of IEventEmitter

That is actually how we implemented it in the end in powershell-yaml. At least for now. Tags should be the long term solution. Just need to find the time to do it.

@aaubry not sure if I ever mentioned this before, but thanks for all your work!

from yamldotnet.

aaubry avatar aaubry commented on August 24, 2024 1

The YAML specification defines different schemas that specify how scalars should be treated by default. It is funny that you mention this because I just started working on schemas yesterday :)
I expect to have a working implementation soon.

from yamldotnet.

Untit1ed avatar Untit1ed commented on August 24, 2024 1

Any progress on this?

from yamldotnet.

aaubry avatar aaubry commented on August 24, 2024 1

@Roadrunner67 I'll do a release today. Regarding the fix, I don't think it should be included because I don't think that it is correct. The YAML language uses tags to represent types. Whether a scalar is plain or quoted is a presentation detail. The spec states that two scalars are equal if they have the same tag and content, regardless of their style.

Nevertheless, if someone wants @gabriel-samfira 's behaviour, it is just a matter of registering a implementation of IEventEmitter. There's no need to change the existing code.

from yamldotnet.

ecooke-macu avatar ecooke-macu commented on August 24, 2024 1

This works, quoting string when they are numbers

using YamlDotNet.Serialization;

var test = new { Item = "123" };

var serializer = new SerializerBuilder()
    .WithQuotingNecessaryStrings()
    .Build();
var actual = serializer.Serialize(test);

Console.WriteLine(actual);

Results in:

Item: "123"

from yamldotnet.

gabriel-samfira avatar gabriel-samfira commented on August 24, 2024

I am also curious about progress on this. The problem is that not all strings should be serialized as Plain scalars. This adds ambiguity. For example:

$builder = [YamlDotNet.Serialization.SerializerBuilder]::new()
$serializer = $builder.Build()

$obj = @{"IAmAString" = "200"; "IAmAnInt" = 200}
$serializer.Serialize($obj)

which outputs:

IAmAString: 200
IAmAnInt: 200

When you deserialize this, you loose the type. This is why the Python yaml module, adds single quotes around values that might cause ambiguity.

I think this could be fixed here:

https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs#L83

By testing if the value can be cast to any type other than string, and if it can be, just set:

eventInfo.IsPlainImplicit = false;
eventInfo.IsQuotedImplicit = true;

from yamldotnet.

gabriel-samfira avatar gabriel-samfira commented on August 24, 2024

So, using something like this:

diff --git a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs
index dae028e..d78aff5 100644
--- a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs
+++ b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs
@@ -81,6 +81,21 @@ namespace YamlDotNet.Serialization.EventEmitters
                     break;
 
                 case TypeCode.String:
+                    decimal dec;
+                    int number;
+                    float flt;
+                    long lng;
+                    double dbl;
+                    var val = eventInfo.Source.Value.ToString();
+                    if (Int32.TryParse(val, out number) ||
+                        Int64.TryParse(val, out lng) ||
+                        Double.TryParse(val, out dbl) ||
+                        Decimal.TryParse(val, out dec) ||
+                        float.TryParse(val, out flt))
+                    {
+                        eventInfo.Style = ScalarStyle.DoubleQuoted;
+                    }
+                    goto case TypeCode.Char;
                 case TypeCode.Char:
                     eventInfo.Tag = "tag:yaml.org,2002:str";
                     eventInfo.RenderedValue = eventInfo.Source.Value.ToString();
@@ -146,4 +161,4 @@ namespace YamlDotNet.Serialization.EventEmitters
             }
         }
     }
-}
\ No newline at end of file
+}

Results in:

$builder = [YamlDotNet.Serialization.SerializerBuilder]::new()
$serializer = $builder.Build()
$obj = @{
    "IAmAString" = "200"
    "IAmAnInt" = 200
    "11" = "KeyAsIntAndShouldBeDisambiguated"
    "1sa1" = "JustAStringStartingWithANumber"
}
$serializer.Serialize($obj)                                                                                                           
IAmAnInt: 200
IAmAString: "200"
"11": KeyAsIntAndShouldBeDisambiguated
1sa1: JustAStringStartingWithANumber

Which uses quotes around values that are at risk to loose their type when serialized and deserialized by other parsers.

from yamldotnet.

ridhoq avatar ridhoq commented on August 24, 2024

Just checking in on the progress of this. It looks like @gabriel-samfira has fixed this downstream in the following PR: https://github.com/cloudbase/powershell-yaml/pull/41/files#diff-e4c14acd05e286e165f6b75e7a30d165. Is it possible to bring this change (or something similar) upstream in this library?

from yamldotnet.

Roadrunner67 avatar Roadrunner67 commented on August 24, 2024

@aaubry I noticed you accepted my PR #477, are you up to a version release soon?
What do you need to include this fix from @gabriel-samfira ?

from yamldotnet.

aaubry avatar aaubry commented on August 24, 2024

Thanks @gabriel-samfira, you're welcome!

from yamldotnet.

Ronan-Lenor avatar Ronan-Lenor commented on August 24, 2024

is it normal that it don't work in 8.1.2?
do i have to wait the version 8.1.3?

for the moment i still use something like that in my *.yaml files:
key: !!int 1000

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on August 24, 2024

I know this is pretty old, but it will be resolved in the next release so I'm closing this.

from yamldotnet.

Mrgaton avatar Mrgaton commented on August 24, 2024

Please fix it

from yamldotnet.

Mrgaton avatar Mrgaton commented on August 24, 2024

Nevermine i just writed myself a fixer of the output because I know that this is gonna take forever

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on August 24, 2024

It’s actually released. On the serializer builder call with quoting necessary strings.

from yamldotnet.

Mrgaton avatar Mrgaton commented on August 24, 2024

It’s actually released. On the serializer builder call with quoting necessary strings.

It highlights everything except the strings

from yamldotnet.

Mrgaton avatar Mrgaton commented on August 24, 2024

yes but i dont want to quote numbers or bools I want to quote strings

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on August 24, 2024

Currently you can use the yamlmember attribute to specify you want double or single quotes.

Currently there is no feature for defaulting to using quotes. Wouldn’t be hard to do though. If that is what you are looking for, open a new issue.

from yamldotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.