This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 647
camelCase all the JSONs #746
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0bec15a
Add camelCase utility
SteveSandersonMS aaa3f00
Use camelCase when JSON-serializing (but not for dictionary keys)
SteveSandersonMS 7a2098a
Make JSON deserialization treat member names case-insensitively (but …
SteveSandersonMS 56f5c6d
Use camelCase in JSON in the samples and templates
SteveSandersonMS a299748
Reverse the order of the params for the camelcase test because it's w…
SteveSandersonMS bfce965
CR feedback
SteveSandersonMS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
[ | ||
{ | ||
"DateFormatted": "06/05/2018", | ||
"TemperatureC": 1, | ||
"Summary": "Freezing", | ||
"TemperatureF": 33 | ||
"dateFormatted": "06/05/2018", | ||
"temperatureC": 1, | ||
"summary": "Freezing", | ||
"temperatureF": 33 | ||
}, | ||
{ | ||
"DateFormatted": "07/05/2018", | ||
"TemperatureC": 14, | ||
"Summary": "Bracing", | ||
"TemperatureF": 57 | ||
"dateFormatted": "07/05/2018", | ||
"temperatureC": 14, | ||
"summary": "Bracing", | ||
"temperatureF": 57 | ||
}, | ||
{ | ||
"DateFormatted": "08/05/2018", | ||
"TemperatureC": -13, | ||
"Summary": "Freezing", | ||
"TemperatureF": 9 | ||
"dateFormatted": "08/05/2018", | ||
"temperatureC": -13, | ||
"summary": "Freezing", | ||
"temperatureF": 9 | ||
}, | ||
{ | ||
"DateFormatted": "09/05/2018", | ||
"TemperatureC": -16, | ||
"Summary": "Balmy", | ||
"TemperatureF": 4 | ||
"dateFormatted": "09/05/2018", | ||
"temperatureC": -16, | ||
"summary": "Balmy", | ||
"temperatureF": 4 | ||
}, | ||
{ | ||
"DateFormatted": "10/05/2018", | ||
"TemperatureC": -2, | ||
"Summary": "Chilly", | ||
"TemperatureF": 29 | ||
"dateFormatted": "10/05/2018", | ||
"temperatureC": -2, | ||
"summary": "Chilly", | ||
"temperatureF": 29 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 20 additions & 20 deletions
40
...NetCore.Blazor.Templates/content/BlazorStandalone-CSharp/wwwroot/sample-data/weather.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
[ | ||
{ | ||
"Date": "2018-05-06", | ||
"TemperatureC": 1, | ||
"Summary": "Freezing", | ||
"TemperatureF": 33 | ||
"date": "2018-05-06", | ||
"temperatureC": 1, | ||
"summary": "Freezing", | ||
"temperatureF": 33 | ||
}, | ||
{ | ||
"Date": "2018-05-07", | ||
"TemperatureC": 14, | ||
"Summary": "Bracing", | ||
"TemperatureF": 57 | ||
"date": "2018-05-07", | ||
"temperatureC": 14, | ||
"summary": "Bracing", | ||
"temperatureF": 57 | ||
}, | ||
{ | ||
"Date": "2018-05-08", | ||
"TemperatureC": -13, | ||
"Summary": "Freezing", | ||
"TemperatureF": 9 | ||
"date": "2018-05-08", | ||
"temperatureC": -13, | ||
"summary": "Freezing", | ||
"temperatureF": 9 | ||
}, | ||
{ | ||
"Date": "2018-05-09", | ||
"TemperatureC": -16, | ||
"Summary": "Balmy", | ||
"TemperatureF": 4 | ||
"date": "2018-05-09", | ||
"temperatureC": -16, | ||
"summary": "Balmy", | ||
"temperatureF": 4 | ||
}, | ||
{ | ||
"Date": "2018-05-10", | ||
"TemperatureC": -2, | ||
"Summary": "Chilly", | ||
"TemperatureF": 29 | ||
"date": "2018-05-10", | ||
"temperatureC": -2, | ||
"summary": "Chilly", | ||
"temperatureF": 29 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.AspNetCore.Blazor.Json | ||
{ | ||
internal static class CamelCase | ||
{ | ||
public static string MemberNameToCamelCase(string value) | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
throw new ArgumentException( | ||
$"The value '{value ?? "null"}' is not a valid member name.", | ||
nameof(value)); | ||
} | ||
|
||
// If we don't need to modify the value, bail out without creating a char array | ||
if (!char.IsUpper(value[0])) | ||
{ | ||
return value; | ||
} | ||
|
||
// We have to modify at least one character | ||
var chars = value.ToCharArray(); | ||
|
||
var length = chars.Length; | ||
if (length < 2 || !char.IsUpper(chars[1])) | ||
{ | ||
// Only the first character needs to be modified | ||
// Note that this branch is functionally necessary, because the 'else' branch below | ||
// never looks at char[1]. It's always looking at the n+2 character. | ||
chars[0] = char.ToLowerInvariant(chars[0]); | ||
} | ||
else | ||
{ | ||
// If chars[0] and chars[1] are both upper, then we'll lowercase the first char plus | ||
// any consecutive uppercase ones, stopping if we find any char that is followed by a | ||
// non-uppercase one | ||
var i = 0; | ||
while (i < length) | ||
{ | ||
chars[i] = char.ToLowerInvariant(chars[i]); | ||
|
||
i++; | ||
|
||
// If the next-plus-one char isn't also uppercase, then we're now on the last uppercase, so stop | ||
if (i < length - 1 && !char.IsUpper(chars[i + 1])) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return new string(chars); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /cc @JamesNK |
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the result of camel casing is cached, is there much value in doing perf optimizations here? This only runs once per member per type right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the value is limited. But it wasn't difficult to implement and doesn't add a lot of complexity. The main benefit vs the Json.NET implementation is that this version only does one
IsUpper
check per character rather than two. But I agree it's a super-minor benefit.