I'm working with some serialized data from a MySQL database and I need to deserialize this using Ruby (the serialized data is used to build up a WHERE clause for a database query). PHP has the unserialize() method which will convert it into an Array; what is the Ruby equivalent of this?
The data in question looks like this, if it helps any:
a:2:{s:5:"Lists";a:1:{i:0;s:2:"11";}s:5:"Rules";a:1:{i:0;a:3:{s:4:"type";s:5:"group";s:9:"connector";s:3:"and";s:5:"rules";a:1:{i:0;a:3:{s:4:"type";s:4:"rule";s:9:"connector";s:3:"and";s:5:"rules";a:3:{s:8:"ruleName";s:2:"21";s:12:"ruleOperator";s:10:"isnotempty";s:10:"ruleValues";a:1:{i:0;s:0:"";}}}}}}}
I guess the exact equivalent would be this. You could also check out Ruby's Marshall Class, specifically Marshal.load.
Look at Ruby's Marshal Class.
From the docs:
The marshaling library converts
collections of Ruby objects into a
byte stream, allowing them to be
stored outside the currently active
script. This data may subsequently be
read and the original objects
reconstituted. Marshaled data has
major and minor version numbers stored
along with the object information.
Of course this is a two way street, you can only un-Marshal, Marshaled ruby objects.
If it's XML, there's the Hash.from_xml method.
Related
I'm doing a SOAP request in PHP. The result has an option to return either XML or JSON. I decided on JSON because I am familiar with json_decode. With json_decode, if the parameter 'true' is added, it returns an associative array without it the default is an Object.
This is for a train schedule. It's by station which includes trips and stops. How do I decided in my PHP application I'm writing if I should deal with this train station schedule data as an Object or associative array? What would be the deciding factor? What are the pros and cons to either?
It mostly comes down to which you're the most familiar with.
There are a couple considerations to keep in mind though:
PHP has a large set of functions for dealing with and manipulating arrays. These same functions will (for the most part) not work with StdClass objects. If you're going to need some of this functionality, arrays may be easier.
JSON differentiates between arrays (unkeyed lists of items), and objects (each item stored under a string "key"). If you care about the difference - you need to detect whether something was an array or an object in the original data - it can be difficult to do so with straight PHP arrays, and it may make more sense to go with objects.
Is it possible to make the data type of a column in a table an object of a class?
For instance, if I have a class that manages strings called "MyString", is it possible to use it instead of varchar?
No, you cannot. That's why we have ORM : Object Relational Mapping
Quoting Wikipedia :
Object-relational mapping (ORM, O/RM, and O/R mapping) in computer
software is a programming technique for converting data between
incompatible type systems in object-oriented programming languages.
This creates, in effect, a "virtual object database"
I think that's what you were looking for, good luck !
For good ORMs libraries in PHP, look at this : Good PHP ORM Library? on S.O
No, databases are separate entities to scripting languages, so a database like MySQL has no concept of a PHP Class.... and PHP Classes are different to (for example) Java classes, so to implement this, every database would need a datatype for every conceivable class-based language.... and then consider the practicalities of a PHP script wanting to access data stored by a Java app.
What you can do, however, is serialize objects, and store them in a database table column, of type VARCHAR... but beware that private/protected methods serialise with NULL characters in the serialized string
If you weren't using MYSQL probably you could use User-Defined Datatypes.
But since they are not available for MYSQL, you only can use this Datatypes.
I come from a Java background and have only recently started learning PHP and CodeIgniter.
While I find the framework awesome for its clean design and impressive documentation, I notice that the framework doesn't necessarily discourage the use of data arrays instead of value objects for passing data around. For ex., the database queries return the result in an array which you can then pass over to the views for rendering. Similarly, most of the core library methods take associative arrays as inputs.
This, to me, seems like a bad design for an Object Oriented language which should promote and maybe even enforce using value objects for their obvious benefits of encapsulation.
Is this really an example of bad design or simply a matter of style/preference ? Are there any obvious benefits of using arrays for data over a more OO approach ?
Don't be misled by the myth that "everything has to be an object" for your code to be "good Object Oriented design". When you start trying to formulate rationalisations that "I shouldn't be allowed to do this, because it's not good OOP", you're programming backwards.
When you want a list of pieces of data, a data array will suffice. Indeed, it's appropriate.
Why build a complicated object structure when a hash will do? IMHO, many things in the java world are over-engineered. This opinion seems to be shared with many dynamic languages and toolsets, such as Ruby on Rails.
An array is an object and perfectly valid to use for passing data around. No need to define your own class when a built in one will do.
PHP is not an object oriented language. It's an hybrid language.
Arrays are used everywhere because they are significantly more powerful than in other languages (Java in particular). And behind the scenes both arrays and objects use the same dictionary implementation in PHP.
If you want to objectize arrays, then wrap them in:
$array = new ArrayObject($array, ArrayObject::ARRAY_AS_PROPS);
Or you can just typecast an array 1:1 into an value object:
$obj = (object) $array;
And back:
$array = (array) $obj;
They work alike in quite a few contexts anyway (foreaching over them is easy).
Well, an obvious reason to use value objects instead of associative arrays is that if you're passing data in an array all around the place and then you have to change the name of a db table column, you'll have to update the way you access it everywhere in your code, whereas if you have a value object, you need to update only a one line (inside the VO constructor)
here's a definition of marshaling from Wikipedia:
In computer science, marshalling
(similar to serialization) is the
process of transforming the memory
representation of an object to a data
format suitable for storage or
transmission. It is typically used
when data must be moved between
different parts of a computer program
or from one program to another.
I have always done data serialization in php via its serialize function, usually on objects or arrays. But how is wikipedia's definition of marshaling/serialization takes place in this serizalize() function?
What serialize doesn't do is transport class definitions. When unserializing an object, that object's class definition must be present (loaded from the code base), otherwise unserializing will fail. From the Wikipedia article you mention:
To "marshal" an object means to record its state and codebase(s) in such a way that when the marshalled object is "unmarshalled", a copy of the original object is obtained, possibly by automatically loading the class definitions of the object. You can marshal any object that is serializable or remote. Marshalling is like serialization, except marshalling also records codebases. Marshalling is different from serialization in that marshalling treats remote objects specially.
If I understand correctly, Serialize is definitely not 100% compatible with the definition of marshaling in that respect. I don't know a pre-defined mechanism that would do this in PHP. I guess you would have to combine the serialized data and all necessary class definitions into a package (a ZIP file for example).
Like Pekka mentioned above, PHP doesn't include the class definition, so it does not do marshaling. If the class for a serialized object is present, however, then the answer to your question is yes: serialization is as easy as serialize($abc).
The best way that I know of to take care of marshaling in PHP is to use a third party tool like Google Buffer Protocols or Facebook (Apache?) Thrift, which will serialize and marshal for you. Kind of a roundabout way of doing it (and as long as you have the class present, you don't need to marshal anyway), but they're probably the best solution to the problem.
I am sending an NSData Object to the server script running PHP.Is it possible to collect that object in PHP ?
Is it possible to get NSArray,NSString, NSData or any Custom Objects (Subclass of NSObject) in PHP ?
Thanks
Look for a method for serializing and deserializing your data.
Facebook uses a protocol called Thrift Google uses protocol buffers.
If you don't need to have an industry standard solution, you can come up with your own way to tag values with meta data in order to determine how PHP will handle your data. PHP doesn't (at least that I'm aware of) have all the niftness that objective C has with its objects and inheritance, but PHP has a function or library for almost anything you will need to do.
One approach might be to use JSON to store type/value pair for each parameter you want to process.