What does it mean to serialize data or an object? - php

using php, if possible.
What does it mean? I was reading one of my old questions ( How do you pass values between PHP pages for MVC?)
and in one of the answers it says:
The part responsible for transferring the data between the controller and the view is the View engine (or class) internal to CodeIgniter. It takes that array from the controller and deserializes it for the view.
I don't know what that means (I read the comments). I put CodeIgniter as the example and tag, but I guess it could be a general question.
Thanks.

To serialize data is to generate a storable representation of a value as a string, for example:
json_encode is a type of serialization but PHP has native support also of serialize that can serialize almost any type of data except resources types, you can find a small guide to serialization here:
http://www.devshed.com/c/a/PHP/The-Basics-of-Serializing-Objects-in-PHP/
For the full manual that corresponds to PHP you can find it in the link that Cédric Belin posted in the post below :D

Serializing usually means converting object (or complex object structure) into text/binary form, suitable for storing or transmitting over network.
Deserialization is a reverse process.

See this link
Serialization is the process of converting an object or an object graph in linear sequence of bytes for storage or transmission to another location.

If you want to store your data in memory or transmit over computer network and reconstruct later in different environment, first you need to convert this data into understandable/usable format by computers which is binary format (00011010101011...).
SERIALization is a convertion of data structure into SERIES of bits.

Related

encrypted object in mysql [duplicate]

The goal is to unserialize a PHP serialized string and get sutable object in C#
Is there any way to make this possible in C#(.Net)?
To be more specific:
We need to make an application which comunicates (Via HTTP) to specific website which returns the needed information. Fortunately/unfortunately we dont have permission to website so the data (array mostly) that is returned from website is PHP serialized.
I suppose using JSON as an intermediary step could be useful.
You should probably write it to XML or JSON. You can construct your C# object back from the XML
Edit: Looks like there is already a XML serializer for PHP

Rails multi-array format in mysql

Have an old project that someone did in PHP and am attempting to convert it over to Rails. Am encountering an issue when trying to iterate through a multiple array in rails.
The array as stored in the DB looks like this:
a:5:{i:0;s:8:"Director";i:1;s:11:"Shareholder";i:2;s:14:"Vice President";i:3;s:9:"Secretary";i:4;s:9:"Treasurer";}
Am trying to display the String values such as "Director" and "Shareholder".
Does the DB field need to be changed to a different format to work?
How would this be done in rails?
Thank you in advance
That looks like the output of PHP's serialize function to me.
If you have PHP around or don't mind installing it to help with the data migration, then I'd write a short PHP script to convert those columns to JSON using PHP's unserialize and json_encode functions: read a value, unserialize it, json_encode the result, write it back into the database (all in PHP).
After that, you should be able to use ActiveRecord's serialize with JSON as the storage format:
class Model < ActiveRecord::Base
serialize :whatever_it_is_called, JSON
end
You could also use YAML (the default for ActiveRecord's serialize) if you had YAML support in PHP-land.
If you don't want to touch PHP at all, then you'll have to write Ruby parser for PHP's serialize format (which seems to be well documented in the PHP docs) and hook that up to ActiveRecord's serialize.
You will of course be working with a copy of the real database for all this work.

string / array format convert with php

i'm manually reading out database entries from a plugin (eventscalendar wordpress). there are user defined fields / custom fields stored in the database. excuse my ignorance - but i'm having trouble associating the format the data is stored in:
a:1:{s:3:"key";s:6:"extern";}
so in the end all i would need is the value of s:6:"" in a simple variable.
lets say $key = "extern"
any quick solutions?
thanks,
mark
Take a look at unserialize to get it back into an array: http://php.net/manual/en/function.unserialize.php
That is a serialized array-- note the a at the beginning. It also works on Objects.
It is created with PHP's serialize and converted back to proper array/object with unserialize.
It serves a similar purpose to the JSON format.

Return object instead of json_encode

So, I found that making my WebMethod As Object, Return Dictionary rather than As String, Return JavaScriptSerializer.Serialized() reduces the size of the JSON by ~ 20%.
Yeah, I know this isn't a big deal for traditional webapps where you're serving the consumer (in the past) and a few kb would be big, but it's HUGE for B2B where you're trying to serve up to your customers AJAXd jQuery pages with far less data transmission and greater speed when transmitting dynamic tables that could be potentially 100mb before dynamicization and id lists of about 1-2mb, but I digress.
It looks like json_encode does the same thing adding more than necessary to the JSON, from what I've read on other posts. Is there a way to simply output the array as an object or build an object from multiple arrays and export that?
1) Is print (and its' family) the only way to output?
2) Is json_encode (and its' family) necessary? After all, I don't have to decode if I output properly at the jQuery level.
I'm a big fan of speed & efficiency. As AJAX/jsLibs take over, and data becomes bigger while these hunkering server-side scripts go by the wayside, it looks like the next logical objective (aside from a standardized push to client) is to keep the size of the JSON as small as possible.
How can I keep the garbage with AJAX/PHP down to a mininum? How can I export arrays as object directly?
Thanks for bearing with me. I'm terrible with vocabulary. I hope what I want to do is relatively clear enough.
As always, thanks in advance, and thank you stack for being my brain!
The problem I was having with .net webmethod was that I was javascriptserializing my output before sending it out with <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> included. That's what added the extra data to my json output.
Arrays can be exported as is with json_encode, and that's all that's needed.

Why need to use JSON in php and AJAX

I just started doing jQuery last week, and so far I already made some basic systems with ajax, like basic jQuery CRUD and simple chat system without referencing on other's work for I decided to test myself on how far I can do systems alone in jQuery(without JSON and XML yet).
But when I decided to look at other's work (hoping to get/learn good practices and codes out there) many or almost every program that deals with ajax have some JSON in it. So I decided to study and read JSON specially this one, but I guess because it's my first time dealing with it, I'm having a problem sinking it into my brain. Yeah I know it is a "lightweight way of describing hierarchical data", I also know how to make JSON like mixing a literal array and object in JS, and how to dsplay it in js.
But my question is, what's the difference and what's the advantage than not using it?
When I can still get and store data on the server using ajax and database without JSON.
By the way I haven't focus on XML yet because based from my research it's better to use JSON in AJAX.
Can you give me some actual scenario dealing with
s1. ajax php mysql (this with what disadvantages?)
and
s2. ajax php mysql json (this with what advantages?)
I mean, my focus is to send and get data, and I already can do it with s1.
Sorry if you find my question stupid. Tia. :)
Why use JSON? The answer is portability and structure.
JSON is portable because parsers and writers are available for many, many languages. This means that JSON that a PHP script generates can be very easily understood by a JavaScript script. It is the best way to transmit complex structures like arrays and objects, and have it still be compatible with multiple languages.
JSON provides structure because the data you transmit with it can have consistent formatting. This is instead of transmitting back plain-text (i.e. unformatted) data, like comma-separated or delimited data.
Data that is merely delimited (for example, "BookName1,BookName2,BookName3") is more difficult for humans to understand, debug, and work with. If you wanted to debug a response between your server and your browser and the data was delimited (like my example above), you might have a hard time understanding it. Also, if you want to add different data types, provide separate records, etc., then your custom data format becomes more complicated. Eventually, you might end up reinventing JSON.
As a side note, JSON is indeed better than XML. It is much more efficient space-wise. There are no tag names to take up space. Structure is created via nested braces, instead of verbose tags.
Resources
Here is an interesting article on the differences and pros/cons of XML and JSON: http://www.json.org/xml.html
Examples
Per your request, here is an example of encoding JSON with PHP. This is ripped from the docs:
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
Output:
{"a":1,"b":2,"c":3,"d":4,"e":5}
Contrast this to something like this, without JSON:
a,1
b,2
c,3
d,4
e,5
To parse that, you'd have to iterate through each line, split the values yourself, and then create the array. This isn't that difficult, but imagine you have a nested object:
$arr = array ('a'=> array(1,2,3),'b'=> array('a' => 1, 'b' => 2),'c'=>3,'d'=> array(1,2,3,4,5) ,'e'=>5); // etc.
With JSON, it's no different to encode it. Just use json_encode. But, encoding this manually, and then decoding it manually would be significantly more work.
Programming in any sort of programming language, you have several different types of data at your disposal, including the very useful array type.
Interchanging data between Javascript and any server side language can only happen through strings. I.e. you can send and return any text, but there's no way to send a native array or number type.
JSON is an elegant way to express array and other types using only a string. This way you can pass arbitrary data back and forth between different environments and are not limited to pure text. XML solves the same kind of problem, but is often overkill for simple AJAX requests.

Categories