Skip to content

Commit

Permalink
Merge pull request #112 from avinet/string-quotes
Browse files Browse the repository at this point in the history
Force double quotes on integers and null in string
  • Loading branch information
xoofx authored Jul 4, 2022
2 parents 21487e2 + da2c0e8 commit a8fcaa5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
63 changes: 50 additions & 13 deletions src/SharpYaml.Tests/Serialization/SerializationTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public class MyCustomClassWithSpecialMembers
public MyCustomClassWithSpecialMembers()
{
StringListByContent = new List<string>();
StringMapbyContent = new Dictionary<string, object>();
ObjectMapbyContent = new Dictionary<string, object>();
ListByContent = new List<string>();
}

Expand Down Expand Up @@ -528,14 +528,14 @@ public MyCustomClassWithSpecialMembers()
/// Idem as for <see cref="StringList"/> but for dictionary.
/// </summary>
/// <value>The string map.</value>
public IDictionary<string, object> StringMap { get; set; }
public IDictionary<string, object> ObjectMap { get; set; }


/// <summary>
/// Idem as for <see cref="StringListByContent"/> but for dictionary.
/// </summary>
/// <value>The content of the string mapby.</value>
public Dictionary<string, object> StringMapbyContent { get; private set; }
public Dictionary<string, object> ObjectMapbyContent { get; private set; }

/// <summary>
/// For this property, the deserializer is using the actual
Expand Down Expand Up @@ -567,18 +567,18 @@ public void TestMyCustomClassWithSpecialMembers()
- a
- b
Name: Yes
StringList:
- 1
- 2
StringListByContent:
- 3
- 4
StringMap:
ObjectMap:
c: yes
d: 3
StringMapbyContent:
ObjectMapbyContent:
e: 4
f: no
StringList:
- ""1""
- ""2""
StringListByContent:
- ""3""
- ""4""
Value: 0
".Trim();

Expand Down Expand Up @@ -1616,13 +1616,50 @@ public void TestNumberInString()
{
var test = new
{
t = "1e3"
f1 = "1.2",
f2 = "1e3",
f3 = "-0.0",
f4 = ".inf",
i1 = "1234",
i2 = "-2",
};

var serializer = new Serializer();
var str = serializer.Serialize(test).Trim();
System.Console.WriteLine(str);
Assert.AreEqual("f1: \"1.2\"\r\nf2: \"1e3\"\r\nf3: \"-0.0\"\r\nf4: \".inf\"\r\ni1: \"1234\"\r\ni2: \"-2\"", str);
}

[Test]
public void TestBooleanInString()
{
var test = new
{
b1 = true,
b2 = false,
s1 = "true",
s2 = "false",
};

var serializer = new Serializer();
var str = serializer.Serialize(test).Trim();
System.Console.WriteLine(str);
Assert.AreEqual("b1: true\r\nb2: false\r\ns1: \"true\"\r\ns2: \"false\"", str);
}

[Test]
public void TestNullInString()
{
var test = new
{
n = (string)null,
s = "null",
};

var serializer = new Serializer();
var str = serializer.Serialize(test).Trim();
System.Console.WriteLine(str);
Assert.AreEqual(@"t: ""1e3""", str);
Assert.AreEqual("n: null\r\ns: \"null\"", str);
}

private void SerialRoundTrip(SerializerSettings settings, string text, Type serializedType = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ public void WriteYaml(ref ObjectContext objectContext)
case TypeCode.Char:
scalar.Style = ScalarStyle.Any;

// Force double quote for float and bool in string
if (value is string str && double.TryParse(str, out _))
// Force double quote for integer, float, bool and null in string
if (value is string str)
{
if (objectContext.SerializerContext.Schema.TryParse(new Scalar(str), true, out var defaultTag, out var parsedValue) && (parsedValue is double || parsedValue is bool))
if (objectContext.SerializerContext.Schema.TryParse(new Scalar(str), true, out var defaultTag, out var parsedValue)
&& (parsedValue is double || parsedValue is bool || parsedValue is int || parsedValue == null))
{
scalar.Style = ScalarStyle.DoubleQuoted;
}
Expand Down

0 comments on commit a8fcaa5

Please sign in to comment.