Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize/Deserialize like PHP? #55

Closed
irazasyed opened this issue Apr 6, 2015 · 15 comments
Closed

Serialize/Deserialize like PHP? #55

irazasyed opened this issue Apr 6, 2015 · 15 comments

Comments

@irazasyed
Copy link

Hi,

Is there a way we could Serialize the output like PHP's Serialize version?

Currently it outputs in one line as a string. Would be helpful if it could support additional method such as this one.

Let me know!

Thanks.

P.S Kudos for the awesome work you've done. Very nice, clean and easier to use!

@irazasyed irazasyed changed the title Serialize/Unserialize like PHP? Serialize/Deserialize like PHP? Apr 7, 2015
@nlohmann
Copy link
Owner

nlohmann commented Apr 8, 2015

Dear @irazasyed, thanks for the interest in the project!

Do I understand correctly that you would like support to serialize (viz. dump) arbitrary data structures to JSON? If so, I feel that this is a very complex task, and I currently have no plans in realizing this. If this is what you are looking for, I suggest looking at the http://uscilab.github.io/cereal project.

Let me know if I misunderstood.

All the best,
Niels

@irazasyed
Copy link
Author

Hi @nlohmann, Thanks for the response.

Check this example: http://ideone.com/TymHJI If you pass an array, the PHP's serialize function dumps the data into a string while keeping the data type (Such as s = String, i = Int, a = array, b = bool & so on). So when we deserialize, we have the format back to same as how it was previously sent, could be an object, array, class, collection or even just a string.

More details are here: http://php.net/manual/en/function.serialize.php#66147

This way, we can serialize the data and store it into the database and or other storage systems, even a text file or use it to exchange data between an API (In my case it's a requirement). It's also handy for encrypting data, many use cases.

The project you've linked me above is not what I was looking for (It doesn't Serialize like PHP), I've already checked that.

It would be good if your project can do it, if possible.

Let me know!

Thanks again.

Syed

@nlohmann
Copy link
Owner

nlohmann commented Apr 9, 2015

If I understand you correctly, then you want to be able to dump a JSON value to the format of PHP's serialize function, right?

@whackashoe
Copy link
Contributor

Should be able to write a serialize function pretty easily I think.

Loop through all keys, do each of the is_X functions to determine what to prepend to its value.

Unserializing I think would be slightly more difficult. You'd have to make a structure that had a pointer to the actual data, as well as some bits to say what the type of data its pointing to is called.

@nlohmann
Copy link
Owner

nlohmann commented Apr 9, 2015

What advantage would that serialization format over the standard JSON serialization format?

@irazasyed
Copy link
Author

@nlohmann Exactly! That's what i want to do as the API built using PHP sends data encrypted as well as accepts encrypted data. Now that encryption function serializes the data with json encode and likewise deserializes when passed to the decryption (later json decodes).

As far as the advantage is concerned, it would mostly be useful for people working with PHP i believe. To exchange the data with a program while preserving the data types and original object, to store into database, memory, file, etc. (Not that JSON can't be used for the same).

@irazasyed
Copy link
Author

@whackashoe Yep! I'm implementing that already. Core feature would be better maybe.

@nlohmann
Copy link
Owner

nlohmann commented Apr 9, 2015

I know that it would be easy, but it would be out of scope of JSON, and I understand that in the end JSON could be used for the same purpose.

@irazasyed
Copy link
Author

No problem. I understand.

BTW Tried to compile this with C++11 and it always ends up with lot of errors. Ended up moving to another library though. I see it was also reported already in #50 issue.

Thanks for your time though.

@nlohmann
Copy link
Owner

nlohmann commented Apr 9, 2015

I think it would be cleaner to leave PHP-specific transformations within PHP. Doesn't it boil down to:

$json_value = "[1,2,3]";
echo serialize(json_decode($json_value));

$serialized_value = "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}";
echo json_encode(unserialize($serialized_value));

@nlohmann
Copy link
Owner

nlohmann commented Apr 9, 2015

@irazasyed ,

sorry for hearing about the compilation troubles. It's a pity that MSVC does such a lousy job in terms of C++11...

@irazasyed
Copy link
Author

Well as i said, The API which i use sends/receives only encrypted data. And unfortunately that encryption function has hardcoded the serialization functionality already, due to which I'm being forced to follow their standard methods to serialize the data in C++, JSON encode, etc. Otherwise, I would've sent the data as JSON encoded already & in PHP serialize/deserialize.

@irazasyed
Copy link
Author

@nlohmann No problem :)

Stared your project, definitely useful and would use in some other projects 👍

@nlohmann
Copy link
Owner

FYI: A nice serialization/deserialization for arbitrary types is now implemented: see #328.

@irazasyed
Copy link
Author

Interesting, I'll take a look at it soon.

Thanks for the info 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants