Skip to content

Commit

Permalink
Added DotNetSettigns.Clone() to allow cloning settings before changin…
Browse files Browse the repository at this point in the history
…g them
  • Loading branch information
loudenvier committed Nov 25, 2023
1 parent e11c50b commit ed58e75
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NetDot/DotNotationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public static class DotNotationExtensions {

public static string AsQueryString(this object o, DotNotationSettings? settings = null) {
settings ??= new();
settings ??= DotNotationSettings.Default.Clone();
settings.UrlEncode = true;
settings.EntrySeparator = "&";
return DotNotation.Serialize(o, settings: settings);
Expand Down
23 changes: 22 additions & 1 deletion NetDot/DotNotationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Specialized;
using System.Globalization;

namespace NetDot
Expand Down Expand Up @@ -62,5 +61,27 @@ public class DotNotationSettings {
/// Controls serialization formats/rules for numbers and dates. Defaults to <see cref="CultureInfo.InvariantCulture"/>.
/// </summary>
public CultureInfo Culture { get;set; } = CultureInfo.InvariantCulture;

/// <summary>
/// Clones this instance copying all properties
/// </summary>
/// <returns>a new <see cref="DotNotationSettings"/> with the source properties copied</returns>
public DotNotationSettings Clone() => new() {
DotConnector = DotConnector,
KeyValueSeparator = KeyValueSeparator,
QuoteChar = QuoteChar,
SpacingAfterKey = SpacingAfterKey,
SpacingBeforeValue = SpacingBeforeValue,
EntrySeparator = EntrySeparator,
QuoteStrings = QuoteStrings,
QuoteValues = QuoteValues,
TrimValues = TrimValues,
TrimChars = TrimChars,
SurroundingTexts = SurroundingTexts,
UrlEncode = UrlEncode,
DateFormatString = DateFormatString,
Culture = Culture,
};

}
}
2 changes: 1 addition & 1 deletion NetDot/NetDot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<VersionPrefix>1.3.0</VersionPrefix>
<VersionPrefix>1.3.1</VersionPrefix>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>NetDot</Title>
<Authors>felipemachado</Authors>
Expand Down
8 changes: 8 additions & 0 deletions NetDotTests/DotNotationExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ public void AsQueryStringWorks() {
}.AsQueryString();
Assert.Equal("page=10&pageSize=50&user.id=1&token=my%20token%2F123", queryString);
}
[Fact]
public void AsQueryStringDoesNotOverrideDefaultSettings() {
var obj = new { id = "abc/123" };
var qs = obj.AsQueryString();
Assert.Equal("id=abc%2F123", qs);
var text = DotNotation.Serialize(obj);
Assert.Equal("id=abc/123", text);
}
}
}

0 comments on commit ed58e75

Please sign in to comment.