void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldNameConvention()

in extra/protobuf/protobuf-24.4/conformance/binary_json_conformance_suite.cc [1839:3283]


void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldNameConvention() {
  RunValidJsonTest("FieldNameInSnakeCase", REQUIRED,
                   R"({
        "fieldname1": 1,
        "fieldName2": 2,
        "FieldName3": 3,
        "fieldName4": 4
      })",
                   R"(
        fieldname1: 1
        field_name2: 2
        _field_name3: 3
        field__name4_: 4
      )");
  RunValidJsonTest("FieldNameWithNumbers", REQUIRED,
                   R"({
        "field0name5": 5,
        "field0Name6": 6
      })",
                   R"(
        field0name5: 5
        field_0_name6: 6
      )");
  RunValidJsonTest("FieldNameWithMixedCases", REQUIRED,
                   R"({
        "fieldName7": 7,
        "FieldName8": 8,
        "fieldName9": 9,
        "FieldName10": 10,
        "FIELDNAME11": 11,
        "FIELDName12": 12
      })",
                   R"(
        fieldName7: 7
        FieldName8: 8
        field_Name9: 9
        Field_Name10: 10
        FIELD_NAME11: 11
        FIELD_name12: 12
      )");
  RunValidJsonTest("FieldNameWithDoubleUnderscores", RECOMMENDED,
                   R"({
        "FieldName13": 13,
        "FieldName14": 14,
        "fieldName15": 15,
        "fieldName16": 16,
        "fieldName17": 17,
        "FieldName18": 18
      })",
                   R"(
        __field_name13: 13
        __Field_name14: 14
        field__name15: 15
        field__Name16: 16
        field_name17__: 17
        Field_name18__: 18
      )");
  // Using the original proto field name in JSON is also allowed.
  RunValidJsonTest("OriginalProtoFieldName", REQUIRED,
                   R"({
        "fieldname1": 1,
        "field_name2": 2,
        "_field_name3": 3,
        "field__name4_": 4,
        "field0name5": 5,
        "field_0_name6": 6,
        "fieldName7": 7,
        "FieldName8": 8,
        "field_Name9": 9,
        "Field_Name10": 10,
        "FIELD_NAME11": 11,
        "FIELD_name12": 12,
        "__field_name13": 13,
        "__Field_name14": 14,
        "field__name15": 15,
        "field__Name16": 16,
        "field_name17__": 17,
        "Field_name18__": 18
      })",
                   R"(
        fieldname1: 1
        field_name2: 2
        _field_name3: 3
        field__name4_: 4
        field0name5: 5
        field_0_name6: 6
        fieldName7: 7
        FieldName8: 8
        field_Name9: 9
        Field_Name10: 10
        FIELD_NAME11: 11
        FIELD_name12: 12
        __field_name13: 13
        __Field_name14: 14
        field__name15: 15
        field__Name16: 16
        field_name17__: 17
        Field_name18__: 18
      )");
  // Field names can be escaped.
  RunValidJsonTest("FieldNameEscaped", REQUIRED, R"({"fieldn\u0061me1": 1})",
                   "fieldname1: 1");
  // String ends with escape character.
  ExpectParseFailureForJson("StringEndsWithEscapeChar", RECOMMENDED,
                            "{\"optionalString\": \"abc\\");
  // Field names must be quoted (or it's not valid JSON).
  ExpectParseFailureForJson("FieldNameNotQuoted", RECOMMENDED,
                            "{fieldname1: 1}");
  // Trailing comma is not allowed (not valid JSON).
  ExpectParseFailureForJson("TrailingCommaInAnObject", RECOMMENDED,
                            R"({"fieldname1":1,})");
  ExpectParseFailureForJson("TrailingCommaInAnObjectWithSpace", RECOMMENDED,
                            R"({"fieldname1":1 ,})");
  ExpectParseFailureForJson("TrailingCommaInAnObjectWithSpaceCommaSpace",
                            RECOMMENDED, R"({"fieldname1":1 , })");
  ExpectParseFailureForJson("TrailingCommaInAnObjectWithNewlines", RECOMMENDED,
                            R"({
        "fieldname1":1,
      })");
  // JSON doesn't support comments.
  ExpectParseFailureForJson("JsonWithComments", RECOMMENDED,
                            R"({
        // This is a comment.
        "fieldname1": 1
      })");
  // JSON spec says whitespace doesn't matter, so try a few spacings to be sure.
  RunValidJsonTest("OneLineNoSpaces", RECOMMENDED,
                   "{\"optionalInt32\":1,\"optionalInt64\":2}",
                   R"(
        optional_int32: 1
        optional_int64: 2
      )");
  RunValidJsonTest("OneLineWithSpaces", RECOMMENDED,
                   "{ \"optionalInt32\" : 1 , \"optionalInt64\" : 2 }",
                   R"(
        optional_int32: 1
        optional_int64: 2
      )");
  RunValidJsonTest("MultilineNoSpaces", RECOMMENDED,
                   "{\n\"optionalInt32\"\n:\n1\n,\n\"optionalInt64\"\n:\n2\n}",
                   R"(
        optional_int32: 1
        optional_int64: 2
      )");
  RunValidJsonTest(
      "MultilineWithSpaces", RECOMMENDED,
      "{\n  \"optionalInt32\"  :  1\n  ,\n  \"optionalInt64\"  :  2\n}\n",
      R"(
        optional_int32: 1
        optional_int64: 2
      )");
  // Missing comma between key/value pairs.
  ExpectParseFailureForJson("MissingCommaOneLine", RECOMMENDED,
                            "{ \"optionalInt32\": 1 \"optionalInt64\": 2 }");
  ExpectParseFailureForJson(
      "MissingCommaMultiline", RECOMMENDED,
      "{\n  \"optionalInt32\": 1\n  \"optionalInt64\": 2\n}");
  // Duplicated field names are not allowed.
  ExpectParseFailureForJson("FieldNameDuplicate", RECOMMENDED,
                            R"({
        "optionalNestedMessage": {a: 1},
        "optionalNestedMessage": {}
      })");
  ExpectParseFailureForJson("FieldNameDuplicateDifferentCasing1", RECOMMENDED,
                            R"({
        "optional_nested_message": {a: 1},
        "optionalNestedMessage": {}
      })");
  ExpectParseFailureForJson("FieldNameDuplicateDifferentCasing2", RECOMMENDED,
                            R"({
        "optionalNestedMessage": {a: 1},
        "optional_nested_message": {}
      })");
  // Serializers should use lowerCamelCase by default.
  RunValidJsonTestWithValidator(
      "FieldNameInLowerCamelCase", REQUIRED,
      R"({
        "fieldname1": 1,
        "fieldName2": 2,
        "FieldName3": 3,
        "fieldName4": 4
      })",
      [](const Json::Value& value) {
        return value.isMember("fieldname1") && value.isMember("fieldName2") &&
               value.isMember("FieldName3") && value.isMember("fieldName4");
      },
      true);
  RunValidJsonTestWithValidator(
      "FieldNameWithNumbers", REQUIRED,
      R"({
        "field0name5": 5,
        "field0Name6": 6
      })",
      [](const Json::Value& value) {
        return value.isMember("field0name5") && value.isMember("field0Name6");
      },
      true);
  RunValidJsonTestWithValidator(
      "FieldNameWithMixedCases", REQUIRED,
      R"({
        "fieldName7": 7,
        "FieldName8": 8,
        "fieldName9": 9,
        "FieldName10": 10,
        "FIELDNAME11": 11,
        "FIELDName12": 12
      })",
      [](const Json::Value& value) {
        return value.isMember("fieldName7") && value.isMember("FieldName8") &&
               value.isMember("fieldName9") && value.isMember("FieldName10") &&
               value.isMember("FIELDNAME11") && value.isMember("FIELDName12");
      },
      true);
  RunValidJsonTestWithValidator(
      "FieldNameWithDoubleUnderscores", RECOMMENDED,
      R"({
        "FieldName13": 13,
        "FieldName14": 14,
        "fieldName15": 15,
        "fieldName16": 16,
        "fieldName17": 17,
        "FieldName18": 18
      })",
      [](const Json::Value& value) {
        return value.isMember("FieldName13") && value.isMember("FieldName14") &&
               value.isMember("fieldName15") && value.isMember("fieldName16") &&
               value.isMember("fieldName17") && value.isMember("FieldName18");
      },
      true);
  RunValidJsonTestWithValidator(
      "StoresDefaultPrimitive", REQUIRED,
      R"({
        "FieldName13": 0
      })",
      [](const Json::Value& value) { return value.isMember("FieldName13"); },
      false);
  RunValidJsonTestWithValidator(
      "SkipsDefaultPrimitive", REQUIRED,
      R"({
        "FieldName13": 0
      })",
      [](const Json::Value& value) { return !value.isMember("FieldName13"); },
      true);
  RunValidJsonTestWithValidator(
      "FieldNameExtension", RECOMMENDED,
      R"({
        "[protobuf_test_messages.proto2.extension_int32]": 1
      })",
      [](const Json::Value& value) {
        return value.isMember(
            "[protobuf_test_messages.proto2.extension_int32]");
      },
      false);
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForNonRepeatedTypes() {
  // Integer fields.
  RunValidJsonTest("Int32FieldMaxValue", REQUIRED,
                   R"({"optionalInt32": 2147483647})",
                   "optional_int32: 2147483647");
  RunValidJsonTest("Int32FieldMinValue", REQUIRED,
                   R"({"optionalInt32": -2147483648})",
                   "optional_int32: -2147483648");
  RunValidJsonTest("Uint32FieldMaxValue", REQUIRED,
                   R"({"optionalUint32": 4294967295})",
                   "optional_uint32: 4294967295");
  RunValidJsonTest("Int64FieldMaxValue", REQUIRED,
                   R"({"optionalInt64": "9223372036854775807"})",
                   "optional_int64: 9223372036854775807");
  RunValidJsonTest("Int64FieldMinValue", REQUIRED,
                   R"({"optionalInt64": "-9223372036854775808"})",
                   "optional_int64: -9223372036854775808");
  RunValidJsonTest("Uint64FieldMaxValue", REQUIRED,
                   R"({"optionalUint64": "18446744073709551615"})",
                   "optional_uint64: 18446744073709551615");
  // While not the largest Int64, this is the largest
  // Int64 which can be exactly represented within an
  // IEEE-754 64-bit float, which is the expected level
  // of interoperability guarantee. Larger values may
  // work in some implementations, but should not be
  // relied upon.
  RunValidJsonTest("Int64FieldMaxValueNotQuoted", REQUIRED,
                   R"({"optionalInt64": 9223372036854774784})",
                   "optional_int64: 9223372036854774784");
  RunValidJsonTest("Int64FieldMinValueNotQuoted", REQUIRED,
                   R"({"optionalInt64": -9223372036854775808})",
                   "optional_int64: -9223372036854775808");
  // Largest interoperable Uint64; see comment above
  // for Int64FieldMaxValueNotQuoted.
  RunValidJsonTest("Uint64FieldMaxValueNotQuoted", REQUIRED,
                   R"({"optionalUint64": 18446744073709549568})",
                   "optional_uint64: 18446744073709549568");
  // Values can be represented as JSON strings.
  RunValidJsonTest("Int32FieldStringValue", REQUIRED,
                   R"({"optionalInt32": "2147483647"})",
                   "optional_int32: 2147483647");
  RunValidJsonTest("Int32FieldStringValueEscaped", REQUIRED,
                   R"({"optionalInt32": "2\u003147483647"})",
                   "optional_int32: 2147483647");

  // Parsers reject out-of-bound integer values.
  ExpectParseFailureForJson("Int32FieldTooLarge", REQUIRED,
                            R"({"optionalInt32": 2147483648})");
  ExpectParseFailureForJson("Int32FieldTooSmall", REQUIRED,
                            R"({"optionalInt32": -2147483649})");
  ExpectParseFailureForJson("Uint32FieldTooLarge", REQUIRED,
                            R"({"optionalUint32": 4294967296})");
  ExpectParseFailureForJson("Int64FieldTooLarge", REQUIRED,
                            R"({"optionalInt64": "9223372036854775808"})");
  ExpectParseFailureForJson("Int64FieldTooSmall", REQUIRED,
                            R"({"optionalInt64": "-9223372036854775809"})");
  ExpectParseFailureForJson("Uint64FieldTooLarge", REQUIRED,
                            R"({"optionalUint64": "18446744073709551616"})");
  // Parser reject non-integer numeric values as well.
  ExpectParseFailureForJson("Int32FieldNotInteger", REQUIRED,
                            R"({"optionalInt32": 0.5})");
  ExpectParseFailureForJson("Uint32FieldNotInteger", REQUIRED,
                            R"({"optionalUint32": 0.5})");
  ExpectParseFailureForJson("Int64FieldNotInteger", REQUIRED,
                            R"({"optionalInt64": "0.5"})");
  ExpectParseFailureForJson("Uint64FieldNotInteger", REQUIRED,
                            R"({"optionalUint64": "0.5"})");

  // Integers but represented as float values are accepted.
  RunValidJsonTest("Int32FieldFloatTrailingZero", REQUIRED,
                   R"({"optionalInt32": 100000.000})",
                   "optional_int32: 100000");
  RunValidJsonTest("Int32FieldExponentialFormat", REQUIRED,
                   R"({"optionalInt32": 1e5})", "optional_int32: 100000");
  RunValidJsonTest("Int32FieldMaxFloatValue", REQUIRED,
                   R"({"optionalInt32": 2.147483647e9})",
                   "optional_int32: 2147483647");
  RunValidJsonTest("Int32FieldMinFloatValue", REQUIRED,
                   R"({"optionalInt32": -2.147483648e9})",
                   "optional_int32: -2147483648");
  RunValidJsonTest("Uint32FieldMaxFloatValue", REQUIRED,
                   R"({"optionalUint32": 4.294967295e9})",
                   "optional_uint32: 4294967295");

  // Parser reject non-numeric values.
  ExpectParseFailureForJson("Int32FieldNotNumber", REQUIRED,
                            R"({"optionalInt32": "3x3"})");
  ExpectParseFailureForJson("Uint32FieldNotNumber", REQUIRED,
                            R"({"optionalUint32": "3x3"})");
  ExpectParseFailureForJson("Int64FieldNotNumber", REQUIRED,
                            R"({"optionalInt64": "3x3"})");
  ExpectParseFailureForJson("Uint64FieldNotNumber", REQUIRED,
                            R"({"optionalUint64": "3x3"})");
  // JSON does not allow "+" on numeric values.
  ExpectParseFailureForJson("Int32FieldPlusSign", REQUIRED,
                            R"({"optionalInt32": +1})");
  // JSON doesn't allow leading 0s.
  ExpectParseFailureForJson("Int32FieldLeadingZero", REQUIRED,
                            R"({"optionalInt32": 01})");
  ExpectParseFailureForJson("Int32FieldNegativeWithLeadingZero", REQUIRED,
                            R"({"optionalInt32": -01})");
  // String values must follow the same syntax rule. Specifically leading
  // or trailing spaces are not allowed.
  ExpectParseFailureForJson("Int32FieldLeadingSpace", REQUIRED,
                            R"({"optionalInt32": " 1"})");
  ExpectParseFailureForJson("Int32FieldTrailingSpace", REQUIRED,
                            R"({"optionalInt32": "1 "})");

  // 64-bit values are serialized as strings.
  RunValidJsonTestWithValidator(
      "Int64FieldBeString", RECOMMENDED, R"({"optionalInt64": 1})",
      [](const Json::Value& value) {
        return value["optionalInt64"].type() == Json::stringValue &&
               value["optionalInt64"].asString() == "1";
      },
      true);
  RunValidJsonTestWithValidator(
      "Uint64FieldBeString", RECOMMENDED, R"({"optionalUint64": 1})",
      [](const Json::Value& value) {
        return value["optionalUint64"].type() == Json::stringValue &&
               value["optionalUint64"].asString() == "1";
      },
      true);

  // Bool fields.
  RunValidJsonTest("BoolFieldTrue", REQUIRED, R"({"optionalBool":true})",
                   "optional_bool: true");
  RunValidJsonTest("BoolFieldFalse", REQUIRED, R"({"optionalBool":false})",
                   "optional_bool: false");

  // Other forms are not allowed.
  ExpectParseFailureForJson("BoolFieldIntegerZero", RECOMMENDED,
                            R"({"optionalBool":0})");
  ExpectParseFailureForJson("BoolFieldIntegerOne", RECOMMENDED,
                            R"({"optionalBool":1})");
  ExpectParseFailureForJson("BoolFieldCamelCaseTrue", RECOMMENDED,
                            R"({"optionalBool":True})");
  ExpectParseFailureForJson("BoolFieldCamelCaseFalse", RECOMMENDED,
                            R"({"optionalBool":False})");
  ExpectParseFailureForJson("BoolFieldAllCapitalTrue", RECOMMENDED,
                            R"({"optionalBool":TRUE})");
  ExpectParseFailureForJson("BoolFieldAllCapitalFalse", RECOMMENDED,
                            R"({"optionalBool":FALSE})");
  ExpectParseFailureForJson("BoolFieldDoubleQuotedTrue", RECOMMENDED,
                            R"({"optionalBool":"true"})");
  ExpectParseFailureForJson("BoolFieldDoubleQuotedFalse", RECOMMENDED,
                            R"({"optionalBool":"false"})");

  // Float fields.
  RunValidJsonTest("FloatFieldMinPositiveValue", REQUIRED,
                   R"({"optionalFloat": 1.175494e-38})",
                   "optional_float: 1.175494e-38");
  RunValidJsonTest("FloatFieldMaxNegativeValue", REQUIRED,
                   R"({"optionalFloat": -1.175494e-38})",
                   "optional_float: -1.175494e-38");
  RunValidJsonTest("FloatFieldMaxPositiveValue", REQUIRED,
                   R"({"optionalFloat": 3.402823e+38})",
                   "optional_float: 3.402823e+38");
  RunValidJsonTest("FloatFieldMinNegativeValue", REQUIRED,
                   R"({"optionalFloat": 3.402823e+38})",
                   "optional_float: 3.402823e+38");
  // Values can be quoted.
  RunValidJsonTest("FloatFieldQuotedValue", REQUIRED,
                   R"({"optionalFloat": "1"})", "optional_float: 1");
  // Special values.
  RunValidJsonTest("FloatFieldNan", REQUIRED, R"({"optionalFloat": "NaN"})",
                   "optional_float: nan");
  RunValidJsonTest("FloatFieldInfinity", REQUIRED,
                   R"({"optionalFloat": "Infinity"})", "optional_float: inf");
  RunValidJsonTest("FloatFieldNegativeInfinity", REQUIRED,
                   R"({"optionalFloat": "-Infinity"})", "optional_float: -inf");
  // Non-canonical Nan will be correctly normalized.
  {
    TestAllTypesProto3 message;
    // IEEE floating-point standard 32-bit quiet NaN:
    //   0111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
    message.set_optional_float(WireFormatLite::DecodeFloat(0x7FA12345));
    RunValidJsonTestWithProtobufInput("FloatFieldNormalizeQuietNan", REQUIRED,
                                      message, "optional_float: nan");
    // IEEE floating-point standard 64-bit signaling NaN:
    //   1111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
    message.set_optional_float(WireFormatLite::DecodeFloat(0xFFB54321));
    RunValidJsonTestWithProtobufInput("FloatFieldNormalizeSignalingNan",
                                      REQUIRED, message, "optional_float: nan");
  }

  // Special values must be quoted.
  ExpectParseFailureForJson("FloatFieldNanNotQuoted", RECOMMENDED,
                            R"({"optionalFloat": NaN})");
  ExpectParseFailureForJson("FloatFieldInfinityNotQuoted", RECOMMENDED,
                            R"({"optionalFloat": Infinity})");
  ExpectParseFailureForJson("FloatFieldNegativeInfinityNotQuoted", RECOMMENDED,
                            R"({"optionalFloat": -Infinity})");
  // Parsers should reject out-of-bound values.
  ExpectParseFailureForJson("FloatFieldTooSmall", REQUIRED,
                            R"({"optionalFloat": -3.502823e+38})");
  ExpectParseFailureForJson("FloatFieldTooLarge", REQUIRED,
                            R"({"optionalFloat": 3.502823e+38})");

  // Double fields.
  RunValidJsonTest("DoubleFieldMinPositiveValue", REQUIRED,
                   R"({"optionalDouble": 2.22507e-308})",
                   "optional_double: 2.22507e-308");
  RunValidJsonTest("DoubleFieldMaxNegativeValue", REQUIRED,
                   R"({"optionalDouble": -2.22507e-308})",
                   "optional_double: -2.22507e-308");
  RunValidJsonTest("DoubleFieldMaxPositiveValue", REQUIRED,
                   R"({"optionalDouble": 1.79769e+308})",
                   "optional_double: 1.79769e+308");
  RunValidJsonTest("DoubleFieldMinNegativeValue", REQUIRED,
                   R"({"optionalDouble": -1.79769e+308})",
                   "optional_double: -1.79769e+308");
  // Values can be quoted.
  RunValidJsonTest("DoubleFieldQuotedValue", REQUIRED,
                   R"({"optionalDouble": "1"})", "optional_double: 1");
  // Special values.
  RunValidJsonTest("DoubleFieldNan", REQUIRED, R"({"optionalDouble": "NaN"})",
                   "optional_double: nan");
  RunValidJsonTest("DoubleFieldInfinity", REQUIRED,
                   R"({"optionalDouble": "Infinity"})", "optional_double: inf");
  RunValidJsonTest("DoubleFieldNegativeInfinity", REQUIRED,
                   R"({"optionalDouble": "-Infinity"})",
                   "optional_double: -inf");
  // Non-canonical Nan will be correctly normalized.
  {
    TestAllTypesProto3 message;
    message.set_optional_double(
        WireFormatLite::DecodeDouble(int64_t{0x7FFA123456789ABC}));
    RunValidJsonTestWithProtobufInput("DoubleFieldNormalizeQuietNan", REQUIRED,
                                      message, "optional_double: nan");
    message.set_optional_double(
        WireFormatLite::DecodeDouble(uint64_t{0xFFFBCBA987654321}));
    RunValidJsonTestWithProtobufInput("DoubleFieldNormalizeSignalingNan",
                                      REQUIRED, message,
                                      "optional_double: nan");
  }

  // Special values must be quoted.
  ExpectParseFailureForJson("DoubleFieldNanNotQuoted", RECOMMENDED,
                            R"({"optionalDouble": NaN})");
  ExpectParseFailureForJson("DoubleFieldInfinityNotQuoted", RECOMMENDED,
                            R"({"optionalDouble": Infinity})");
  ExpectParseFailureForJson("DoubleFieldNegativeInfinityNotQuoted", RECOMMENDED,
                            R"({"optionalDouble": -Infinity})");

  // Parsers should reject out-of-bound values.
  ExpectParseFailureForJson("DoubleFieldTooSmall", REQUIRED,
                            R"({"optionalDouble": -1.89769e+308})");
  ExpectParseFailureForJson("DoubleFieldTooLarge", REQUIRED,
                            R"({"optionalDouble": +1.89769e+308})");

  // Enum fields.
  RunValidJsonTest("EnumField", REQUIRED, R"({"optionalNestedEnum": "FOO"})",
                   "optional_nested_enum: FOO");
  // Enum fields with alias
  RunValidJsonTest("EnumFieldWithAlias", REQUIRED,
                   R"({"optionalAliasedEnum": "ALIAS_BAZ"})",
                   "optional_aliased_enum: ALIAS_BAZ");
  RunValidJsonTest("EnumFieldWithAliasUseAlias", REQUIRED,
                   R"({"optionalAliasedEnum": "MOO"})",
                   "optional_aliased_enum: ALIAS_BAZ");
  RunValidJsonTest("EnumFieldWithAliasLowerCase", REQUIRED,
                   R"({"optionalAliasedEnum": "moo"})",
                   "optional_aliased_enum: ALIAS_BAZ");
  RunValidJsonTest("EnumFieldWithAliasDifferentCase", REQUIRED,
                   R"({"optionalAliasedEnum": "bAz"})",
                   "optional_aliased_enum: ALIAS_BAZ");
  // Enum values must be represented as strings.
  ExpectParseFailureForJson("EnumFieldNotQuoted", REQUIRED,
                            R"({"optionalNestedEnum": FOO})");
  // Numeric values are allowed.
  RunValidJsonTest("EnumFieldNumericValueZero", REQUIRED,
                   R"({"optionalNestedEnum": 0})", "optional_nested_enum: FOO");
  RunValidJsonTest("EnumFieldNumericValueNonZero", REQUIRED,
                   R"({"optionalNestedEnum": 1})", "optional_nested_enum: BAR");
  // Unknown enum values are represented as numeric values.
  RunValidJsonTestWithValidator(
      "EnumFieldUnknownValue", REQUIRED, R"({"optionalNestedEnum": 123})",
      [](const Json::Value& value) {
        return value["optionalNestedEnum"].type() == Json::intValue &&
               value["optionalNestedEnum"].asInt() == 123;
      },
      true);

  // String fields.
  RunValidJsonTest("StringField", REQUIRED,
                   R"({"optionalString": "Hello world!"})",
                   R"(optional_string: "Hello world!")");
  RunValidJsonTest("StringFieldUnicode", REQUIRED,
                   // Google in Chinese.
                   R"({"optionalString": "谷歌"})",
                   R"(optional_string: "谷歌")");
  RunValidJsonTest("StringFieldEscape", REQUIRED,
                   R"({"optionalString": "\"\\\/\b\f\n\r\t"})",
                   R"(optional_string: "\"\\/\b\f\n\r\t")");
  RunValidJsonTest("StringFieldUnicodeEscape", REQUIRED,
                   R"({"optionalString": "\u8C37\u6B4C"})",
                   R"(optional_string: "谷歌")");
  RunValidJsonTest("StringFieldUnicodeEscapeWithLowercaseHexLetters", REQUIRED,
                   R"({"optionalString": "\u8c37\u6b4c"})",
                   R"(optional_string: "谷歌")");
  RunValidJsonTest(
      "StringFieldSurrogatePair", REQUIRED,
      // The character is an emoji: grinning face with smiling eyes. 😁
      R"({"optionalString": "\uD83D\uDE01"})",
      R"(optional_string: "\xF0\x9F\x98\x81")");
  RunValidJsonTest("StringFieldEmbeddedNull", REQUIRED,
                   R"({"optionalString": "Hello\u0000world!"})",
                   R"(optional_string: "Hello\000world!")");

  // Unicode escapes must start with "\u" (lowercase u).
  ExpectParseFailureForJson("StringFieldUppercaseEscapeLetter", RECOMMENDED,
                            R"({"optionalString": "\U8C37\U6b4C"})");
  ExpectParseFailureForJson("StringFieldInvalidEscape", RECOMMENDED,
                            R"({"optionalString": "\uXXXX\u6B4C"})");
  ExpectParseFailureForJson("StringFieldUnterminatedEscape", RECOMMENDED,
                            R"({"optionalString": "\u8C3"})");
  ExpectParseFailureForJson("StringFieldUnpairedHighSurrogate", RECOMMENDED,
                            R"({"optionalString": "\uD800"})");
  ExpectParseFailureForJson("StringFieldUnpairedLowSurrogate", RECOMMENDED,
                            R"({"optionalString": "\uDC00"})");
  ExpectParseFailureForJson("StringFieldSurrogateInWrongOrder", RECOMMENDED,
                            R"({"optionalString": "\uDE01\uD83D"})");
  ExpectParseFailureForJson("StringFieldNotAString", REQUIRED,
                            R"({"optionalString": 12345})");

  // Bytes fields.
  RunValidJsonTest("BytesField", REQUIRED, R"({"optionalBytes": "AQI="})",
                   R"(optional_bytes: "\x01\x02")");
  RunValidJsonTest("BytesFieldBase64Url", RECOMMENDED,
                   R"({"optionalBytes": "-_"})", R"(optional_bytes: "\xfb")");

  // Message fields.
  RunValidJsonTest("MessageField", REQUIRED,
                   R"({"optionalNestedMessage": {"a": 1234}})",
                   "optional_nested_message: {a: 1234}");

  // Oneof fields.
  ExpectParseFailureForJson("OneofFieldDuplicate", REQUIRED,
                            R"({"oneofUint32": 1, "oneofString": "test"})");
  RunValidJsonTest("OneofFieldNullFirst", REQUIRED,
                   R"({"oneofUint32": null, "oneofString": "test"})",
                   "oneof_string: \"test\"");
  RunValidJsonTest("OneofFieldNullSecond", REQUIRED,
                   R"({"oneofString": "test", "oneofUint32": null})",
                   "oneof_string: \"test\"");
  // Ensure zero values for oneof make it out/backs.
  TestAllTypesProto3 messageProto3;
  TestAllTypesProto2 messageProto2;
  TestOneofMessage(messageProto3, true);
  TestOneofMessage(messageProto2, false);
  RunValidJsonTest("OneofZeroUint32", RECOMMENDED, R"({"oneofUint32": 0})",
                   "oneof_uint32: 0");
  RunValidJsonTest("OneofZeroMessage", RECOMMENDED,
                   R"({"oneofNestedMessage": {}})", "oneof_nested_message: {}");
  RunValidJsonTest("OneofZeroString", RECOMMENDED, R"({"oneofString": ""})",
                   "oneof_string: \"\"");
  RunValidJsonTest("OneofZeroBytes", RECOMMENDED, R"({"oneofBytes": ""})",
                   "oneof_bytes: \"\"");
  RunValidJsonTest("OneofZeroBool", RECOMMENDED, R"({"oneofBool": false})",
                   "oneof_bool: false");
  RunValidJsonTest("OneofZeroUint64", RECOMMENDED, R"({"oneofUint64": 0})",
                   "oneof_uint64: 0");
  RunValidJsonTest("OneofZeroFloat", RECOMMENDED, R"({"oneofFloat": 0.0})",
                   "oneof_float: 0");
  RunValidJsonTest("OneofZeroDouble", RECOMMENDED, R"({"oneofDouble": 0.0})",
                   "oneof_double: 0");
  RunValidJsonTest("OneofZeroEnum", RECOMMENDED, R"({"oneofEnum":"FOO"})",
                   "oneof_enum: FOO");

  // Map fields.
  RunValidJsonTest("Int32MapField", REQUIRED,
                   R"({"mapInt32Int32": {"1": 2, "3": 4}})",
                   "map_int32_int32: {key: 1 value: 2}"
                   "map_int32_int32: {key: 3 value: 4}");
  ExpectParseFailureForJson("Int32MapFieldKeyNotQuoted", RECOMMENDED,
                            R"({"mapInt32Int32": {1: 2, 3: 4}})");
  RunValidJsonTest("Uint32MapField", REQUIRED,
                   R"({"mapUint32Uint32": {"1": 2, "3": 4}})",
                   "map_uint32_uint32: {key: 1 value: 2}"
                   "map_uint32_uint32: {key: 3 value: 4}");
  ExpectParseFailureForJson("Uint32MapFieldKeyNotQuoted", RECOMMENDED,
                            R"({"mapUint32Uint32": {1: 2, 3: 4}})");
  RunValidJsonTest("Int64MapField", REQUIRED,
                   R"({"mapInt64Int64": {"1": 2, "3": 4}})",
                   "map_int64_int64: {key: 1 value: 2}"
                   "map_int64_int64: {key: 3 value: 4}");
  ExpectParseFailureForJson("Int64MapFieldKeyNotQuoted", RECOMMENDED,
                            R"({"mapInt64Int64": {1: 2, 3: 4}})");
  RunValidJsonTest("Uint64MapField", REQUIRED,
                   R"({"mapUint64Uint64": {"1": 2, "3": 4}})",
                   "map_uint64_uint64: {key: 1 value: 2}"
                   "map_uint64_uint64: {key: 3 value: 4}");
  ExpectParseFailureForJson("Uint64MapFieldKeyNotQuoted", RECOMMENDED,
                            R"({"mapUint64Uint64": {1: 2, 3: 4}})");
  RunValidJsonTest("BoolMapField", REQUIRED,
                   R"({"mapBoolBool": {"true": true, "false": false}})",
                   "map_bool_bool: {key: true value: true}"
                   "map_bool_bool: {key: false value: false}");
  ExpectParseFailureForJson("BoolMapFieldKeyNotQuoted", RECOMMENDED,
                            R"({"mapBoolBool": {true: true, false: false}})");
  RunValidJsonTest("MessageMapField", REQUIRED,
                   R"({
        "mapStringNestedMessage": {
          "hello": {"a": 1234},
          "world": {"a": 5678}
        }
      })",
                   R"(
        map_string_nested_message: {
          key: "hello"
          value: {a: 1234}
        }
        map_string_nested_message: {
          key: "world"
          value: {a: 5678}
        }
      )");
  // Since Map keys are represented as JSON strings, escaping should be allowed.
  RunValidJsonTest("Int32MapEscapedKey", REQUIRED,
                   R"({"mapInt32Int32": {"\u0031": 2}})",
                   "map_int32_int32: {key: 1 value: 2}");
  RunValidJsonTest("Int64MapEscapedKey", REQUIRED,
                   R"({"mapInt64Int64": {"\u0031": 2}})",
                   "map_int64_int64: {key: 1 value: 2}");
  RunValidJsonTest("BoolMapEscapedKey", REQUIRED,
                   R"({"mapBoolBool": {"tr\u0075e": true}})",
                   "map_bool_bool: {key: true value: true}");

  // http://www.rfc-editor.org/rfc/rfc7159.txt says strings have to use double
  // quotes.
  ExpectParseFailureForJson("StringFieldSingleQuoteKey", RECOMMENDED,
                            R"({'optionalString': "Hello world!"})");
  ExpectParseFailureForJson("StringFieldSingleQuoteValue", RECOMMENDED,
                            R"({"optionalString": 'Hello world!'})");
  ExpectParseFailureForJson("StringFieldSingleQuoteBoth", RECOMMENDED,
                            R"({'optionalString': 'Hello world!'})");
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForRepeatedTypes() {
  // Repeated fields.
  RunValidJsonTest("PrimitiveRepeatedField", REQUIRED,
                   R"({"repeatedInt32": [1, 2, 3, 4]})",
                   "repeated_int32: [1, 2, 3, 4]");
  RunValidJsonTest("EnumRepeatedField", REQUIRED,
                   R"({"repeatedNestedEnum": ["FOO", "BAR", "BAZ"]})",
                   "repeated_nested_enum: [FOO, BAR, BAZ]");
  RunValidJsonTest("StringRepeatedField", REQUIRED,
                   R"({"repeatedString": ["Hello", "world"]})",
                   R"(repeated_string: ["Hello", "world"])");
  RunValidJsonTest("BytesRepeatedField", REQUIRED,
                   R"({"repeatedBytes": ["AAEC", "AQI="]})",
                   R"(repeated_bytes: ["\x00\x01\x02", "\x01\x02"])");
  RunValidJsonTest("MessageRepeatedField", REQUIRED,
                   R"({"repeatedNestedMessage": [{"a": 1234}, {"a": 5678}]})",
                   "repeated_nested_message: {a: 1234}"
                   "repeated_nested_message: {a: 5678}");

  // Repeated field elements are of incorrect type.
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingIntegersGotBool", REQUIRED,
      R"({"repeatedInt32": [1, false, 3, 4]})");
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingIntegersGotString", REQUIRED,
      R"({"repeatedInt32": [1, 2, "name", 4]})");
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingIntegersGotMessage", REQUIRED,
      R"({"repeatedInt32": [1, 2, 3, {"a": 4}]})");
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingStringsGotInt", REQUIRED,
      R"({"repeatedString": ["1", 2, "3", "4"]})");
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingStringsGotBool", REQUIRED,
      R"({"repeatedString": ["1", "2", false, "4"]})");
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingStringsGotMessage", REQUIRED,
      R"({"repeatedString": ["1", 2, "3", {"a": 4}]})");
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingMessagesGotInt", REQUIRED,
      R"({"repeatedNestedMessage": [{"a": 1}, 2]})");
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingMessagesGotBool", REQUIRED,
      R"({"repeatedNestedMessage": [{"a": 1}, false]})");
  ExpectParseFailureForJson(
      "RepeatedFieldWrongElementTypeExpectingMessagesGotString", REQUIRED,
      R"({"repeatedNestedMessage": [{"a": 1}, "2"]})");
  // Trailing comma in the repeated field is not allowed.
  ExpectParseFailureForJson("RepeatedFieldTrailingComma", RECOMMENDED,
                            R"({"repeatedInt32": [1, 2, 3, 4,]})");
  ExpectParseFailureForJson("RepeatedFieldTrailingCommaWithSpace", RECOMMENDED,
                            "{\"repeatedInt32\": [1, 2, 3, 4 ,]}");
  ExpectParseFailureForJson("RepeatedFieldTrailingCommaWithSpaceCommaSpace",
                            RECOMMENDED,
                            "{\"repeatedInt32\": [1, 2, 3, 4 , ]}");
  ExpectParseFailureForJson(
      "RepeatedFieldTrailingCommaWithNewlines", RECOMMENDED,
      "{\"repeatedInt32\": [\n  1,\n  2,\n  3,\n  4,\n]}");
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForNullTypes() {
  // "null" is accepted for all fields types.
  RunValidJsonTest("AllFieldAcceptNull", REQUIRED,
                   R"({
        "optionalInt32": null,
        "optionalInt64": null,
        "optionalUint32": null,
        "optionalUint64": null,
        "optionalSint32": null,
        "optionalSint64": null,
        "optionalFixed32": null,
        "optionalFixed64": null,
        "optionalSfixed32": null,
        "optionalSfixed64": null,
        "optionalFloat": null,
        "optionalDouble": null,
        "optionalBool": null,
        "optionalString": null,
        "optionalBytes": null,
        "optionalNestedEnum": null,
        "optionalNestedMessage": null,
        "repeatedInt32": null,
        "repeatedInt64": null,
        "repeatedUint32": null,
        "repeatedUint64": null,
        "repeatedSint32": null,
        "repeatedSint64": null,
        "repeatedFixed32": null,
        "repeatedFixed64": null,
        "repeatedSfixed32": null,
        "repeatedSfixed64": null,
        "repeatedFloat": null,
        "repeatedDouble": null,
        "repeatedBool": null,
        "repeatedString": null,
        "repeatedBytes": null,
        "repeatedNestedEnum": null,
        "repeatedNestedMessage": null,
        "mapInt32Int32": null,
        "mapBoolBool": null,
        "mapStringNestedMessage": null
      })",
                   "");

  // Repeated field elements cannot be null.
  ExpectParseFailureForJson("RepeatedFieldPrimitiveElementIsNull", RECOMMENDED,
                            R"({"repeatedInt32": [1, null, 2]})");
  ExpectParseFailureForJson(
      "RepeatedFieldMessageElementIsNull", RECOMMENDED,
      R"({"repeatedNestedMessage": [{"a":1}, null, {"a":2}]})");
  // Map field keys cannot be null.
  ExpectParseFailureForJson("MapFieldKeyIsNull", RECOMMENDED,
                            R"({"mapInt32Int32": {null: 1}})");
  // Map field values cannot be null.
  ExpectParseFailureForJson("MapFieldValueIsNull", RECOMMENDED,
                            R"({"mapInt32Int32": {"0": null}})");
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForWrapperTypes() {
  RunValidJsonTest("OptionalBoolWrapper", REQUIRED,
                   R"({"optionalBoolWrapper": false})",
                   "optional_bool_wrapper: {value: false}");
  RunValidJsonTest("OptionalInt32Wrapper", REQUIRED,
                   R"({"optionalInt32Wrapper": 0})",
                   "optional_int32_wrapper: {value: 0}");
  RunValidJsonTest("OptionalUint32Wrapper", REQUIRED,
                   R"({"optionalUint32Wrapper": 0})",
                   "optional_uint32_wrapper: {value: 0}");
  RunValidJsonTest("OptionalInt64Wrapper", REQUIRED,
                   R"({"optionalInt64Wrapper": 0})",
                   "optional_int64_wrapper: {value: 0}");
  RunValidJsonTest("OptionalUint64Wrapper", REQUIRED,
                   R"({"optionalUint64Wrapper": 0})",
                   "optional_uint64_wrapper: {value: 0}");
  RunValidJsonTest("OptionalFloatWrapper", REQUIRED,
                   R"({"optionalFloatWrapper": 0})",
                   "optional_float_wrapper: {value: 0}");
  RunValidJsonTest("OptionalDoubleWrapper", REQUIRED,
                   R"({"optionalDoubleWrapper": 0})",
                   "optional_double_wrapper: {value: 0}");
  RunValidJsonTest("OptionalStringWrapper", REQUIRED,
                   R"({"optionalStringWrapper": ""})",
                   R"(optional_string_wrapper: {value: ""})");
  RunValidJsonTest("OptionalBytesWrapper", REQUIRED,
                   R"({"optionalBytesWrapper": ""})",
                   R"(optional_bytes_wrapper: {value: ""})");
  RunValidJsonTest("OptionalWrapperTypesWithNonDefaultValue", REQUIRED,
                   R"({
        "optionalBoolWrapper": true,
        "optionalInt32Wrapper": 1,
        "optionalUint32Wrapper": 1,
        "optionalInt64Wrapper": "1",
        "optionalUint64Wrapper": "1",
        "optionalFloatWrapper": 1,
        "optionalDoubleWrapper": 1,
        "optionalStringWrapper": "1",
        "optionalBytesWrapper": "AQI="
      })",
                   R"(
        optional_bool_wrapper: {value: true}
        optional_int32_wrapper: {value: 1}
        optional_uint32_wrapper: {value: 1}
        optional_int64_wrapper: {value: 1}
        optional_uint64_wrapper: {value: 1}
        optional_float_wrapper: {value: 1}
        optional_double_wrapper: {value: 1}
        optional_string_wrapper: {value: "1"}
        optional_bytes_wrapper: {value: "\x01\x02"}
      )");
  RunValidJsonTest("RepeatedBoolWrapper", REQUIRED,
                   R"({"repeatedBoolWrapper": [true, false]})",
                   "repeated_bool_wrapper: {value: true}"
                   "repeated_bool_wrapper: {value: false}");
  RunValidJsonTest("RepeatedInt32Wrapper", REQUIRED,
                   R"({"repeatedInt32Wrapper": [0, 1]})",
                   "repeated_int32_wrapper: {value: 0}"
                   "repeated_int32_wrapper: {value: 1}");
  RunValidJsonTest("RepeatedUint32Wrapper", REQUIRED,
                   R"({"repeatedUint32Wrapper": [0, 1]})",
                   "repeated_uint32_wrapper: {value: 0}"
                   "repeated_uint32_wrapper: {value: 1}");
  RunValidJsonTest("RepeatedInt64Wrapper", REQUIRED,
                   R"({"repeatedInt64Wrapper": [0, 1]})",
                   "repeated_int64_wrapper: {value: 0}"
                   "repeated_int64_wrapper: {value: 1}");
  RunValidJsonTest("RepeatedUint64Wrapper", REQUIRED,
                   R"({"repeatedUint64Wrapper": [0, 1]})",
                   "repeated_uint64_wrapper: {value: 0}"
                   "repeated_uint64_wrapper: {value: 1}");
  RunValidJsonTest("RepeatedFloatWrapper", REQUIRED,
                   R"({"repeatedFloatWrapper": [0, 1]})",
                   "repeated_float_wrapper: {value: 0}"
                   "repeated_float_wrapper: {value: 1}");
  RunValidJsonTest("RepeatedDoubleWrapper", REQUIRED,
                   R"({"repeatedDoubleWrapper": [0, 1]})",
                   "repeated_double_wrapper: {value: 0}"
                   "repeated_double_wrapper: {value: 1}");
  RunValidJsonTest("RepeatedStringWrapper", REQUIRED,
                   R"({"repeatedStringWrapper": ["", "AQI="]})",
                   R"(
        repeated_string_wrapper: {value: ""}
        repeated_string_wrapper: {value: "AQI="}
      )");
  RunValidJsonTest("RepeatedBytesWrapper", REQUIRED,
                   R"({"repeatedBytesWrapper": ["", "AQI="]})",
                   R"(
        repeated_bytes_wrapper: {value: ""}
        repeated_bytes_wrapper: {value: "\x01\x02"}
      )");
  RunValidJsonTest("WrapperTypesWithNullValue", REQUIRED,
                   R"({
        "optionalBoolWrapper": null,
        "optionalInt32Wrapper": null,
        "optionalUint32Wrapper": null,
        "optionalInt64Wrapper": null,
        "optionalUint64Wrapper": null,
        "optionalFloatWrapper": null,
        "optionalDoubleWrapper": null,
        "optionalStringWrapper": null,
        "optionalBytesWrapper": null,
        "repeatedBoolWrapper": null,
        "repeatedInt32Wrapper": null,
        "repeatedUint32Wrapper": null,
        "repeatedInt64Wrapper": null,
        "repeatedUint64Wrapper": null,
        "repeatedFloatWrapper": null,
        "repeatedDoubleWrapper": null,
        "repeatedStringWrapper": null,
        "repeatedBytesWrapper": null
      })",
                   "");

  // Duration
  RunValidJsonTest(
      "DurationMinValue", REQUIRED,
      R"({"optionalDuration": "-315576000000.999999999s"})",
      "optional_duration: {seconds: -315576000000 nanos: -999999999}");
  RunValidJsonTest(
      "DurationMaxValue", REQUIRED,
      R"({"optionalDuration": "315576000000.999999999s"})",
      "optional_duration: {seconds: 315576000000 nanos: 999999999}");
  RunValidJsonTest("DurationRepeatedValue", REQUIRED,
                   R"({"repeatedDuration": ["1.5s", "-1.5s"]})",
                   "repeated_duration: {seconds: 1 nanos: 500000000}"
                   "repeated_duration: {seconds: -1 nanos: -500000000}");
  RunValidJsonTest("DurationNull", REQUIRED, R"({"optionalDuration": null})",
                   "");
  RunValidJsonTest("DurationNegativeSeconds", REQUIRED,
                   R"({"optionalDuration": "-5s"})",
                   "optional_duration: {seconds: -5 nanos: 0}");
  RunValidJsonTest("DurationNegativeNanos", REQUIRED,
                   R"({"optionalDuration": "-0.5s"})",
                   "optional_duration: {seconds: 0 nanos: -500000000}");

  ExpectParseFailureForJson("DurationMissingS", REQUIRED,
                            R"({"optionalDuration": "1"})");
  ExpectParseFailureForJson(
      "DurationJsonInputTooSmall", REQUIRED,
      R"({"optionalDuration": "-315576000001.000000000s"})");
  ExpectParseFailureForJson(
      "DurationJsonInputTooLarge", REQUIRED,
      R"({"optionalDuration": "315576000001.000000000s"})");
  ExpectSerializeFailureForJson(
      "DurationProtoInputTooSmall", REQUIRED,
      "optional_duration: {seconds: -315576000001 nanos: 0}");
  ExpectSerializeFailureForJson(
      "DurationProtoInputTooLarge", REQUIRED,
      "optional_duration: {seconds: 315576000001 nanos: 0}");

  RunValidJsonTestWithValidator(
      "DurationHasZeroFractionalDigit", RECOMMENDED,
      R"({"optionalDuration": "1.000000000s"})",
      [](const Json::Value& value) {
        return value["optionalDuration"].asString() == "1s";
      },
      true);
  RunValidJsonTestWithValidator(
      "DurationHas3FractionalDigits", RECOMMENDED,
      R"({"optionalDuration": "1.010000000s"})",
      [](const Json::Value& value) {
        return value["optionalDuration"].asString() == "1.010s";
      },
      true);
  RunValidJsonTestWithValidator(
      "DurationHas6FractionalDigits", RECOMMENDED,
      R"({"optionalDuration": "1.000010000s"})",
      [](const Json::Value& value) {
        return value["optionalDuration"].asString() == "1.000010s";
      },
      true);
  RunValidJsonTestWithValidator(
      "DurationHas9FractionalDigits", RECOMMENDED,
      R"({"optionalDuration": "1.000000010s"})",
      [](const Json::Value& value) {
        return value["optionalDuration"].asString() == "1.000000010s";
      },
      true);

  // Timestamp
  RunValidJsonTest("TimestampMinValue", REQUIRED,
                   R"({"optionalTimestamp": "0001-01-01T00:00:00Z"})",
                   "optional_timestamp: {seconds: -62135596800}");
  RunValidJsonTest(
      "TimestampMaxValue", REQUIRED,
      R"({"optionalTimestamp": "9999-12-31T23:59:59.999999999Z"})",
      "optional_timestamp: {seconds: 253402300799 nanos: 999999999}");
  RunValidJsonTest(
      "TimestampRepeatedValue", REQUIRED,
      R"({
        "repeatedTimestamp": [
          "0001-01-01T00:00:00Z",
          "9999-12-31T23:59:59.999999999Z"
        ]
      })",
      "repeated_timestamp: {seconds: -62135596800}"
      "repeated_timestamp: {seconds: 253402300799 nanos: 999999999}");
  RunValidJsonTest("TimestampLeap", REQUIRED,
                   R"({"optionalTimestamp": "1993-02-10T00:00:00.000Z"})",
                   "optional_timestamp: {seconds: 729302400}");
  RunValidJsonTest("TimestampWithPositiveOffset", REQUIRED,
                   R"({"optionalTimestamp": "1970-01-01T08:00:01+08:00"})",
                   "optional_timestamp: {seconds: 1}");
  RunValidJsonTest("TimestampWithNegativeOffset", REQUIRED,
                   R"({"optionalTimestamp": "1969-12-31T16:00:01-08:00"})",
                   "optional_timestamp: {seconds: 1}");
  RunValidJsonTest("TimestampNull", REQUIRED, R"({"optionalTimestamp": null})",
                   "");

  ExpectParseFailureForJson("TimestampJsonInputTooSmall", REQUIRED,
                            R"({"optionalTimestamp": "0000-01-01T00:00:00Z"})");
  ExpectParseFailureForJson(
      "TimestampJsonInputTooLarge", REQUIRED,
      R"({"optionalTimestamp": "10000-01-01T00:00:00Z"})");
  ExpectParseFailureForJson("TimestampJsonInputMissingZ", REQUIRED,
                            R"({"optionalTimestamp": "0001-01-01T00:00:00"})");
  ExpectParseFailureForJson("TimestampJsonInputMissingT", REQUIRED,
                            R"({"optionalTimestamp": "0001-01-01 00:00:00Z"})");
  ExpectParseFailureForJson("TimestampJsonInputLowercaseZ", REQUIRED,
                            R"({"optionalTimestamp": "0001-01-01T00:00:00z"})");
  ExpectParseFailureForJson("TimestampJsonInputLowercaseT", REQUIRED,
                            R"({"optionalTimestamp": "0001-01-01t00:00:00Z"})");
  ExpectSerializeFailureForJson("TimestampProtoInputTooSmall", REQUIRED,
                                "optional_timestamp: {seconds: -62135596801}");
  ExpectSerializeFailureForJson("TimestampProtoInputTooLarge", REQUIRED,
                                "optional_timestamp: {seconds: 253402300800}");
  RunValidJsonTestWithValidator(
      "TimestampZeroNormalized", RECOMMENDED,
      R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
      [](const Json::Value& value) {
        return value["optionalTimestamp"].asString() == "1970-01-01T00:00:00Z";
      },
      true);
  RunValidJsonTestWithValidator(
      "TimestampHasZeroFractionalDigit", RECOMMENDED,
      R"({"optionalTimestamp": "1970-01-01T00:00:00.000000000Z"})",
      [](const Json::Value& value) {
        return value["optionalTimestamp"].asString() == "1970-01-01T00:00:00Z";
      },
      true);
  RunValidJsonTestWithValidator(
      "TimestampHas3FractionalDigits", RECOMMENDED,
      R"({"optionalTimestamp": "1970-01-01T00:00:00.010000000Z"})",
      [](const Json::Value& value) {
        return value["optionalTimestamp"].asString() ==
               "1970-01-01T00:00:00.010Z";
      },
      true);
  RunValidJsonTestWithValidator(
      "TimestampHas6FractionalDigits", RECOMMENDED,
      R"({"optionalTimestamp": "1970-01-01T00:00:00.000010000Z"})",
      [](const Json::Value& value) {
        return value["optionalTimestamp"].asString() ==
               "1970-01-01T00:00:00.000010Z";
      },
      true);
  RunValidJsonTestWithValidator(
      "TimestampHas9FractionalDigits", RECOMMENDED,
      R"({"optionalTimestamp": "1970-01-01T00:00:00.000000010Z"})",
      [](const Json::Value& value) {
        return value["optionalTimestamp"].asString() ==
               "1970-01-01T00:00:00.000000010Z";
      },
      true);
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldMask() {
  RunValidJsonTest("FieldMask", REQUIRED,
                   R"({"optionalFieldMask": "foo,barBaz"})",
                   R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})");
  RunValidJsonTest("EmptyFieldMask", REQUIRED, R"({"optionalFieldMask": ""})",
                   R"(optional_field_mask: {})");
  ExpectParseFailureForJson("FieldMaskInvalidCharacter", RECOMMENDED,
                            R"({"optionalFieldMask": "foo,bar_bar"})");
  ExpectSerializeFailureForJson("FieldMaskPathsDontRoundTrip", RECOMMENDED,
                                R"(optional_field_mask: {paths: "fooBar"})");
  ExpectSerializeFailureForJson("FieldMaskNumbersDontRoundTrip", RECOMMENDED,
                                R"(optional_field_mask: {paths: "foo_3_bar"})");
  ExpectSerializeFailureForJson("FieldMaskTooManyUnderscore", RECOMMENDED,
                                R"(optional_field_mask: {paths: "foo__bar"})");
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForStruct() {
  RunValidJsonTest("Struct", REQUIRED,
                   R"({
        "optionalStruct": {
          "nullValue": null,
          "intValue": 1234,
          "boolValue": true,
          "doubleValue": 1234.5678,
          "stringValue": "Hello world!",
          "listValue": [1234, "5678"],
          "objectValue": {
            "value": 0
          }
        }
      })",
                   R"(
        optional_struct: {
          fields: {
            key: "nullValue"
            value: {null_value: NULL_VALUE}
          }
          fields: {
            key: "intValue"
            value: {number_value: 1234}
          }
          fields: {
            key: "boolValue"
            value: {bool_value: true}
          }
          fields: {
            key: "doubleValue"
            value: {number_value: 1234.5678}
          }
          fields: {
            key: "stringValue"
            value: {string_value: "Hello world!"}
          }
          fields: {
            key: "listValue"
            value: {
              list_value: {
                values: {
                  number_value: 1234
                }
                values: {
                  string_value: "5678"
                }
              }
            }
          }
          fields: {
            key: "objectValue"
            value: {
              struct_value: {
                fields: {
                  key: "value"
                  value: {
                    number_value: 0
                  }
                }
              }
            }
          }
        }
      )");
  RunValidJsonTest("StructWithEmptyListValue", REQUIRED,
                   R"({
        "optionalStruct": {
          "listValue": []
        }
      })",
                   R"(
        optional_struct: {
          fields: {
            key: "listValue"
            value: {
              list_value: {
              }
            }
          }
        }
      )");
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForValue() {
  RunValidJsonTest("ValueAcceptInteger", REQUIRED, R"({"optionalValue": 1})",
                   "optional_value: { number_value: 1}");
  RunValidJsonTest("ValueAcceptFloat", REQUIRED, R"({"optionalValue": 1.5})",
                   "optional_value: { number_value: 1.5}");
  RunValidJsonTest("ValueAcceptBool", REQUIRED, R"({"optionalValue": false})",
                   "optional_value: { bool_value: false}");
  RunValidJsonTest("ValueAcceptNull", REQUIRED, R"({"optionalValue": null})",
                   "optional_value: { null_value: NULL_VALUE}");
  RunValidJsonTest("ValueAcceptString", REQUIRED,
                   R"({"optionalValue": "hello"})",
                   R"(optional_value: { string_value: "hello"})");
  RunValidJsonTest("ValueAcceptList", REQUIRED,
                   R"({"optionalValue": [0, "hello"]})",
                   R"(
        optional_value: {
          list_value: {
            values: {
              number_value: 0
            }
            values: {
              string_value: "hello"
            }
          }
        }
      )");
  RunValidJsonTest("ValueAcceptObject", REQUIRED,
                   R"({"optionalValue": {"value": 1}})",
                   R"(
        optional_value: {
          struct_value: {
            fields: {
              key: "value"
              value: {
                number_value: 1
              }
            }
          }
        }
      )");
  RunValidJsonTest("RepeatedValue", REQUIRED,
                   R"({
        "repeatedValue": [["a"]]
      })",
                   R"(
        repeated_value: [
          {
            list_value: {
              values: [
                { string_value: "a"}
              ]
            }
          }
        ]
      )");
  RunValidJsonTest("RepeatedListValue", REQUIRED,
                   R"({
        "repeatedListValue": [["a"]]
      })",
                   R"(
        repeated_list_value: [
          {
            values: [
              { string_value: "a"}
            ]
          }
        ]
      )");
  RunValidJsonTestWithValidator(
      "NullValueInOtherOneofOldFormat", RECOMMENDED,
      R"({"oneofNullValue": "NULL_VALUE"})",
      [](const Json::Value& value) {
        return (value.isMember("oneofNullValue") &&
                value["oneofNullValue"].isNull());
      },
      true);
  RunValidJsonTestWithValidator(
      "NullValueInOtherOneofNewFormat", RECOMMENDED,
      R"({"oneofNullValue": null})",
      [](const Json::Value& value) {
        return (value.isMember("oneofNullValue") &&
                value["oneofNullValue"].isNull());
      },
      true);
  RunValidJsonTestWithValidator(
      "NullValueInNormalMessage", RECOMMENDED,
      R"({"optionalNullValue": null})",
      [](const Json::Value& value) {
        return value.empty();
      },
      true);
  ExpectSerializeFailureForJson("ValueRejectNanNumberValue", RECOMMENDED,
                                "optional_value: { number_value: nan}");
  ExpectSerializeFailureForJson("ValueRejectInfNumberValue", RECOMMENDED,
                                "optional_value: { number_value: inf}");
}

void BinaryAndJsonConformanceSuite::RunJsonTestsForAny() {
  RunValidJsonTest("Any", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3",
          "optionalInt32": 12345
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
            optional_int32: 12345
          }
        }
      )");
  RunValidJsonTest("AnyNested", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/google.protobuf.Any",
          "value": {
            "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3",
            "optionalInt32": 12345
          }
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/google.protobuf.Any] {
            [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
              optional_int32: 12345
            }
          }
        }
      )");
  // The special "@type" tag is not required to appear first.
  RunValidJsonTest("AnyUnorderedTypeTag", REQUIRED,
                   R"({
        "optionalAny": {
          "optionalInt32": 12345,
          "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3"
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
            optional_int32: 12345
          }
        }
      )");
  // Well-known types in Any.
  RunValidJsonTest("AnyWithInt32ValueWrapper", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/google.protobuf.Int32Value",
          "value": 12345
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/google.protobuf.Int32Value] {
            value: 12345
          }
        }
      )");
  RunValidJsonTest("AnyWithDuration", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/google.protobuf.Duration",
          "value": "1.5s"
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/google.protobuf.Duration] {
            seconds: 1
            nanos: 500000000
          }
        }
      )");
  RunValidJsonTest("AnyWithTimestamp", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/google.protobuf.Timestamp",
          "value": "1970-01-01T00:00:00Z"
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/google.protobuf.Timestamp] {
            seconds: 0
            nanos: 0
          }
        }
      )");
  RunValidJsonTest("AnyWithFieldMask", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/google.protobuf.FieldMask",
          "value": "foo,barBaz"
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/google.protobuf.FieldMask] {
            paths: ["foo", "bar_baz"]
          }
        }
      )");
  RunValidJsonTest("AnyWithStruct", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/google.protobuf.Struct",
          "value": {
            "foo": 1
          }
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/google.protobuf.Struct] {
            fields: {
              key: "foo"
              value: {
                number_value: 1
              }
            }
          }
        }
      )");
  RunValidJsonTest("AnyWithValueForJsonObject", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/google.protobuf.Value",
          "value": {
            "foo": 1
          }
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/google.protobuf.Value] {
            struct_value: {
              fields: {
                key: "foo"
                value: {
                  number_value: 1
                }
              }
            }
          }
        }
      )");
  RunValidJsonTest("AnyWithValueForInteger", REQUIRED,
                   R"({
        "optionalAny": {
          "@type": "type.googleapis.com/google.protobuf.Value",
          "value": 1
        }
      })",
                   R"(
        optional_any: {
          [type.googleapis.com/google.protobuf.Value] {
            number_value: 1
          }
        }
      )");
}

}  // namespace protobuf