Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize/deserialize nested objects to JSON with type in PHP

I have classes that extends an abstract class. I need to create instances of these classes through a string - preferably JSON.

Many of the objects are nested, and many properties are private. I need a way to:

  1. Create a JSON string of the complete object (with private properties and nested objects - with their private properties).
  2. Create a new Object from a JSON string, with the correct type and all nested objects.

I guess it needs to be recursive.

I'm using namespaces that end up looking like crap if I just cast the object to an array.

I'm thinking of writing a parser, label the classes in my JSON strings and then hardcode a factory function for every class, but that would take a lot of time.

like image 217
Christoffer Avatar asked Nov 16 '15 09:11

Christoffer


People also ask

How to retrieve data from nested JSON PHP?

Firstly read the contents of the text file into a string variable using the file_get_contents() function and then use json_decode() function to convert the JSON string to a PHP variable. $filepath = './persons. txt'; $json_string = file_get_contents($filepath); $json = json_decode($json_string, true);

What is JsonSerializable?

An object that converts between JSON and the equivalent Foundation objects.

What is JSON serialization PHP?

The JsonSerializable::jsonSerialize() function is an inbuilt function in PHP which is used to serialize the JSON object to a value that can be serialized natively by using json_encode() function. Syntax: mixed JsonSerializable::jsonSerialize( void ) Parameters: This function does not accept any parameters.

What is serialization in PHP give example?

Serializing an object means converting it to a bytestream representation that can be stored in a file. This is useful for persistent data; for example, PHP sessions automatically save and restore objects.


3 Answers

I suggest using the jms serializer: http://jmsyst.com/libs/serializer Easy to use, configuarble and supports all the features you requested.

like image 99
Atan Avatar answered Oct 20 '22 15:10

Atan


I suggest you to use php's serialize function

In cases like this it's better to use this function because it exists for this purpose: you can store the serialized string wherever you want and after unserializing it you will get back the original PHP object with all the properties

With JSON, as you said, you'll have no clue of what class the object was (unless you store it manually as a string) and of course there will be all the problems related to the private properties

like image 1
Moppo Avatar answered Oct 20 '22 15:10

Moppo


There are three methods for doing this: JSON, Serialize, and var_export.

With JSON, it will only work with objects of stdClass, but it's easy to read and can be used outside of PHP.

Serialize works with instances of classes other than stdClass, but it can be difficult to read and can only be used by PHP. http://php.net/manual/en/function.serialize.php

var_export outputs the PHP code to create the object (so you'd write it to a PHP file), it's very easy to read but can't be used outside PHP. The objects will need to have the set state method. http://php.net/manual/en/function.var-export.php

like image 1
Ryan Jenkin Avatar answered Oct 20 '22 14:10

Ryan Jenkin