Darryl Pentz | 3 Sep 15:32
Favicon

[groovy-user] Marshaling list to string and vice versa


I found that if I do:

def someList = ["foo", "bar"]
String myStr = someList.toString()

myStr will have "[foo, bar]"

Is there any way to reverse that marshaling, so something along the lines
of:

def someList = myStr as List

which is incorrect because it makes someList = ['[', 'f', 'o', 'o', ....]
etc. Or do I have to do it the hard way splitting and whatnot?

thanks,
Darryl
--

-- 
View this message in context: http://www.nabble.com/Marshaling-list-to-string-and-vice-versa-tp19289274p19289274.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Norbert Lazzeri | 3 Sep 18:16

Re: [groovy-user] Marshaling list to string and vice versa

maybe regex will help you out.
http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/

Darryl Pentz schrieb:
> I found that if I do:
>
> def someList = ["foo", "bar"]
> String myStr = someList.toString()
>
> myStr will have "[foo, bar]"
>
> Is there any way to reverse that marshaling, so something along the lines
> of:
>
> def someList = myStr as List
>
> which is incorrect because it makes someList = ['[', 'f', 'o', 'o', ....]
> etc. Or do I have to do it the hard way splitting and whatnot?
>
> thanks,
> Darryl
>   

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Guillaume Laforge | 3 Sep 18:40
Gravatar

Re: [groovy-user] Marshaling list to string and vice versa

Yeah, there's nothing out of the box for umarshaling.

On Wed, Sep 3, 2008 at 6:16 PM, Norbert Lazzeri <elnogge@...> wrote:
> maybe regex will help you out.
> http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/
>
> Darryl Pentz schrieb:
>>
>> I found that if I do:
>>
>> def someList = ["foo", "bar"]
>> String myStr = someList.toString()
>>
>> myStr will have "[foo, bar]"
>>
>> Is there any way to reverse that marshaling, so something along the lines
>> of:
>>
>> def someList = myStr as List
>>
>> which is incorrect because it makes someList = ['[', 'f', 'o', 'o', ....]
>> etc. Or do I have to do it the hard way splitting and whatnot?
>>
>> thanks,
>> Darryl
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
(Continue reading)

Stand Trooper | 3 Sep 18:57

Re: [groovy-user] Marshaling list to string and vice versa

however, the following use of metaClass-ing will work if you want to go that route -- add your own error checking as needed

You could, probably, also do it as a non static metaClass or even an ExpandoMetaClass I would suppose

def someList = ["foo", "bar"]
String myStr = someList.toString()

String.metaClass.'static'.toList = {str ->
    //skip the first bracket at the 0 position and the bracket at the last position
    // assume the list will be a comma and space delimited for now
    str.substring(1, str.size() - 1).split(", ")
}

def tmp = String.toList(myStr)

println tmp as List

timo

On Wed, Sep 3, 2008 at 11:40 AM, Guillaume Laforge <glaforge-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Yeah, there's nothing out of the box for umarshaling.

On Wed, Sep 3, 2008 at 6:16 PM, Norbert Lazzeri <elnogge-Mmb7MZpHnFY@public.gmane.org> wrote:
> maybe regex will help you out.
> http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/
>
> Darryl Pentz schrieb:
>>
>> I found that if I do:
>>
>> def someList = ["foo", "bar"]
>> String myStr = someList.toString()
>>
>> myStr will have "[foo, bar]"
>>
>> Is there any way to reverse that marshaling, so something along the lines
>> of:
>>
>> def someList = myStr as List
>>
>> which is incorrect because it makes someList = ['[', 'f', 'o', 'o', ....]
>> etc. Or do I have to do it the hard way splitting and whatnot?
>>
>> thanks,
>> Darryl


Paul King | 4 Sep 11:10
Favicon
Gravatar

Re: [groovy-user] Marshaling list to string and vice versa


You can consider using inspect() and evaluate():

def checkRoundTrip(orig) {
    def string = orig.inspect()
    def object = new GroovyShell().evaluate(string)
    assert orig == object
}
checkRoundTrip(["cat", "dog"])
checkRoundTrip([cat:3, dog:4])
checkRoundTrip(['foo'] as LinkedList)

Cheers, Paul.

Darryl Pentz wrote:
> I found that if I do:
> 
> def someList = ["foo", "bar"]
> String myStr = someList.toString()
> 
> myStr will have "[foo, bar]"
> 
> Is there any way to reverse that marshaling, so something along the lines
> of:
> 
> def someList = myStr as List
> 
> which is incorrect because it makes someList = ['[', 'f', 'o', 'o', ....]
> etc. Or do I have to do it the hard way splitting and whatnot?
> 
> thanks,
> Darryl

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jim White | 5 Sep 07:38

Re: [groovy-user] Marshaling list to string and vice versa

If you're not looking for a human-readable string, you can use 
serialization thusly:

def someList = ["foo", "bar"]
def bos = new ByteArrayOutputStream()
def oos = new ObjectOutputStream(bos)
println someList
oos.writeObject(someList)
oos.close()
def serstr = bos.toString()
println serstr
def ois = new ObjectInputStream(new ByteArrayInputStream(serstr.getBytes()))
def readList = ois.readObject()
println readList
println readList == someList

==>

[foo, bar]
¨Ìsrjava.util.ArrayListxÅ“ô«aùIsizexpwtfootbarx
[foo, bar]
true

Jim

Darryl Pentz wrote:

> I found that if I do:
> 
> def someList = ["foo", "bar"]
> String myStr = someList.toString()
> 
> myStr will have "[foo, bar]"
> 
> Is there any way to reverse that marshaling, so something along the lines
> of:
> 
> def someList = myStr as List
> 
> which is incorrect because it makes someList = ['[', 'f', 'o', 'o', ....]
> etc. Or do I have to do it the hard way splitting and whatnot?
> 
> thanks,
> Darryl

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane