简体   繁体   English

Groovy等同于PHP的反序列化

[英]Groovy equivalent for php's unserialize

I am working with serialized data from Wordpress database. 我正在使用Wordpress数据库中的序列化数据。 Strings created with PHP serialize function and look something like this: 用PHP序列化函数创建的字符串如下所示:

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";} 一个:4:{S:6: “重量”; S:2: “15”; S:6: “长度”; S:1: “8”; S:5: “宽度”; S:1:” 8 “; S:6:” 高度 “; S:2:” 17" ;}

Is there a way to deserialize this in Groovy? 在Groovy中是否可以反序列化此方法? or is this not a product of standardized serialization? 还是这不是标准化序列化的产物?

Thanks! 谢谢!

Here's an example using the pherialize library 这是使用pherialize的示例

@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?) 尽管可能的话,我建议您以两者都以本地语言(json?)的格式共享数据。

There was a similar question already. 一个类似的问题了。 It seems that there are parsers for PHP serialization implemented in java. 似乎有用Java实现的PHP序列化解析器。 Every java lib can used with groovy, so solving this shouldn't be a problem. 每个Java库都可以与groovy一起使用,因此解决此问题应该不是问题。 On of the libraries can be found here 可以在此处找到其中的库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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