Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,16 @@ public DecimalFormatter(bool serializeAsString)

public void Serialize(ref JsonWriter writer, decimal value, IJsonFormatterResolver formatterResolver)
{
// always include decimal point and at least one decimal place
var s = value.ToString("0.0###########################", CultureInfo.InvariantCulture);
if (serializeAsString)
{
writer.WriteString(value.ToString(CultureInfo.InvariantCulture));
writer.WriteString(s);
}
else
{
// write as number format.
writer.WriteRaw(StringEncoding.UTF8.GetBytes(value.ToString(CultureInfo.InvariantCulture)));
writer.WriteRaw(StringEncoding.UTF8.GetBytes(s));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ enum DtoaMode
// PRECISION
}

[Flags]
enum Flags
{
NO_FLAGS = 0,
Expand Down Expand Up @@ -214,7 +215,7 @@ enum Flags
//const int max_leading_padding_zeroes_in_precision_mode_;
//const int max_trailing_padding_zeroes_in_precision_mode_;

static readonly Flags flags_ = Flags.UNIQUE_ZERO | Flags.EMIT_POSITIVE_EXPONENT_SIGN;
static readonly Flags flags_ = Flags.UNIQUE_ZERO | Flags.EMIT_POSITIVE_EXPONENT_SIGN | Flags.EMIT_TRAILING_DECIMAL_POINT | Flags.EMIT_TRAILING_ZERO_AFTER_POINT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So happy these flags exist and problem is solved at the root!

static readonly char exponent_character_ = 'E';
static readonly int decimal_in_shortest_low_ = -4; // C# ToString("G")
static readonly int decimal_in_shortest_high_ = 15;// C# ToString("G")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public static float ToSingle(byte[] buffer, int offset, out int readCount)
// port
internal static partial class StringToDoubleConverter
{
[Flags]
enum Flags
{
NO_FLAGS = 0,
Expand Down
24 changes: 10 additions & 14 deletions src/Elasticsearch.Net/Utf8Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ public static T Deserialize<T>(Stream stream, IJsonFormatterResolver resolver)
var ms = stream as MemoryStream;
if (ms != null)
{
ArraySegment<byte> buf2;
if (ms.TryGetBuffer(out buf2))
if (ms.TryGetBuffer(out var buf2))
{
// when token is number, can not use from pool(can not find end line).
var token = new JsonReader(buf2.Array, buf2.Offset).GetCurrentJsonToken();
Expand Down Expand Up @@ -367,19 +366,16 @@ public static async System.Threading.Tasks.Task<T> DeserializeAsync<T>(Stream st

#endif

public static string PrettyPrint(byte[] json)
{
return PrettyPrint(json, 0);
}
public static string PrettyPrint(byte[] json) => PrettyPrint(json, 0);

public static string PrettyPrint(byte[] json, int offset)
public static string PrettyPrint(byte[] json, int offset)
{
var reader = new JsonReader(json, offset);
var buffer = MemoryPool.Rent();
try
{
var writer = new JsonWriter(buffer);
WritePrittyPrint(ref reader, ref writer, 0);
WritePrettyPrint(ref reader, ref writer, 0);
return writer.ToString();
}
finally
Expand All @@ -396,7 +392,7 @@ public static string PrettyPrint(string json)
try
{
var writer = new JsonWriter(buffer);
WritePrittyPrint(ref reader, ref writer, 0);
WritePrettyPrint(ref reader, ref writer, 0);
return writer.ToString();
}
finally
Expand All @@ -418,7 +414,7 @@ public static byte[] PrettyPrintByteArray(byte[] json, int offset)
try
{
var writer = new JsonWriter(buffer);
WritePrittyPrint(ref reader, ref writer, 0);
WritePrettyPrint(ref reader, ref writer, 0);
return writer.ToUtf8ByteArray();
}
finally
Expand All @@ -434,7 +430,7 @@ public static byte[] PrettyPrintByteArray(string json)
try
{
var writer = new JsonWriter(buffer);
WritePrittyPrint(ref reader, ref writer, 0);
WritePrettyPrint(ref reader, ref writer, 0);
return writer.ToUtf8ByteArray();
}
finally
Expand All @@ -446,7 +442,7 @@ public static byte[] PrettyPrintByteArray(string json)
static readonly byte[][] indent = Enumerable.Range(0, 100).Select(x => Encoding.UTF8.GetBytes(new string(' ', x * 2))).ToArray();
static readonly byte[] newLine = Encoding.UTF8.GetBytes(Environment.NewLine);

static void WritePrittyPrint(ref JsonReader reader, ref JsonWriter writer, int depth)
static void WritePrettyPrint(ref JsonReader reader, ref JsonWriter writer, int depth)
{
var token = reader.GetCurrentJsonToken();
switch (token)
Expand All @@ -466,7 +462,7 @@ static void WritePrittyPrint(ref JsonReader reader, ref JsonWriter writer, int d
writer.WriteRaw(indent[depth + 1]);
writer.WritePropertyName(reader.ReadPropertyName());
writer.WriteRaw((byte)' ');
WritePrittyPrint(ref reader, ref writer, depth + 1);
WritePrettyPrint(ref reader, ref writer, depth + 1);
}
writer.WriteRaw(newLine);
writer.WriteRaw(indent[depth]);
Expand All @@ -486,7 +482,7 @@ static void WritePrittyPrint(ref JsonReader reader, ref JsonWriter writer, int d
writer.WriteRaw(newLine);
}
writer.WriteRaw(indent[depth + 1]);
WritePrittyPrint(ref reader, ref writer, depth + 1);
WritePrettyPrint(ref reader, ref writer, depth + 1);
}
writer.WriteRaw(newLine);
writer.WriteRaw(indent[depth]);
Expand Down
1 change: 0 additions & 1 deletion src/Nest/Search/Suggesters/TermSuggester/TermSuggester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public interface ITermSuggester : ISuggester
StringDistance? StringDistance { get; set; }

[DataMember(Name = "suggest_mode")]

SuggestMode? SuggestMode { get; set; }

[IgnoreDataMember]
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Tests.Core/Serialization/SerializationTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private JsonSerializerSettings ExpectedJsonSerializerSettings(bool preserveNullI
ContractResolver = new DefaultContractResolver { NamingStrategy = new DefaultNamingStrategy() },
NullValueHandling = preserveNullInExpected ? NullValueHandling.Include : NullValueHandling.Ignore,
//copied here because anonymyzing geocoordinates is too tedious
Converters = new List<JsonConverter> { new TestGeoCoordinateJsonConverter(), new Utf8JsonDecimalConverter() }
Converters = new List<JsonConverter> { new TestGeoCoordinateJsonConverter() }
};
}
}
34 changes: 0 additions & 34 deletions src/Tests/Tests.Core/Serialization/Utf8JsonDecimalConverter.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public MatrixStatsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage)
fields = new[] { "numberOfCommits", "numberOfContributors" },
missing = new
{
numberOfCommits = 0,
numberOfContributors = 1
numberOfCommits = 0.0,
numberOfContributors = 1.0
},
mode = "median"
}
Expand Down
60 changes: 60 additions & 0 deletions src/Tests/Tests/CodeStandards/Serialization/FractionalNumbers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Elastic.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;

namespace Tests.CodeStandards.Serialization
{
public class FractionalNumbers
{
private readonly IElasticsearchSerializer _serializer;

public FractionalNumbers()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool, new InMemoryConnection());
var client = new ElasticClient(settings);
_serializer = client.RequestResponseSerializer;
}

[U]
public void SerializeDouble()
{
var poco = new
{
Whole = 1d,
Fractional = 1.1d
};

var serialized = _serializer.SerializeToString(poco);
serialized.Should().Be("{\"whole\":1.0,\"fractional\":1.1}");
}

[U]
public void SerializeFloat()
{
var poco = new
{
Whole = 1f,
Fractional = 1.1f
};

var serialized = _serializer.SerializeToString(poco);
serialized.Should().Be("{\"whole\":1.0,\"fractional\":1.1}");
}

[U]
public void SerializeDecimal()
{
var poco = new
{
Whole = 1m,
Fractional = 1.1m
};

var serialized = _serializer.SerializeToString(poco);
serialized.Should().Be("{\"whole\":1.0,\"fractional\":1.1}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public BoolQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(
new { match_all = new { } }
},
minimum_should_match = 1,
boost = 2,
boost = 2.0,
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
{
numberOfCommits = new
{
origin = 1,
origin = 1.0,
scale = 0.1,
decay = 0.5
}
Expand All @@ -98,8 +98,8 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
{
origin = new
{
lat = 70,
lon = -70
lat = 70.0,
lon = -70.0
},
scale = "1mi"
},
Expand All @@ -118,7 +118,7 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
},
new { random_score = new { seed = 1337, field = "_seq_no" } },
new { random_score = new { seed = "randomstring", field = "_seq_no" } },
new { weight = 1 },
new { weight = 1.0 },
new
{
script_score = new
Expand All @@ -130,8 +130,8 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
}
}
},
max_boost = 20,
min_score = 1,
max_boost = 20.0,
min_score = 1.0,
query = new
{
match_all = new { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public GeoBoundingBoxQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : b
{
top_left = new
{
lat = 34,
lon = -34
lat = 34.0,
lon = -34.0
},
bottom_right = new
{
lat = -34,
lon = 34
lat = -34.0,
lon = 34.0
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public GeoDistanceQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base
validation_method = "ignore_malformed",
location = new
{
lat = 34,
lon = -34
lat = 34.0,
lon = -34.0
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public GeoPolygonQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(
{
points = new[]
{
new { lat = 45, lon = -45 },
new { lat = -34, lon = 34 }
new { lat = 45.0, lon = -45.0 },
new { lat = -34.0, lon = 34.0 }
}
}
}
Expand Down