Comments (18)
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.
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.
Any progress on this?
from yamldotnet.
@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.
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.
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:
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.
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.
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.
@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.
Thanks @gabriel-samfira, you're welcome!
from yamldotnet.
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.
I know this is pretty old, but it will be resolved in the next release so I'm closing this.
from yamldotnet.
Please fix it
from yamldotnet.
Nevermine i just writed myself a fixer of the output because I know that this is gonna take forever
from yamldotnet.
It’s actually released. On the serializer builder call with quoting necessary strings.
from yamldotnet.
It’s actually released. On the serializer builder call with quoting necessary strings.
It highlights everything except the strings
from yamldotnet.
yes but i dont want to quote numbers or bools I want to quote strings
from yamldotnet.
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)
- Overriding the name of a type dynamically HOT 2
- ObjectNodeDeserializer: Naming Convention Error on Deserialization HOT 2
- is it possible to style a string in a special way? HOT 3
- Set license expression on nuget package HOT 1
- Improve nuget package readme HOT 2
- Serialization / deserialization is not thread safe HOT 2
- Very large files fail to parse HOT 9
- Using ```YamlStream``` will cause "property not found" in ```IDeserializer.Deserialize()``` HOT 1
- Empty line in comments HOT 1
- Support for FSharp BCL List deserialization
- Aliases do not resolve correctly in Sequences HOT 1
- Better serialization of strings that look like integers (specifically, octals) HOT 2
- How to set custom Anchor names? HOT 6
- parse yaml file VB,net HOT 9
- Support keys longer than 1024 characters
- Deserialization into generic dictionary using WithAttemptingUnquotedStringTypeDeserialization deserializes hexadecimal value incorrectly.
- I am getting property not found error HOT 5
- How can I serialize to get a pretty json or flow yaml format string with newlines?
- SemanticErrorException on valid YAML
- Deserialization exception when running quick start example on Linux
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yamldotnet.