ConvertToPascalCase() static private method

static private ConvertToPascalCase ( string camelCaseName ) : string
camelCaseName string
return string
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            int result = 0;

            // What's happening in this code? We express [Flags] enums in JSON as arrays of
            // strings. On deserialization, we walk the array, locate each string,
            // and convert it to its equivalent enum value. Because we don't have a strong
            // sense of the destination type, we simply treat the enum values as numbers
            // and OR them together. This number will eventually be unboxed and assigned
            // to the target enum property.

            // Read start of array
            reader.Read();

            while (reader.TokenType == JsonToken.String)
            {
                string enumName = EnumConverter.ConvertToPascalCase((string)reader.Value);
                result |= (int)Enum.Parse(objectType, enumName);
                reader.Read();
            }

            return(result);
        }
Example #2
0
        public void EnumConverter_ConvertToPascalCase()
        {
            Assert.Equal("M", EnumConverter.ConvertToPascalCase("m"));
            Assert.Equal("MD", EnumConverter.ConvertToPascalCase("md"));
            Assert.Equal("MD5", EnumConverter.ConvertToPascalCase("md5"));
            Assert.Equal("MDFoo", EnumConverter.ConvertToPascalCase("mdFoo"));
            Assert.Equal("Mfoo", EnumConverter.ConvertToPascalCase("mfoo"));

            // NOTE: our heuristics for identifying two letter terms that
            // require casing as a group are necessarily limited to our
            // specific cases. Doing a reasonable job of this requires
            // maintaining a dictionary of two letter words to distinguish
            // them from two letter acronyms (which are cased as a group).
            // Even with a dictionary, there is overlap, such as with
            // Io, a moon of jupiter, and IO.

            Assert.Equal("METoo", EnumConverter.ConvertToPascalCase("meToo"));
        }