mirkancal
mirkancal

Reputation: 5385

How to unserialize this kind of String in Dart?

I have this kind of string and all I know is serialized.

a:2:{i:0;s:12:"Sample array";i:1;a:2:{i:0;s:5:"Apple";i:1;s:6:"Orange";}}

I didn't find much examples on the web, except php docs or php questions. Not sure it's a common way to serialize it or has a special name. My question is, how to unserialize this string with Dart?

The actual string I'm dealing with is this and I can't unserialize it with some web tools.

"a:2:{s:6:\"monday\";a:3:{i:3;a:6:{s:4:\"show\";s:12:\"side-by-side\";s:9:\"from_hour\";s:2:\"01\";s:12:\"from_minutes\";s:2:\"00\";s:7:\"to_hour\";s:2:\"02\";s:10:\"to_minutes\";s:2:\"00\";s:2:\"id\";s:39:\"monday_dcb5a1b146e20461d8234c13bd3b11b3\";}i:0;a:6:{s:4:\"show\";s:21:\"the-joe-finnegan-show\";s:9:\"from_hour\";s:2:\"08\";s:12:\"from_minutes\";s:2:\"10\";s:7:\"to_hour\";s:2:\"09\";s:10:\"to_minutes\";s:2:\"00\";s:2:\"id\";s:39:\"monday_6ce0d6f821031d782273713b293e8861\";}i:2;a:6:{s:4:\"show\";s:9:\"lets-talk\";s:9:\"from_hour\";s:2:\"11\";s:12:\"from_minutes\";s:2:\"00\";s:7:\"to_hour\";s:2:\"12\";s:10:\"to_minutes\";s:2:\"00\";s:2:\"id\";s:39:\"monday_ea6562913d78053a9dcad09762b9892d\";}}s:7:\"tuesday\";a:1:{i:0;a:6:{s:4:\"show\";s:12:\"side-by-side\";s:9:\"from_hour\";s:2:\"01\";s:12:\"from_minutes\";s:2:\"00\";s:7:\"to_hour\";s:2:\"03\";s:10:\"to_minutes\";s:2:\"00\";s:2:\"id\";s:40:\"tuesday_1acd9974eae192dcff5b72141e65dda2\";}}}"

And here's a related question, seems so simple on the php side. What kind of string is this? How do I unserialize this string?

Upvotes: 1

Views: 442

Answers (1)

Rodrigo Cardozo
Rodrigo Cardozo

Reputation: 435

I've made a translation to Dart of Nicolas Chambrier's javascript implementation to unserialize PHP strings.

Given a serialized string in php, translates and returns a Map<String, dynamic> object.

Here is a direct link to the class php_unserialize.dart, and you can also check how to use it here.

Usage:

String _sample = 'O:8:"stdClass":5:{s:4:"name";s:7:"Rodrigo";s:4:"last";s:7:"Cardozo";s:3:"age";i:35;s:9:"developer";b:1;s:6:"height";d:73.5;}';

var object = Php.unserialize(_sample);  

print(object["name"]);

Upvotes: 2

Related Questions