-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Renaming an object key #979
Comments
Hi Yann, I'm not sure that's the right solution to your problem. Regards, |
Hey Benoît, Sorry for the response delay. Currently, I have it implemented like this: // callback function
bool callbackFunc(JsonVariantConst request, JsonVariant response) {
// on success
response["result"] = // something based on request["params"]
// on error
response["error"] = // (parts of) error object as of Json-RPC specification
}
// register the function to call when a Json-RPC request
// with method "methodname" comes in
server.registerMethod("methodname",callbackFunc)
// process an incoming Json-RPC message
// 1. check the "method"
// 2. call the appropriate registered callback and let it write to response
// 3. complete the response with additional data (e.g. "id", "error", etc...)
server.process(JsonVariantConst request, JsonVariant response) So the callback function gets to see the full request AND needs to fiddle with the full response. I would rather like to implement it like this: bool callbackFunc(JsonVariantConst params, JsonVariant resultOrError) {
// on success
resultOrError ... // set to something based on params
return true;
// on error
resultOrError ... // set to (parts of) error object as of Json-RPC specification
return false;
} Based on the return value, the However, it works the way it is now. It really is not a pressing matter right now. I just thought that being able to (memory-)efficiently rename object keys would make some things easier. Cheers, Yann |
Currently, it is not possible to rename an object key efficiently. However, renaming an object key can be handy at times.
For example, I am currently implementing a Json-RPC library. The library also provides a server class which processes Json-RPC requests (a
JsonVariant
) and calls appropriate callback methods which in turn fill a Json-RPC response (also aJsonVariant
). Depending on the success of the callback, either the"result"
member or the"error"
member must be filled according to the specification. If renaming object keys would be possible with ArduinoJson, the callbacks could just care about filling aJsonVariant
with a result or an error object and the server would take care of choosing the right key name depending on the callback's success. Currently, the callbacks need to be aware of the Json-RPC specification which I consider a rather clumsy style.This is just my use case of renaming object keys but I am sure other people would also benefit from this feature.
BTW Thank you so much for this awesome library! :-)
Yann
The text was updated successfully, but these errors were encountered: