Added ability to rename key-values.#21
Closed
alexevladgabriel wants to merge 4 commits intoclugg:masterfrom
Closed
Added ability to rename key-values.#21alexevladgabriel wants to merge 4 commits intoclugg:masterfrom
alexevladgabriel wants to merge 4 commits intoclugg:masterfrom
Conversation
Contributor
|
You could do it like this:
stock bool json_rename(JSON_Object obj, const char[] key, const char[] toKey) {
JSONCellType type = obj.GetKeyType( key );
if ( type == JSON_Type_Invalid ) return false;
if ( StrEqual( key, toKey, true ) ) return false;
switch ( type ) {
case JSON_Type_String: {
int length = obj.GetKeyLength( key ) + 1;
char[] value = new char[length];
obj.GetString( key, value, length );
obj.SetString( toKey, value );
}
case JSON_Type_Int: obj.SetInt( toKey, obj.GetInt( key ) );
case JSON_Type_Float: obj.SetFloat( toKey, obj.GetFloat( key ) );
case JSON_Type_Bool: obj.SetBool( toKey, obj.GetBool( key ) );
case JSON_Type_Object: obj.SetObject( toKey, obj.GetObject( key ) );
}
obj.Remove( key );
return true;
}And since |
Contributor
|
Oh, you can also move switch ( obj.GetKeyType( key ) ) {
case JSON_Type_Invalid: return false;
case JSON_Type_String: {
int length = obj.GetKeyLength( key ) + 1;
char[] value = new char[length];
obj.GetString( key, value, length );
obj.SetString( toKey, value );
}
case JSON_Type_Int: obj.SetInt( toKey, obj.GetInt( key ) );
case JSON_Type_Float: obj.SetFloat( toKey, obj.GetFloat( key ) );
case JSON_Type_Bool: obj.SetBool( toKey, obj.GetBool( key ) );
case JSON_Type_Object: obj.SetObject( toKey, obj.GetObject( key ) );
} |
Owner
|
Thanks very much @alexevladgabriel for the suggestion/implementation and @MAGNAT2645 for reviewing it! I'm in the midst of working on some underlying changes (a new major release) so I am going to hold off on merging this for the time being, but I will update you here once it's released. |
Owner
|
This has been released under v3.3.0. Thanks for your contribution! :) |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Added a new function to rename key-values of JSON Objects. I had needed that feature in one plugin rewrite.
If you have any ideas for modification and making test cases.