Skip to main content
ThunderLang™Programming Language

Gallery · C# · Json.NET

How Json.NET camel-cases a name

Json.NET (Newtonsoft.Json) is one of the most downloaded .NET packages. When it writes "userId" for a C# property called UserId, this is the function doing it, including the rule that turns "URLValue" into "urlValue", not "uRLValue".

What to look for

The loop lowers leading capitals and stops at the first capital that is followed by a lowercase letter.

How much Thunder could read

10 of 13 steps put into words; the other 3 are shown as code.

Read by the current Thunder compiler when this page was built. Hand-checked against the code: accurate, with some wording that is thin or close to the code.

Flow: How does it go? One action or rule as a path: questions with Yes and No, repeats, the Stops, what happens if something goes wrong, and what always happens at the end.

String utils can

To camel case

From codelines 155–194
You give its
  1. `StringUtils.IsNullOrEmpty(s) || !char.IsUpper(s[0])`?

    Yes

    1. Gives back s

    No continue

  2. char[] chars = s.ToCharArray();
  3. For each: Counts i from 0 while i is less than the length of chars

    For each

    1. I is 1 and not char.IsUpper(chars[i])?

      Yes

      1. Does: Leaves the loop

      No continue

    2. bool hasNext = (i + 1 < chars.Length);
    3. I is more than 0 and has next is set and not char.IsUpper(chars[i + 1])?

      Yes

      1. Char.IsSeparator(chars[i + 1])?

        Yes

        1. Saves: Stores the result of to lower under i in chars

        No continue

      2. Does: Leaves the loop

      No continue

    4. Saves: Stores the result of to lower under i in chars

    When done

  4. Gives back a new string
The same reading as Thunder Text, built with this page
To camel case (String utils)
  You give it: s
  - Ask: `StringUtils.IsNullOrEmpty(s) || !char.IsUpper(s[0])`? (code, not explained)  (line 157)
    Yes:
      - Gives back s  (line 159)
    No: continue
  - `char[] chars = s.ToCharArray();` (code, not explained)  (line 162)
  - Counts i from 0 while i is less than the length of chars  (line 164)
    For each:
      - Ask: I is 1 and not char.IsUpper(chars[i])?  (line 166)
        Yes:
          - Leaves the loop  (line 168)
        No: continue
      - `bool hasNext = (i + 1 < chars.Length);` (code, not explained)  (line 171)
      - Ask: I is more than 0 and has next is set and not char.IsUpper(chars[i + 1])?  (line 172)
        Yes:
          - Ask: Char.IsSeparator(chars[i + 1])?  (line 182)
            Yes:
              - Stores the result of to lower under i in chars  (line 184)
            No: continue
          - Leaves the loop  (line 187)
        No: continue
      - Stores the result of to lower under i in chars  (line 190)
  - Gives back a new string  (line 193)

Source and license

From Json.NET (JamesNK/Newtonsoft.Json), Src/Newtonsoft.Json/Utilities/StringUtils.cs, lines 155–194, at commit 0a2e291 (13.0.3).

Copyright (c) 2007 James Newton-King. Licensed under the MIT. The code is shown unmodified; the Thunder reading is ThunderLang's.

Full license text (LICENSE)
The MIT License (MIT)

Copyright (c) 2007 James Newton-King

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.