Micor
Micor

Reputation: 1542

Groovy equivalent for php's unserialize

I am working with serialized data from Wordpress database. Strings created with PHP serialize function and look something like this:

a:4:{s:6:"weight";s:2:"15";s:6:"length";s:1:"8";s:5:"width";s:1:"8";s:6:"height";s:2:"17";}

Is there a way to deserialize this in Groovy? or is this not a product of standardized serialization?

Thanks!

Upvotes: 0

Views: 355

Answers (2)

tim_yates
tim_yates

Reputation: 171144

Here's an example using the pherialize library

@GrabResolver( name='Ailis', root='http://nexus.ailis.de/content/groups/public' )
@Grab( 'de.ailis.pherialize:pherialize:1.2.1' )
import de.ailis.pherialize.*

def phpValue = 'a:4:{s:6:"weight";s:2:"15";s:6:"length";s:1:"8";s:5:"width";s:1:"8";s:6:"height";s:2:"17";}'

def groovyMap = Pherialize.unserialize( phpValue ).toArray().collectEntries { k, v ->
    [ k.toType( k.type ), v.toType( v.type ) ]
}

assert groovyMap == [ weight:'15', length:'8', width:'8', height:'17' ]

Though if possible, I'd suggest you share data between the two in a format they both natively speak (json?)

Upvotes: 3

Opal
Opal

Reputation: 84844

There was a similar question already. It seems that there are parsers for PHP serialization implemented in java. Every java lib can used with groovy, so solving this shouldn't be a problem. On of the libraries can be found here

Upvotes: 1

Related Questions