简体   繁体   中英

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!

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?)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM