private static object ParseSingleStringValue(FieldDescriptor field, string text) { switch (field.FieldType) { case FieldType.String: return(text); case FieldType.Bytes: try { return(ByteString.FromBase64(text)); } catch (FormatException e) { throw InvalidProtocolBufferException.InvalidBase64(e); } case FieldType.Int32: case FieldType.SInt32: case FieldType.SFixed32: return(ParseNumericString(text, int.Parse)); case FieldType.UInt32: case FieldType.Fixed32: return(ParseNumericString(text, uint.Parse)); case FieldType.Int64: case FieldType.SInt64: case FieldType.SFixed64: return(ParseNumericString(text, long.Parse)); case FieldType.UInt64: case FieldType.Fixed64: return(ParseNumericString(text, ulong.Parse)); case FieldType.Double: double d = ParseNumericString(text, double.Parse); ValidateInfinityAndNan(text, double.IsPositiveInfinity(d), double.IsNegativeInfinity(d), double.IsNaN(d)); return(d); case FieldType.Float: float f = ParseNumericString(text, float.Parse); ValidateInfinityAndNan(text, float.IsPositiveInfinity(f), float.IsNegativeInfinity(f), float.IsNaN(f)); return(f); case FieldType.Enum: var enumValue = field.EnumType.FindValueByName(text); if (enumValue == null) { throw new InvalidProtocolBufferException($"Invalid enum value: {text} for enum type: {field.EnumType.FullName}"); } // Just return it as an int, and let the CLR convert it. return(enumValue.Number); default: throw new InvalidProtocolBufferException($"Unsupported conversion from JSON string for field type {field.FieldType}"); } }