Format() public method

Formats the specified message as JSON.
public Format ( IMessage message ) : string
message IMessage The message to format.
return string
Esempio n. 1
0
        public void DefaultValues_WhenOmitted()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false));

            AssertJson("{ }", formatter.Format(new ForeignMessage()));
            AssertJson("{ }", formatter.Format(new TestAllTypes()));
            AssertJson("{ }", formatter.Format(new TestMap()));
        }
        public void DefaultValues_WhenOmitted()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false));

            AssertJson("{ }", formatter.Format(new ForeignMessage()));
            AssertJson("{ }", formatter.Format(new TestAllTypes()));
            AssertJson("{ }", formatter.Format(new TestMap()));
        }
Esempio n. 3
0
        public void NullValueNotInOneof_FormatDefaults()
        {
            var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
            var message   = new NullValueNotInOneof();

            AssertJson("{ 'nullValue': null }", formatter.Format(message));
        }
Esempio n. 4
0
        public void WithFormatDefaultValues_DoesNotAffectOneofFields()
        {
            var message   = new TestOneof();
            var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
            var json      = formatter.Format(message);

            AssertJson("{ }", json);
        }
Esempio n. 5
0
        public void WithFormatDefaultValues_DoesNotAffectMessageFields()
        {
            var message   = new TestAllTypes();
            var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
            var json      = formatter.Format(message);

            Assert.IsFalse(json.Contains("\"singleNestedMessage\""));
            Assert.IsFalse(json.Contains("\"singleForeignMessage\""));
            Assert.IsFalse(json.Contains("\"singleImportMessage\""));
        }
Esempio n. 6
0
        public void WithFormatDefaultValues_DoesNotAffectProto2Fields()
        {
            var message = new TestProtos.Proto2.ForeignMessage {
                C = 0
            };
            var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
            var json      = formatter.Format(message);

            // The specified field is formatted, but the non-specified field (d) is not.
            AssertJson("{ 'c': 0 }", json);
        }
Esempio n. 7
0
        public void WithFormatDefaultValues_DoesNotAffectProto3OptionalFields()
        {
            var message = new TestProto3Optional {
                OptionalInt32 = 0
            };
            var formatter = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
            var json      = formatter.Format(message);

            // The non-optional proto3 fields are formatted, as is the optional-but-specified field.
            AssertJson("{ 'optionalInt32': 0, 'singularInt32': 0, 'singularInt64': '0' }", json);
        }
Esempio n. 8
0
        public void WrapperFormatting_IncludeNull()
        {
            // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
            var message = new TestWellKnownTypes {
                Int32Field = 10
            };
            var formatter  = new JsonFormatter(new JsonFormatter.Settings(true));
            var actualJson = formatter.Format(message);

            Assert.IsTrue(actualJson.Contains("\"int64Field\": null"));
            Assert.IsFalse(actualJson.Contains("\"int32Field\": null"));
        }
Esempio n. 9
0
        public void WrapperFormatting_FormatDefaultValuesDoesNotFormatNull()
        {
            // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
            var message = new TestWellKnownTypes {
                Int32Field = 10
            };
            var formatter  = new JsonFormatter(JsonFormatter.Settings.Default.WithFormatDefaultValues(true));
            var actualJson = formatter.Format(message);

            // This *used* to include "int64Field": null, but that was a bug.
            // WithDefaultValues should not affect message fields, including wrapper types.
            Assert.IsFalse(actualJson.Contains("\"int64Field\": null"));
            Assert.IsTrue(actualJson.Contains("\"int32Field\": 10"));
        }
Esempio n. 10
0
        public void Any_WellKnownType()
        {
            var registry  = TypeRegistry.FromMessages(Timestamp.Descriptor);
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));
            var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
            var original  = Any.Pack(timestamp);
            var json      = formatter.Format(original); // This is tested in JsonFormatterTest
            var parser    = new JsonParser(new JsonParser.Settings(10, registry));

            Assert.AreEqual(original, parser.Parse <Any>(json));
            string valueFirstJson = "{ \"value\": \"1673-06-19T12:34:56Z\", \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\" }";

            Assert.AreEqual(original, parser.Parse <Any>(valueFirstJson));
        }
Esempio n. 11
0
        public void OutputIsInNumericFieldOrder_WithDefaults()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
            var message   = new TestJsonFieldOrdering();

            AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Format(message));
            message = new TestJsonFieldOrdering {
                O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain"
            };
            AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
            message = new TestJsonFieldOrdering {
                O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain"
            };
            AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
        }
Esempio n. 12
0
        public void Oneof(string fooStringValue, string expectedJson)
        {
            var message = new TestOneof();

            if (fooStringValue != null)
            {
                message.FooString = fooStringValue;
            }

            // We should get the same result both with and without "format default values".
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false));

            AssertJson(expectedJson, formatter.Format(message));
            formatter = new JsonFormatter(new JsonFormatter.Settings(true));
            AssertJson(expectedJson, formatter.Format(message));
        }
Esempio n. 13
0
        public void EnumAsInt()
        {
            var message = new TestAllTypes
            {
                SingleForeignEnum   = ForeignEnum.ForeignBar,
                RepeatedForeignEnum = { ForeignEnum.ForeignBaz, (ForeignEnum)100, 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);
        }
Esempio n. 14
0
        public void Any_Nested()
        {
            var registry            = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
            var formatter           = new JsonFormatter(new JsonFormatter.Settings(false, registry));
            var parser              = new JsonParser(new JsonParser.Settings(10, registry));
            var doubleNestedMessage = new TestAllTypes {
                SingleInt32 = 20
            };
            var nestedMessage = Any.Pack(doubleNestedMessage);
            var message       = new TestWellKnownTypes {
                AnyField = Any.Pack(nestedMessage)
            };
            var json = formatter.Format(message);

            // Use the descriptor-based parser just for a change.
            Assert.AreEqual(message, parser.Parse(json, TestWellKnownTypes.Descriptor));
        }
Esempio n. 15
0
        public void AnyNested()
        {
            var registry  = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));

            // Nest an Any as the value of an Any.
            var doubleNestedMessage = new TestAllTypes {
                SingleInt32 = 20
            };
            var nestedMessage = Any.Pack(doubleNestedMessage);
            var message       = new TestWellKnownTypes {
                AnyField = Any.Pack(nestedMessage)
            };

            AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 20 } } }",
                       formatter.Format(message));
        }
Esempio n. 16
0
        public void Any_RegularMessage()
        {
            var registry  = TypeRegistry.FromMessages(TestAllTypes.Descriptor);
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
            var message   = new TestAllTypes {
                SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage {
                    Bb = 20
                }
            };
            var original = Any.Pack(message);
            var json     = formatter.Format(original); // This is tested in JsonFormatterTest
            var parser   = new JsonParser(new JsonParser.Settings(10, registry));

            Assert.AreEqual(original, parser.Parse <Any>(json));
            string valueFirstJson = "{ \"singleInt32\": 10, \"singleNestedMessage\": { \"bb\": 20 }, \"@type\": \"type.googleapis.com/protobuf_unittest.TestAllTypes\" }";

            Assert.AreEqual(original, parser.Parse <Any>(valueFirstJson));
        }
Esempio n. 17
0
 public void AnyMessageType()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
     var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } };
     var any = Any.Pack(message);
     AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Format(any));
 }
Esempio n. 18
0
 /// <summary>
 /// Converts a message to JSON for diagnostic purposes with no extra context.
 /// </summary>
 /// <remarks>
 /// <para>
 /// This differs from calling <see cref="Format(IMessage)"/> on the default JSON
 /// formatter in its handling of <see cref="Any"/>. As no type registry is available
 /// in <see cref="object.ToString"/> calls, the normal way of resolving the type of
 /// an <c>Any</c> message cannot be applied. Instead, a JSON property named <c>@value</c>
 /// is included with the base64 data from the <see cref="Any.Value"/> property of the message.
 /// </para>
 /// <para>The value returned by this method is only designed to be used for diagnostic
 /// purposes. It may not be parsable by <see cref="JsonParser"/>, and may not be parsable
 /// by other Protocol Buffer implementations.</para>
 /// </remarks>
 /// <param name="message">The message to format for diagnostic purposes.</param>
 /// <returns>The diagnostic-only JSON representation of the message</returns>
 public static string ToDiagnosticString(IMessage message)
 {
     ProtoPreconditions.CheckNotNull(message, nameof(message));
     return(diagnosticFormatter.Format(message));
 }
Esempio n. 19
0
        public void DefaultValues_WhenIncluded()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));

            AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
        }
Esempio n. 20
0
        public void AnyMessageType()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
            var message   = new TestAllTypes {
                SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage {
                    Bb = 20
                }
            };
            var any = Any.Pack(message);

            AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Format(any));
        }
Esempio n. 21
0
        public void AnyMessageType_CustomPrefix()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
            var message   = new TestAllTypes {
                SingleInt32 = 10
            };
            var any = Any.Pack(message, "foo.bar/baz");

            AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any));
        }
Esempio n. 22
0
 public void AnyMessageType_CustomPrefix()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
     var message = new TestAllTypes { SingleInt32 = 10 };
     var any = Any.Pack(message, "foo.bar/baz");
     AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any));
 }
Esempio n. 23
0
        public void AnyWellKnownType()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(Timestamp.Descriptor)));
            var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
            var any       = Any.Pack(timestamp);

            AssertJson("{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }", formatter.Format(any));
        }
Esempio n. 24
0
 public void WrapperFormatting_IncludeNull()
 {
     // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
     var message = new TestWellKnownTypes { Int32Field = 10 };
     var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
     var actualJson = formatter.Format(message);
     Assert.IsTrue(actualJson.Contains("\"int64Field\": null"));
     Assert.IsFalse(actualJson.Contains("\"int32Field\": null"));
 }
Esempio n. 25
0
        public void Oneof(string fooStringValue, string expectedJson)
        {
            var message = new TestOneof();
            if (fooStringValue != null)
            {
                message.FooString = fooStringValue;
            }

            // We should get the same result both with and without "format default values".
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
            AssertJson(expectedJson, formatter.Format(message));
            formatter = new JsonFormatter(new JsonFormatter.Settings(true));
            AssertJson(expectedJson, formatter.Format(message));
        }
Esempio n. 26
0
 public void DefaultValues_WhenIncluded()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));
     AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
 }
Esempio n. 27
0
 public void OutputIsInNumericFieldOrder_WithDefaults()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
     var message = new TestJsonFieldOrdering();
     AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Format(message));
     message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
     AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
     message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
     AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
 }
Esempio n. 28
0
        public void AnyNested()
        {
            var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
            var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));

            // Nest an Any as the value of an Any.
            var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 };
            var nestedMessage = Any.Pack(doubleNestedMessage);
            var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) };
            AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 20 } } }",
                formatter.Format(message));
        }
Esempio n. 29
0
 public override void WriteJson(JsonWriter writer, object value,
                                JsonSerializer serializer)
 {
     writer.WriteRawValue(_formatter.Format((IMessage)value));
 }
Esempio n. 30
0
 public void AnyWellKnownType()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(Timestamp.Descriptor)));
     var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
     var any = Any.Pack(timestamp);
     AssertJson("{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }", formatter.Format(any));
 }