@ -52,7 +52,7 @@ namespace Google.Protobuf
[Test]
[Test]
public void DefaultValues_WhenOmitted ( )
public void DefaultValues_WhenOmitted ( )
{
{
var formatter = new JsonFormatter ( new JsonFormatter . Settings ( format DefaultValues : false ) ) ;
var formatter = JsonFormatter . Default ;
AssertJson ( "{ }" , formatter . Format ( new ForeignMessage ( ) ) ) ;
AssertJson ( "{ }" , formatter . Format ( new ForeignMessage ( ) ) ) ;
AssertJson ( "{ }" , formatter . Format ( new TestAllTypes ( ) ) ) ;
AssertJson ( "{ }" , formatter . Format ( new TestAllTypes ( ) ) ) ;
@ -62,7 +62,7 @@ namespace Google.Protobuf
[Test]
[Test]
public void DefaultValues_WhenIncluded ( )
public void DefaultValues_WhenIncluded ( )
{
{
var formatter = new JsonFormatter ( new JsonFormatter . Settings ( formatDefaultValues : true ) ) ;
var formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithFormatDefaultValues ( true ) ) ;
AssertJson ( "{ 'c': 0 }" , formatter . Format ( new ForeignMessage ( ) ) ) ;
AssertJson ( "{ 'c': 0 }" , formatter . Format ( new ForeignMessage ( ) ) ) ;
}
}
@ -78,6 +78,23 @@ namespace Google.Protobuf
AssertJson ( expectedText , actualText ) ;
AssertJson ( expectedText , actualText ) ;
}
}
[Test]
public void EnumAsInt ( )
{
var message = new TestAllTypes
{
SingleForeignEnum = ForeignEnum . ForeignBar ,
RepeatedForeignEnum = { ForeignEnum . ForeignBaz , ( ForeignEnum ) 1 0 0 , ForeignEnum . ForeignFoo }
} ;
var formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithFormatEnumsAsIntegers ( true ) ) ;
var actualText = formatter . Format ( message ) ;
var expectedText = "{ " +
"'singleForeignEnum': 5, " +
"'repeatedForeignEnum': [ 6, 100, 4 ]" +
" }" ;
AssertJson ( expectedText , actualText ) ;
}
[Test]
[Test]
public void AllSingleFields ( )
public void AllSingleFields ( )
{
{
@ -266,9 +283,9 @@ namespace Google.Protobuf
}
}
// We should get the same result both with and without "format default values".
// We should get the same result both with and without "format default values".
var formatter = new JsonFormatter ( new JsonFormatter . Settings ( false ) ) ;
var formatter = JsonFormatter . Default ;
AssertJson ( expectedJson , formatter . Format ( message ) ) ;
AssertJson ( expectedJson , formatter . Format ( message ) ) ;
formatter = new JsonFormatter ( new JsonFormatter . Setting s( true ) ) ;
formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithFormatDefaultValue s( true ) ) ;
AssertJson ( expectedJson , formatter . Format ( message ) ) ;
AssertJson ( expectedJson , formatter . Format ( message ) ) ;
}
}
@ -300,7 +317,7 @@ namespace Google.Protobuf
{
{
// The actual JSON here is very large because there are lots of fields. Just test a couple of them.
// The actual JSON here is very large because there are lots of fields. Just test a couple of them.
var message = new TestWellKnownTypes { Int32Field = 1 0 } ;
var message = new TestWellKnownTypes { Int32Field = 1 0 } ;
var formatter = new JsonFormatter ( new JsonFormatter . Setting s( true ) ) ;
var formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithFormatDefaultValue s( true ) ) ;
var actualJson = formatter . Format ( message ) ;
var actualJson = formatter . Format ( message ) ;
Assert . IsTrue ( actualJson . Contains ( "\"int64Field\": null" ) ) ;
Assert . IsTrue ( actualJson . Contains ( "\"int64Field\": null" ) ) ;
Assert . IsFalse ( actualJson . Contains ( "\"int32Field\": null" ) ) ;
Assert . IsFalse ( actualJson . Contains ( "\"int32Field\": null" ) ) ;
@ -309,7 +326,7 @@ namespace Google.Protobuf
[Test]
[Test]
public void OutputIsInNumericFieldOrder_NoDefaults ( )
public void OutputIsInNumericFieldOrder_NoDefaults ( )
{
{
var formatter = new JsonFormatter ( new JsonFormatter . Settings ( false ) ) ;
var formatter = JsonFormatter . Default ;
var message = new TestJsonFieldOrdering { PlainString = "p1" , PlainInt32 = 2 } ;
var message = new TestJsonFieldOrdering { PlainString = "p1" , PlainInt32 = 2 } ;
AssertJson ( "{ 'plainString': 'p1', 'plainInt32': 2 }" , formatter . Format ( message ) ) ;
AssertJson ( "{ 'plainString': 'p1', 'plainInt32': 2 }" , formatter . Format ( message ) ) ;
message = new TestJsonFieldOrdering { O1Int32 = 5 , O2String = "o2" , PlainInt32 = 1 0 , PlainString = "plain" } ;
message = new TestJsonFieldOrdering { O1Int32 = 5 , O2String = "o2" , PlainInt32 = 1 0 , PlainString = "plain" } ;
@ -321,7 +338,7 @@ namespace Google.Protobuf
[Test]
[Test]
public void OutputIsInNumericFieldOrder_WithDefaults ( )
public void OutputIsInNumericFieldOrder_WithDefaults ( )
{
{
var formatter = new JsonFormatter ( new JsonFormatter . Setting s( true ) ) ;
var formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithFormatDefaultValue s( true ) ) ;
var message = new TestJsonFieldOrdering ( ) ;
var message = new TestJsonFieldOrdering ( ) ;
AssertJson ( "{ 'plainString': '', 'plainInt32': 0 }" , formatter . Format ( message ) ) ;
AssertJson ( "{ 'plainString': '', 'plainInt32': 0 }" , formatter . Format ( message ) ) ;
message = new TestJsonFieldOrdering { O1Int32 = 5 , O2String = "o2" , PlainInt32 = 1 0 , PlainString = "plain" } ;
message = new TestJsonFieldOrdering { O1Int32 = 5 , O2String = "o2" , PlainInt32 = 1 0 , PlainString = "plain" } ;
@ -485,7 +502,7 @@ namespace Google.Protobuf
[Test]
[Test]
public void AnyWellKnownType ( )
public void AnyWellKnownType ( )
{
{
var formatter = new JsonFormatter ( new JsonFormatter . Settings ( false , TypeRegistry . FromMessages ( Timestamp . Descriptor ) ) ) ;
var formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithTypeRegistry ( TypeRegistry . FromMessages ( Timestamp . Descriptor ) ) ) ;
var timestamp = new DateTime ( 1 6 7 3 , 6 , 1 9 , 1 2 , 3 4 , 5 6 , DateTimeKind . Utc ) . ToTimestamp ( ) ;
var timestamp = new DateTime ( 1 6 7 3 , 6 , 1 9 , 1 2 , 3 4 , 5 6 , DateTimeKind . Utc ) . ToTimestamp ( ) ;
var any = Any . Pack ( timestamp ) ;
var any = Any . Pack ( timestamp ) ;
AssertJson ( "{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }" , formatter . Format ( any ) ) ;
AssertJson ( "{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }" , formatter . Format ( any ) ) ;
@ -494,7 +511,7 @@ namespace Google.Protobuf
[Test]
[Test]
public void AnyMessageType ( )
public void AnyMessageType ( )
{
{
var formatter = new JsonFormatter ( new JsonFormatter . Settings ( false , TypeRegistry . FromMessages ( TestAllTypes . Descriptor ) ) ) ;
var formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithTypeRegistry ( TypeRegistry . FromMessages ( TestAllTypes . Descriptor ) ) ) ;
var message = new TestAllTypes { SingleInt32 = 1 0 , SingleNestedMessage = new TestAllTypes . Types . NestedMessage { Bb = 2 0 } } ;
var message = new TestAllTypes { SingleInt32 = 1 0 , SingleNestedMessage = new TestAllTypes . Types . NestedMessage { Bb = 2 0 } } ;
var any = Any . Pack ( message ) ;
var any = Any . Pack ( message ) ;
AssertJson ( "{ '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }" , formatter . Format ( any ) ) ;
AssertJson ( "{ '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }" , formatter . Format ( any ) ) ;
@ -503,7 +520,7 @@ namespace Google.Protobuf
[Test]
[Test]
public void AnyMessageType_CustomPrefix ( )
public void AnyMessageType_CustomPrefix ( )
{
{
var formatter = new JsonFormatter ( new JsonFormatter . Settings ( false , TypeRegistry . FromMessages ( TestAllTypes . Descriptor ) ) ) ;
var formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithTypeRegistry ( TypeRegistry . FromMessages ( TestAllTypes . Descriptor ) ) ) ;
var message = new TestAllTypes { SingleInt32 = 1 0 } ;
var message = new TestAllTypes { SingleInt32 = 1 0 } ;
var any = Any . Pack ( message , "foo.bar/baz" ) ;
var any = Any . Pack ( message , "foo.bar/baz" ) ;
AssertJson ( "{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }" , formatter . Format ( any ) ) ;
AssertJson ( "{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }" , formatter . Format ( any ) ) ;
@ -513,7 +530,7 @@ namespace Google.Protobuf
public void AnyNested ( )
public void AnyNested ( )
{
{
var registry = TypeRegistry . FromMessages ( TestWellKnownTypes . Descriptor , TestAllTypes . Descriptor ) ;
var registry = TypeRegistry . FromMessages ( TestWellKnownTypes . Descriptor , TestAllTypes . Descriptor ) ;
var formatter = new JsonFormatter ( new JsonFormatter . Settings ( false , registry ) ) ;
var formatter = new JsonFormatter ( JsonFormatter . Settings . Default . WithTypeRegistry ( registry ) ) ;
// Nest an Any as the value of an Any.
// Nest an Any as the value of an Any.
var doubleNestedMessage = new TestAllTypes { SingleInt32 = 2 0 } ;
var doubleNestedMessage = new TestAllTypes { SingleInt32 = 2 0 } ;