Format() 공개 메소드

Formats the specified message as JSON.
public Format ( IMessage message ) : string
message IMessage The message to format.
리턴 string
예제 #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()));
        }
예제 #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));
        }
예제 #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);
        }
예제 #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\""));
        }
예제 #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);
        }
예제 #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);
        }
예제 #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"));
        }
예제 #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"));
        }
예제 #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));
        }
예제 #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));
        }
예제 #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));
        }
예제 #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);
        }
예제 #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));
        }
예제 #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));
        }
예제 #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));
        }
예제 #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));
 }
예제 #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));
 }
예제 #19
0
        public void DefaultValues_WhenIncluded()
        {
            var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));

            AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
        }
예제 #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));
        }
예제 #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));
        }
예제 #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));
 }
예제 #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));
        }
예제 #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"));
 }
예제 #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));
        }
예제 #26
0
 public void DefaultValues_WhenIncluded()
 {
     var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));
     AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
 }
예제 #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));
 }
예제 #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));
        }
예제 #29
0
 public override void WriteJson(JsonWriter writer, object value,
                                JsonSerializer serializer)
 {
     writer.WriteRawValue(_formatter.Format((IMessage)value));
 }
예제 #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));
 }