-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Comments
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, |
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 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 |
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? |
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. |
What advantage would that serialization format over the standard JSON serialization format? |
@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). |
@whackashoe Yep! I'm implementing that already. Core feature would be better maybe. |
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. |
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. |
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)); |
sorry for hearing about the compilation troubles. It's a pity that MSVC does such a lousy job in terms of C++11... |
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. |
@nlohmann No problem :) Stared your project, definitely useful and would use in some other projects 👍 |
FYI: A nice serialization/deserialization for arbitrary types is now implemented: see #328. |
Interesting, I'll take a look at it soon. Thanks for the info 👍 |
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!
The text was updated successfully, but these errors were encountered: