PHP API Recommendations - Duplicate json_encoded object: good or bad?

I apologize in advance as I am having trouble phrasing my questions, but please bear with me and I will try to be as precise as possible. However, my question is somewhat vague and difficult to ask.

I recently discussed with one of my colleagues the responses received from the RESTful API via PHP. Where we differed is our response from the server.

Its argument was to just json_encode the raw object (which is a representation of a PHP object from a data string from a database table). However, when I found that the problem was related to a situation, such as a situation where you only need to send multiple columns of data.

For example, let's say you return an object where all you need is the primary key, description and name from the table. However, this object also has properties for other uses (var1, var2, etc.), if repeated like this:

echo(json_encode($object));

      

JSON will look like this:

{
    "primary_key": 4,
    "description": "hello",
    "name": "name namerton",
    "var1": null,
    "var2": null,
    "var3": null,
    "var4": null,
    "var5": null,
    "var6": null
}

      

I usually prefer to project my objects like this:

$objectArray = array(
    "primary_key" => $object->primary_key,
    "description" => $object->description,
    "name" => $object->name
);
echo(json_encode($objectArray));

      

This will convert the data to an array, returning a JSON result like this:

{
    "primary_key": 4,
    "description": "hello",
    "name": "name namerton"
}

      

My question, I suppose, is, is there any standard for json encoding and returning them from the API? Is there one bad practice or the other? Or is it a matter of personal preference?

My main concern is sending extra blank keys or even sending unexpected data from the server. That is, if you want to send the primary key, description and name, but you have two other properties, you will also send them back if you JSON encode that object. His main answer is that only properties that need to be sent back need to be public, and what if we send extra data, what? I just can't agree, but I wonder if anyone has an approach to being elected.

Thanks in advance for your opinions / responses.

+3


source to share


2 answers


Personally, I think it's okay to return the entire object, not just the fields I need. This makes the api more flexible. But in order to store the data to be transferred, you can let the api let the client define which columns he wants, his win / win for both sides, the api itself has the necessary flexibility, the client only consumes the data he really needs.



see http://www.sitepoint.com/best-practices-rest-api-scratch-implementation/ - Fields, Filters, Sorting and Search part

+2


source


I agree with you. You shouldn't directly expose your schema via a REST answer lest some bad-faith types decide to hack your setup and now get an educated guess about the schema layout, which can happen even with a valid api key.



I would only return the columns requested and those columns were changed to not display the schema.

+4


source







All Articles