Skip to content

Commit f4ee102

Browse files
authored
CSHARP-3147: Update default JSON output mode (#1468)
* CSHARP-3147: Update default JSON output mode
1 parent cae4a1d commit f4ee102

File tree

73 files changed

+857
-799
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+857
-799
lines changed

src/MongoDB.Bson/IO/JsonWriterSettings.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class JsonWriterSettings : BsonWriterSettings
3232
private bool _indent = false;
3333
private string _indentChars = " ";
3434
private string _newLineChars = "\r\n";
35-
private JsonOutputMode _outputMode = JsonOutputMode.Shell;
35+
private JsonOutputMode _outputMode = JsonOutputMode.RelaxedExtendedJson;
3636
private Version _shellVersion;
3737

3838
// constructors

tests/MongoDB.Bson.Tests/BsonExtensionMethodsNominalTypeTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using FluentAssertions;
17+
using MongoDB.Bson.IO;
1718
using MongoDB.Bson.Serialization;
1819
using System;
1920
using Xunit;
@@ -121,7 +122,7 @@ public void ToJson_generic_should_default_args_nominal_type_to_TNominalType()
121122
{
122123
var c = new C { X = 1 };
123124

124-
var json = c.ToJson();
125+
var json = c.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
125126

126127
json.Should().Be("{ \"X\" : 1 }");
127128
}

tests/MongoDB.Bson.Tests/BsonExtensionMethodsTests.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Linq;
1818
using FluentAssertions;
1919
using MongoDB.Bson;
20+
using MongoDB.Bson.IO;
2021
using MongoDB.Bson.Serialization;
2122
using MongoDB.TestHelpers.XunitExtensions;
2223
using Xunit;
@@ -121,7 +122,7 @@ public void TestToBsonDocumentIdFirst()
121122
public void TestToJsonEmptyDocument()
122123
{
123124
var document = new BsonDocument();
124-
var json = document.ToJson();
125+
var json = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
125126
var expected = "{ }";
126127
Assert.Equal(expected, json);
127128
}
@@ -130,7 +131,7 @@ public void TestToJsonEmptyDocument()
130131
public void TestToJson()
131132
{
132133
var c = new C { N = 1, Id = ObjectId.Empty };
133-
var json = c.ToJson();
134+
var json = c.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
134135
var expected = "{ 'N' : 1, '_id' : ObjectId('000000000000000000000000') }".Replace("'", "\"");
135136
Assert.Equal(expected, json);
136137
}
@@ -139,7 +140,7 @@ public void TestToJson()
139140
public void TestToJsonIdFirst()
140141
{
141142
var c = new C { N = 1, Id = ObjectId.Empty };
142-
var json = c.ToJson(args: new BsonSerializationArgs { SerializeIdFirst = true });
143+
var json = c.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }, args: new BsonSerializationArgs { SerializeIdFirst = true });
143144
var expected = "{ '_id' : ObjectId('000000000000000000000000'), 'N' : 1 }".Replace("'", "\"");
144145
Assert.Equal(expected, json);
145146
}

tests/MongoDB.Bson.Tests/IO/BsonDocumentWriterTests.cs

+87-87
Large diffs are not rendered by default.

tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs

+43-43
Large diffs are not rendered by default.

tests/MongoDB.Bson.Tests/IO/JsonWriterTests.cs

+32-23
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void JsonWriter_should_support_writing_multiple_documents(
4949
string documentSeparator)
5050
{
5151
var document = new BsonDocument("x", 1);
52-
var json = document.ToJson();
52+
var json = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
5353
var expectedResult = Enumerable.Repeat(json, numberOfDocuments).Aggregate("", (a, j) => a + j + documentSeparator);
5454

5555
using (var stringWriter = new StringWriter())
@@ -69,11 +69,20 @@ public void JsonWriter_should_support_writing_multiple_documents(
6969
}
7070
}
7171

72+
[Fact]
73+
public void JsonWriter_should_have_relaxed_extended_json_as_default()
74+
{
75+
using var stringWriter = new StringWriter();
76+
using var jsonWriter = new JsonWriter(stringWriter);
77+
78+
jsonWriter.Settings.OutputMode.Should().Be(JsonOutputMode.RelaxedExtendedJson);
79+
}
80+
7281
[Fact]
7382
public void TestEmptyDocument()
7483
{
7584
BsonDocument document = new BsonDocument();
76-
string json = document.ToJson();
85+
string json = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
7786
string expected = "{ }";
7887
Assert.Equal(expected, json);
7988
}
@@ -82,7 +91,7 @@ public void TestEmptyDocument()
8291
public void TestSingleString()
8392
{
8493
BsonDocument document = new BsonDocument() { { "abc", "xyz" } };
85-
string json = document.ToJson();
94+
string json = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
8695
string expected = "{ \"abc\" : \"xyz\" }";
8796
Assert.Equal(expected, json);
8897
}
@@ -139,7 +148,7 @@ public void TestDecimal128Shell()
139148
};
140149
foreach (var test in tests)
141150
{
142-
var json = new BsonDecimal128(test.Value).ToJson();
151+
var json = new BsonDecimal128(test.Value).ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
143152
Assert.Equal(test.Expected, json);
144153
Assert.Equal(test.Value, BsonSerializer.Deserialize<Decimal128>(json));
145154
}
@@ -207,7 +216,7 @@ public void TestDouble()
207216
};
208217
foreach (var test in tests)
209218
{
210-
var json = test.Value.ToJson();
219+
var json = test.Value.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
211220
Assert.Equal(test.Expected, json);
212221
Assert.Equal(test.Value, BsonSerializer.Deserialize<double>(json));
213222
}
@@ -219,7 +228,7 @@ public void TestDoubleRoundTripOn64BitProcess()
219228
RequireProcess.Check().Bits(64);
220229
var value = 0.6822871999174; // see: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#RFormatString
221230

222-
var json = value.ToJson();
231+
var json = value.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
223232
var rehydrated = BsonSerializer.Deserialize<double>(json);
224233

225234
rehydrated.Should().Be(value);
@@ -240,7 +249,7 @@ public void TestInt64Shell()
240249
};
241250
foreach (var test in tests)
242251
{
243-
var json = test.Value.ToJson();
252+
var json = test.Value.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
244253
Assert.Equal(test.Expected, json);
245254
Assert.Equal(test.Value, BsonSerializer.Deserialize<long>(json));
246255
}
@@ -277,7 +286,7 @@ public void TestEmbeddedDocument()
277286
{
278287
{ "doc", new BsonDocument { { "a", 1 }, { "b", 2 } } }
279288
};
280-
string json = document.ToJson();
289+
string json = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
281290
string expected = "{ \"doc\" : { \"a\" : 1, \"b\" : 2 } }";
282291
Assert.Equal(expected, json);
283292
}
@@ -302,7 +311,7 @@ public void TestArray()
302311
{
303312
{ "array", new BsonArray { 1, 2, 3 } }
304313
};
305-
string json = document.ToJson();
314+
string json = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
306315
string expected = "{ \"array\" : [1, 2, 3] }";
307316
Assert.Equal(expected, json);
308317
}
@@ -321,7 +330,7 @@ public void TestBinaryShell()
321330
};
322331
foreach (var test in tests)
323332
{
324-
var json = test.Value.ToJson(new JsonWriterSettings());
333+
var json = test.Value.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
325334
Assert.Equal(test.Expected, json);
326335
Assert.Equal(test.Value, BsonSerializer.Deserialize<BsonBinaryData>(json));
327336
}
@@ -368,7 +377,7 @@ public void TestDateTimeShell()
368377
};
369378
foreach (var test in tests)
370379
{
371-
var json = test.Value.ToJson();
380+
var json = test.Value.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
372381
Assert.Equal(test.Expected, json);
373382
Assert.Equal(test.Value, BsonSerializer.Deserialize<BsonDateTime>(json));
374383
}
@@ -410,7 +419,7 @@ public void TestJavaScript()
410419
{ "f", new BsonJavaScript("function f() { return 1; }") }
411420
};
412421
string expected = "{ \"f\" : { \"$code\" : \"function f() { return 1; }\" } }";
413-
string actual = document.ToJson();
422+
string actual = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
414423
Assert.Equal(expected, actual);
415424
}
416425

@@ -422,7 +431,7 @@ public void TestJavaScriptWithScope()
422431
{ "f", new BsonJavaScriptWithScope("function f() { return n; }", new BsonDocument("n", 1)) }
423432
};
424433
string expected = "{ \"f\" : { \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } } }";
425-
string actual = document.ToJson();
434+
string actual = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
426435
Assert.Equal(expected, actual);
427436
}
428437

@@ -433,7 +442,7 @@ public void TestUuidStandardWhenGuidRepresentationIsUnspecified()
433442
var guidBytes = GuidConverter.ToBytes(guid, GuidRepresentation.Standard);
434443

435444
var binary = new BsonBinaryData(guidBytes, BsonBinarySubType.UuidStandard); // GuidRepresentation is Unspecified
436-
var result = binary.ToJson(writerSettings: new JsonWriterSettings());
445+
var result = binary.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
437446
result.Should().Be("UUID(\"00112233-4455-6677-8899-aabbccddeeff\")");
438447
}
439448

@@ -445,7 +454,7 @@ public void TestMaxKey()
445454
{ "maxkey", BsonMaxKey.Value }
446455
};
447456
string expected = "{ \"maxkey\" : MaxKey }";
448-
string actual = document.ToJson();
457+
string actual = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
449458
Assert.Equal(expected, actual);
450459
}
451460

@@ -457,7 +466,7 @@ public void TestMinKey()
457466
{ "minkey", BsonMinKey.Value }
458467
};
459468
string expected = "{ \"minkey\" : MinKey }";
460-
string actual = document.ToJson();
469+
string actual = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
461470
Assert.Equal(expected, actual);
462471
}
463472

@@ -469,15 +478,15 @@ public void TestNull()
469478
{ "null", BsonNull.Value }
470479
};
471480
string expected = "{ \"null\" : null }";
472-
string actual = document.ToJson();
481+
string actual = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
473482
Assert.Equal(expected, actual);
474483
}
475484

476485
[Fact]
477486
public void TestObjectIdShell()
478487
{
479488
var objectId = new ObjectId("4d0ce088e447ad08b4721a37");
480-
var json = objectId.ToJson();
489+
var json = objectId.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
481490
var expected = "ObjectId(\"4d0ce088e447ad08b4721a37\")";
482491
Assert.Equal(expected, json);
483492
Assert.Equal(objectId, BsonSerializer.Deserialize<ObjectId>(json));
@@ -513,7 +522,7 @@ public void TestRegularExpressionShell()
513522
};
514523
foreach (var test in tests)
515524
{
516-
var json = test.Value.ToJson();
525+
var json = test.Value.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
517526
Assert.Equal(test.Expected, json);
518527
Assert.Equal(test.Value, BsonSerializer.Deserialize<BsonRegularExpression>(json));
519528
}
@@ -573,7 +582,7 @@ public void TestString()
573582
};
574583
foreach (var test in tests)
575584
{
576-
var json = test.Value.ToJson();
585+
var json = test.Value.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
577586
Assert.Equal(test.Expected, json);
578587
Assert.Equal(test.Value, BsonSerializer.Deserialize<string>(json));
579588
}
@@ -587,7 +596,7 @@ public void TestSymbol()
587596
{ "symbol", BsonSymbolTable.Lookup("name") }
588597
};
589598
string expected = "{ \"symbol\" : { \"$symbol\" : \"name\" } }";
590-
string actual = document.ToJson();
599+
string actual = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
591600
Assert.Equal(expected, actual);
592601
}
593602

@@ -599,7 +608,7 @@ public void TestTimestamp()
599608
{ "timestamp", new BsonTimestamp(1, 2) }
600609
};
601610
string expected = "{ \"timestamp\" : Timestamp(1, 2) }";
602-
string actual = document.ToJson();
611+
string actual = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
603612
Assert.Equal(expected, actual);
604613
}
605614

@@ -611,7 +620,7 @@ public void TestUndefined()
611620
{ "undefined", BsonUndefined.Value }
612621
};
613622
string expected = "{ \"undefined\" : undefined }";
614-
string actual = document.ToJson();
623+
string actual = document.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
615624
Assert.Equal(expected, actual);
616625
}
617626

tests/MongoDB.Bson.Tests/Jira/CSharp102Tests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Linq;
1818
using MongoDB.Bson;
19+
using MongoDB.Bson.IO;
1920
using MongoDB.Bson.Serialization;
2021
using Xunit;
2122

@@ -44,7 +45,7 @@ public void TestClassMap()
4445
Assert.Equal("Id", classMap.IdMemberMap.MemberName);
4546

4647
var test = new Test { Normal = "normal" };
47-
var json = test.ToJson();
48+
var json = test.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
4849
var expected = "{ '_id' : ObjectId('000000000000000000000000'), 'Normal' : 'normal' }".Replace("'", "\"");
4950
Assert.Equal(expected, json);
5051

tests/MongoDB.Bson.Tests/Jira/CSharp120Tests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System.Linq;
1717
using MongoDB.Bson;
18+
using MongoDB.Bson.IO;
1819
using MongoDB.Bson.Serialization;
1920
using Xunit;
2021

@@ -37,7 +38,7 @@ public class CSharp120Tests
3738
public void TestGuidStringRepresentation()
3839
{
3940
var c = new C { X = 1, Y = 2 };
40-
var json = c.ToJson();
41+
var json = c.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
4142
var expected = "{ 'X' : 1, 'Y' : 2 }".Replace("'", "\"");
4243
Assert.Equal(expected, json);
4344

tests/MongoDB.Bson.Tests/Jira/CSharp121Tests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Linq;
1818
using MongoDB.Bson;
19+
using MongoDB.Bson.IO;
1920
using MongoDB.Bson.Serialization;
2021
using MongoDB.Bson.Serialization.Attributes;
2122
using Xunit;
@@ -34,7 +35,7 @@ public class CSharp121Tests
3435
public void TestGuidStringRepresentation()
3536
{
3637
var c = new C { PhotoId = Guid.Empty };
37-
var json = c.ToJson();
38+
var json = c.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
3839
var expected = "{ 'PhotoId' : #S }";
3940
expected = expected.Replace("#S", "'00000000-0000-0000-0000-000000000000'");
4041
expected = expected.Replace("'", "\"");

tests/MongoDB.Bson.Tests/Jira/CSharp122Tests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System.Linq;
1717
using MongoDB.Bson;
18+
using MongoDB.Bson.IO;
1819
using MongoDB.Bson.Serialization;
1920
using MongoDB.Bson.Serialization.Attributes;
2021
using Xunit;
@@ -44,7 +45,7 @@ public void TestTwoPropertiesWithSameName()
4445
{
4546
var c = new C { N = 4, A = 2, V = 3 };
4647
((B)c).N = 1;
47-
var json = c.ToJson();
48+
var json = c.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
4849
var expected = "{ 'B.N' : 1, 'A' : 2, 'V' : 3, 'C.N' : 4 }".Replace("'", "\"");
4950
Assert.Equal(expected, json);
5051

tests/MongoDB.Bson.Tests/Jira/CSharp133Tests.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Globalization;
1818
using System.Linq;
1919
using MongoDB.Bson;
20+
using MongoDB.Bson.IO;
2021
using MongoDB.Bson.Serialization;
2122
using MongoDB.Bson.Serialization.Attributes;
2223
using Xunit;
@@ -42,7 +43,7 @@ public class CSharp133Tests
4243
public void TestNull()
4344
{
4445
var c = new C { S = null, I = null, D = null };
45-
var json = c.ToJson();
46+
var json = c.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
4647
var expected = "{ 'S' : null }".Replace("'", "\"");
4748
Assert.Equal(expected, json);
4849

@@ -61,7 +62,7 @@ public void TestNotNull()
6162
{
6263
var date = new DateTime(1980, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
6364
var c = new C { S = "xyz", I = "xyz", I2 = date, D = "xyz" };
64-
var json = c.ToJson();
65+
var json = c.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell });
6566
var expected = ("{ 'S' : 'xyz', 'I' : 'xyz', 'D' : 'xyz', 'I2' : ISODate('" + date.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture) + "') }").Replace("'", "\"");
6667
Assert.Equal(expected, json);
6768

0 commit comments

Comments
 (0)