I have a horrible DB structure with no naming convention and table and field names are all over the place. I need to present some of this data to a freshly developed mobile app, and I would like to at least present a "clean" design to the mobile app regarding the table and field names.
To this end, I am writing code that will create "nice" json objects for the mobile app to consume. I don't want to manually go and make the "right" name for each field in my db, I would like an automated process to do this.
So the rule I'm trying to apply is: All table and field names should be in lowerCamelCase.
e.g.
Apple_Cart => appleCart
AppleCart => appleCart
Apple__Cart => appleCart
appleCart => appleCart
APPLE_CART => appleCart
Apple_Cart => appleCart
APPLECart => appleCart (not sure about this one)
I dont want to reinvent the wheel here, and I'm sure that either Newtonsoft.Json or another library already has code to do this? Please can someone show me a good way to go about converting field and table name strings to json standard format.
This is how far I got before deciding that I'm about to reinvent the wheel..
/// <summary>
/// Makes the json identifier.
/// </summary>
/// <param name="fieldName">Name of the field.</param>
/// <returns></returns>
private string MakeJsonIdentifier(string fieldName)
{
StringBuilder sb = new StringBuilder(fieldName);
while (sb.ToString().Contains("_"))
{
int posOfFirstUs = sb.ToString().IndexOf("_", StringComparison.Ordinal);
if (sb.ToString().Length > posOfFirstUs + 1)
{
sb[posOfFirstUs + 1] = char.ToUpperInvariant(sb.ToString()[posOfFirstUs + 1]);
sb.Remove(posOfFirstUs, 1);
}
}
return sb.ToString();
}