- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
XStream within the REPL
Fri, 2010-01-08, 05:33
Dear Scala-ers,
I need a suggestion regarding deserializing classes using XStream within the
REPL
Let's suppose that, in the REPL, I define
class F { var a = 10 }
and then I try the following:
val xml = " 33 "
xstream.fromXML(xml).asInstanceOf[F]
an exception will be thrown because XStream does not know anything of class
F. In fact, an instance of class F, when it's defined within the REPL, is
serialized to the following XML:
10
Is there a way to change the name the REPL assigns to classes defined
on-the-go, so to avoid this problem?
Thanks!
Alex
Fri, 2010-01-08, 07:07
#2
Re: XStream within the REPL
It works! Thanks!
Alex
David Bernard-3 wrote:
>
> I don't know about changing name of class in REPL but you could alias
> it in xstream :
>
> xstream.alias("F", classOf[F])
>
> /davidB
>
> On Fri, Jan 8, 2010 at 05:32, turicum wrote:
>>
>> Dear Scala-ers,
>>
>> I need a suggestion regarding deserializing classes using XStream within
>> the
>> REPL
>>
>> Let's suppose that, in the REPL, I define
>>
>> class F { var a = 10 }
>>
>> and then I try the following:
>>
>> val xml = " 33 "
>> xstream.fromXML(xml).asInstanceOf[F]
>>
>> an exception will be thrown because XStream does not know anything of
>> class
>> F. In fact, an instance of class F, when it's defined within the REPL, is
>> serialized to the following XML:
>>
>> 10
>>
>> Is there a way to change the name the REPL assigns to classes defined
>> on-the-go, so to avoid this problem?
>>
>> Thanks!
>> Alex
>>
>> --
>> View this message in context:
>> http://old.nabble.com/XStream-within-the-REPL-tp27071261p27071261.html
>> Sent from the Scala mailing list archive at Nabble.com.
>>
>>
>
>
Fri, 2010-01-08, 07:57
#3
Re: XStream within the _embedded_ REPL
new problem now: it does not work in an embedded interpreter:
import scala.tools.nsc._
val interp = new Interpreter(new Settings)
val code = ""class F { var a = 10 };"
interp.compileString(code)
interp.bind("xml", "String", """ 33 """)
interp.interpret("""
val xstream = new XStream
println(xstream.toXML(new F)) // this works: 10 (but
different from what I get in the REPL)
xstream.alias("F", classOf[F]) // this is fine
val g = xstream.fromXML(xml)) // throws XStream's
CannotResolveClassException: F : F
""")
Adding
xstream.setClassLoader((new F).getClass.getClassLoader)
solves the problem,
also, in the REPL, but not in the embedded interpreter
xstream.setClassLoader(classOf[F].getClass.getClassLoader)
works too.
Is there a more elegant solution?
Thanks!
Alex
David Bernard-3 wrote:
>
> I don't know about changing name of class in REPL but you could alias
> it in xstream :
>
> xstream.alias("F", classOf[F])
>
> /davidB
>
> On Fri, Jan 8, 2010 at 05:32, turicum wrote:
>>
>> Dear Scala-ers,
>>
>> I need a suggestion regarding deserializing classes using XStream within
>> the
>> REPL
>>
>> Let's suppose that, in the REPL, I define
>>
>> class F { var a = 10 }
>>
>> and then I try the following:
>>
>> val xml = " 33 "
>> xstream.fromXML(xml).asInstanceOf[F]
>>
>> an exception will be thrown because XStream does not know anything of
>> class
>> F. In fact, an instance of class F, when it's defined within the REPL, is
>> serialized to the following XML:
>>
>> 10
>>
>> Is there a way to change the name the REPL assigns to classes defined
>> on-the-go, so to avoid this problem?
>>
>> Thanks!
>> Alex
>>
>> --
>> View this message in context:
>> http://old.nabble.com/XStream-within-the-REPL-tp27071261p27071261.html
>> Sent from the Scala mailing list archive at Nabble.com.
>>
>>
>
>
Fri, 2010-01-08, 08:07
#4
Re: XStream within the _embedded_ REPL
I don't known about your classloader issue. but it's better to set
alias (and other xstream initialisation) before any call to toXml(...)
or fromXML(...).
On Fri, Jan 8, 2010 at 07:46, turicum wrote:
>
> new problem now: it does not work in an embedded interpreter:
>
> import scala.tools.nsc._
> val interp = new Interpreter(new Settings)
> val code = ""class F { var a = 10 };"
> interp.compileString(code)
> interp.bind("xml", "String", """ 33 """)
> interp.interpret("""
> val xstream = new XStream
> println(xstream.toXML(new F)) // this works: 10 (but
> different from what I get in the REPL)
> xstream.alias("F", classOf[F]) // this is fine
> val g = xstream.fromXML(xml)) // throws XStream's
> CannotResolveClassException: F : F
> """)
>
> Adding
>
> xstream.setClassLoader((new F).getClass.getClassLoader)
>
> solves the problem,
> also, in the REPL, but not in the embedded interpreter
>
> xstream.setClassLoader(classOf[F].getClass.getClassLoader)
>
> works too.
>
> Is there a more elegant solution?
>
> Thanks!
> Alex
>
>
> David Bernard-3 wrote:
>>
>> I don't know about changing name of class in REPL but you could alias
>> it in xstream :
>>
>> xstream.alias("F", classOf[F])
>>
>> /davidB
>>
>> On Fri, Jan 8, 2010 at 05:32, turicum wrote:
>>>
>>> Dear Scala-ers,
>>>
>>> I need a suggestion regarding deserializing classes using XStream within
>>> the
>>> REPL
>>>
>>> Let's suppose that, in the REPL, I define
>>>
>>> class F { var a = 10 }
>>>
>>> and then I try the following:
>>>
>>> val xml = " 33 "
>>> xstream.fromXML(xml).asInstanceOf[F]
>>>
>>> an exception will be thrown because XStream does not know anything of
>>> class
>>> F. In fact, an instance of class F, when it's defined within the REPL, is
>>> serialized to the following XML:
>>>
>>> 10
>>>
>>> Is there a way to change the name the REPL assigns to classes defined
>>> on-the-go, so to avoid this problem?
>>>
>>> Thanks!
>>> Alex
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/XStream-within-the-REPL-tp27071261p27071261.html
>>> Sent from the Scala mailing list archive at Nabble.com.
>>>
>>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/XStream-within-the-REPL-tp27071261p27071974.html
> Sent from the Scala mailing list archive at Nabble.com.
>
>
Fri, 2010-01-08, 08:17
#5
Re: XStream within the _embedded_ REPL
using
xstream.setClassLoader(this.getClass.getClassLoader)
does the job!
Alex
turicum wrote:
>
> new problem now: it does not work in an embedded interpreter:
>
> import scala.tools.nsc._
> val interp = new Interpreter(new Settings)
> val code = ""class F { var a = 10 };"
> interp.compileString(code)
> interp.bind("xml", "String", """ 33 """)
> interp.interpret("""
> val xstream = new XStream
> println(xstream.toXML(new F)) // this works: 10 (but
> different from what I get in the REPL)
> xstream.alias("F", classOf[F]) // this is fine
> val g = xstream.fromXML(xml)) // throws XStream's
> CannotResolveClassException: F : F
> """)
>
> Adding
>
> xstream.setClassLoader((new F).getClass.getClassLoader)
>
> solves the problem,
> also, in the REPL, but not in the embedded interpreter
>
> xstream.setClassLoader(classOf[F].getClass.getClassLoader)
>
> works too.
>
> Is there a more elegant solution?
>
> Thanks!
> Alex
>
>
> David Bernard-3 wrote:
>>
>> I don't know about changing name of class in REPL but you could alias
>> it in xstream :
>>
>> xstream.alias("F", classOf[F])
>>
>> /davidB
>>
>> On Fri, Jan 8, 2010 at 05:32, turicum wrote:
>>>
>>> Dear Scala-ers,
>>>
>>> I need a suggestion regarding deserializing classes using XStream within
>>> the
>>> REPL
>>>
>>> Let's suppose that, in the REPL, I define
>>>
>>> class F { var a = 10 }
>>>
>>> and then I try the following:
>>>
>>> val xml = " 33 "
>>> xstream.fromXML(xml).asInstanceOf[F]
>>>
>>> an exception will be thrown because XStream does not know anything of
>>> class
>>> F. In fact, an instance of class F, when it's defined within the REPL,
>>> is
>>> serialized to the following XML:
>>>
>>> 10
>>>
>>> Is there a way to change the name the REPL assigns to classes defined
>>> on-the-go, so to avoid this problem?
>>>
>>> Thanks!
>>> Alex
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/XStream-within-the-REPL-tp27071261p27071261.html
>>> Sent from the Scala mailing list archive at Nabble.com.
>>>
>>>
>>
>>
>
>
I don't know about changing name of class in REPL but you could alias
it in xstream :
xstream.alias("F", classOf[F])
/davidB
On Fri, Jan 8, 2010 at 05:32, turicum wrote:
>
> Dear Scala-ers,
>
> I need a suggestion regarding deserializing classes using XStream within the
> REPL
>
> Let's suppose that, in the REPL, I define
>
> class F { var a = 10 }
>
> and then I try the following:
>
> val xml = " 33 "
> xstream.fromXML(xml).asInstanceOf[F]
>
> an exception will be thrown because XStream does not know anything of class
> F. In fact, an instance of class F, when it's defined within the REPL, is
> serialized to the following XML:
>
> 10
>
> Is there a way to change the name the REPL assigns to classes defined
> on-the-go, so to avoid this problem?
>
> Thanks!
> Alex
>
> --
> View this message in context: http://old.nabble.com/XStream-within-the-REPL-tp27071261p27071261.html
> Sent from the Scala mailing list archive at Nabble.com.
>
>