Jump to content

Processing data and identifying whether valid


Go to solution Solved by requinix,

Recommended Posts

I am receiving JSON-RPC method requests, processing it and updating the server state if valid, and either returning a JSON-RPC error response or the results. 




//$jsonRpc is provided by another machine
//Returns ['success'=>true/false, 'rs'=>{error or result}]
$output=processResults($jsonRpc);
$response=($output['success']==1)?
makeResultReponse($output['rs'])
:makeErrorReponse($output['rs']);



Any recommendations on how best to tag the data as valid or not?  For instance:

 

Option 1. Have processResults return ['success'=>true/false, 'rs'=>{error or result}]

 


Option 2. Have processResults return ['result'=>{result}] or  ['error'=>{error}]

 


Option 3.  Have processResults return [true/false, {error or result}]

 

Option 4.  Have processResults return an object which is an instance of the success or error class

 

Option 5.  Uses exceptions to indicate that the received data was invalid.

 

Option 6. Something else?

"result", "success", and "error" are all commonly used (not all at the same time, of course). However if you're following JSON-RPC then you should do what they say:

result

This member is REQUIRED on success.

This member MUST NOT exist if there was an error invoking the method.

The value of this member is determined by the method invoked on the Server.

 

error

This member is REQUIRED on error.

This member MUST NOT exist if there was no error triggered during invocation.

The value for this member MUST be an Object as defined in section 5.1.

"result", "success", and "error" are all commonly used (not all at the same time, of course). However if you're following JSON-RPC then you should do what they say:

 

 

After reading your reply, I re-read my original post, and realize I was not clear.

 

I have implemented per the 2.0 spec for protocol between different machines.  Eventually, I get to a single machine which has multiple classes which perform various functions, and I am inquiring only for this scope where I am in control of end-to-end.  Often a method will return true/false or a value or an object, etc, and and I will implement as applicable.  However, the primary function of the application is dealing with this inter-machine communication and as such, I find myself often dealing with similar data as JSON-RPC.  Seems kind of silly to include a jsonrpc:2.0 property in all my internal handshakes, and I feel arrays can cut down on clutter when needing to create structure which later gets turned into JSON and thus object notation.  But then again, constancy is rather nice, so maybe in the long run it will be of benefit.

 

Or maybe I was clear.  Was your understanding that I was referring only to communication directly within a single application?

  • Solution

So this is for a different part of the application than the RPC messages?

 

Then I'd still follow what the spec says (regarding this particular bit) simply for the consistency - every part of the system uses the same result/error structure so there's no confusion.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.