3

I have a class like:

public class MyClass{
    public int ID {get;set;}
    public string CName{get;set}
    public string FirstName{get;set}        
}

when I using newtonsoft camelcase to convert this class to json ,I get something like this

{
  id:1,
  cname:xxx,
  fistName:xxx
}

Why not ID to iD,CName to cName? what's the exact rule of camelcase?

{
  iD:1,
  cName:xxx,
  fistName:xxx
}
Xin
  • 69
  • 4
  • I don't know whether Newtonsoft has ever documented their camel case algorithm, but the behavior you are seeing is intentional. From [Closed Issue #236: CamelCase - Breaking Change in > v.4.5.8 - Lowercases all characters if uppercase](https://github.com/JamesNK/Newtonsoft.Json/issues/236): *After version 4.5.8 a propertyname "ABC" will be serialized as "abc", whereas in earlier versions it would be serialized as "aBC".* which was closed as, *Not a bug.* – dbc May 21 '18 at 02:30

1 Answers1

4

The first word of camelcase is all lowercase. Hence, ID becomes id, and CName becomes cname. After that, each additional word has only the first letter capitalized, hence name becomes Name. That is to say that Newtonsoft treats ID and CName as single words, not multiple words.

This is the method used to convert characters to camelcase in Newtonsoft. As you can see, it contains little logic for parsing a string into individual words. The code simply assumes that the first word in uppercase ends (1) after the second letter and (2) when the code finds either a space or an uppercase letter followed by a lowercase letter.

    public static string ToCamelCase(string s)
    {
        if (string.IsNullOrEmpty(s) || !char.IsUpper(s[0]))
        {
            return s;
        }

        char[] chars = s.ToCharArray();

        for (int i = 0; i < chars.Length; i++)
        {
            if (i == 1 && !char.IsUpper(chars[i]))
            {
                break;
            }

            bool hasNext = (i + 1 < chars.Length);
            if (i > 0 && hasNext && !char.IsUpper(chars[i + 1]))
            {
                // if the next character is a space, which is not considered uppercase 
                // (otherwise we wouldn't be here...)
                // we want to ensure that the following:
                // 'FOO bar' is rewritten as 'foo bar', and not as 'foO bar'
                // The code was written in such a way that the first word in uppercase
                // ends when if finds an uppercase letter followed by a lowercase letter.
                // now a ' ' (space, (char)32) is considered not upper
                // but in that case we still want our current character to become lowercase
                if (char.IsSeparator(chars[i + 1]))
                {
                    chars[i] = ToLower(chars[i]);
                }

                break;
            }

            chars[i] = ToLower(chars[i]);
        }

        return new string(chars);
    }
Geoff Hacker
  • 141
  • 4
  • referencing to the question `what's the exact rule of camelcase` would you know how does newtonsoft determine if the word is single or compound? – jmesolomon May 21 '18 at 02:22
  • Thanks for your question, @jmesolomon. I've just edited my answer to include that information. – Geoff Hacker May 21 '18 at 03:23