[Lift] Re: Object typecast to Mapper
Hi All, Thanks again, for support. Made many things more clear. Thanks Amit Kumar Verma On Apr 22, 1:35 am, David Pollak feeder.of.the.be...@gmail.com wrote: Amit, Class.forName(...) is called reflection in Scala/Java land. It allows you to get a class based on a String. You can then create a new instance of the class with the newInstance() method. However, what you get is an instance of Object... and you have to case it into something else before using it. In Java, there's only one way to cast things: Object o = someClass.newInstance(); FooBar fb = (FooBar) o; In Scala, there are two ways to cast (one is safer and less verbose, the other is intentionally more verbose): val a: AnyRef = someClass.newInstance val fb: FooBar = a.asInstanceOf[FooBar] or (the radically better way) a match { case fb: FooBar = ... case _ = ... } Hope this helps. Thanks, David On Mon, Apr 20, 2009 at 4:41 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: Hi All, This is a sample function for making an object from string at run time. Here we are not casting the object but creating one. I wanted the same thing for casting the object. public static Object bindObject(Class className) { Object objOutput = null; try { String sClassName = className.getPackage().getName().concat (.Wrap.concat(className.getSimpleName())); objOutput = Class.forName(sClassName.replaceFirst (com.vtech, com.vtech.appxtension)).newInstance(); } catch (Exception e) { try { objOutput = Class.forName(className.getName ()).newInstance(); } catch (Exception e1) { e1.printStackTrace(); } } return objOutput; } Thanks to all for kind support.. Amit Kumar Verma On Apr 18, 8:51 pm, Timothy Perrett timo...@getintheloop.eu wrote: So your talking about reflection right? Take a look at scala Manifests (which aide getting round type erasure) - other than that scala supports all the normal reflection tooling that Java does. Tim On 18/04/2009 06:56, Amit Kumar Verma cdac.a...@gmail.com wrote: Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. But we use this feature in Java for casting the objects. -- Lift, the simply functional web frameworkhttp://liftweb.net Beginning Scalahttp://www.apress.com/book/view/1430219890 Follow me:http://twitter.com/dpp Git some:http://github.com/dpp --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Object typecast to Mapper
Amit, Class.forName(...) is called reflection in Scala/Java land. It allows you to get a class based on a String. You can then create a new instance of the class with the newInstance() method. However, what you get is an instance of Object... and you have to case it into something else before using it. In Java, there's only one way to cast things: Object o = someClass.newInstance(); FooBar fb = (FooBar) o; In Scala, there are two ways to cast (one is safer and less verbose, the other is intentionally more verbose): val a: AnyRef = someClass.newInstance val fb: FooBar = a.asInstanceOf[FooBar] or (the radically better way) a match { case fb: FooBar = ... case _ = ... } Hope this helps. Thanks, David On Mon, Apr 20, 2009 at 4:41 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: Hi All, This is a sample function for making an object from string at run time. Here we are not casting the object but creating one. I wanted the same thing for casting the object. public static Object bindObject(Class className) { Object objOutput = null; try { String sClassName = className.getPackage().getName().concat (.Wrap.concat(className.getSimpleName())); objOutput = Class.forName(sClassName.replaceFirst (com.vtech, com.vtech.appxtension)).newInstance(); } catch (Exception e) { try { objOutput = Class.forName(className.getName ()).newInstance(); } catch (Exception e1) { e1.printStackTrace(); } } return objOutput; } Thanks to all for kind support.. Amit Kumar Verma On Apr 18, 8:51 pm, Timothy Perrett timo...@getintheloop.eu wrote: So your talking about reflection right? Take a look at scala Manifests (which aide getting round type erasure) - other than that scala supports all the normal reflection tooling that Java does. Tim On 18/04/2009 06:56, Amit Kumar Verma cdac.a...@gmail.com wrote: Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. But we use this feature in Java for casting the objects. -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Git some: http://github.com/dpp --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Object typecast to Mapper
Hi Amit, Try that one def bindObject[T : AnyRef](className: Class[T]): Option[Object] = { val sClassName = className.getPackage.getName.concat(.Wrap.concat(className.getSimpleName)) try { Some(Class.forName(sClassName.replaceFirst(com.vtech, com.vtech.appxtension)).newInstance.asInstanceOf[T]) } catch { case e: Exception = try { Some(Class.forName(className.getName).newInstance.asInstanceOf[T]) } catch { case e1: Exception = println(e1.printStackTrace) None } } } Regards, Sergey On Mon, Apr 20, 2009 at 3:41 PM, Amit Kumar Verma cdac.a...@gmail.comwrote: Hi All, This is a sample function for making an object from string at run time. Here we are not casting the object but creating one. I wanted the same thing for casting the object. public static Object bindObject(Class className) { Object objOutput = null; try { String sClassName = className.getPackage().getName().concat (.Wrap.concat(className.getSimpleName())); objOutput = Class.forName(sClassName.replaceFirst (com.vtech, com.vtech.appxtension)).newInstance(); } catch (Exception e) { try { objOutput = Class.forName(className.getName ()).newInstance(); } catch (Exception e1) { e1.printStackTrace(); } } return objOutput; } Thanks to all for kind support.. Amit Kumar Verma On Apr 18, 8:51 pm, Timothy Perrett timo...@getintheloop.eu wrote: So your talking about reflection right? Take a look at scala Manifests (which aide getting round type erasure) - other than that scala supports all the normal reflection tooling that Java does. Tim On 18/04/2009 06:56, Amit Kumar Verma cdac.a...@gmail.com wrote: Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. But we use this feature in Java for casting the objects. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Object typecast to Mapper
Hi David, Sorry I used dynamic binding which was not correct. But in the previous thread you said : Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. But we use this feature in Java for casting the objects. I will write this to scala group. I have also written you about the road map of lift. Thanks for your support Amit Kumar verma On Apr 17, 8:30 pm, David Pollak feeder.of.the.be...@gmail.com wrote: On Fri, Apr 17, 2009 at 2:00 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: Hi David, thanks, this is working fine. Problem is resolved. But dynamic binding is feature of java then why Scala don't support this. I'm sorry, I don't understand what dynamic binding is. Scala supports casting just like Java. This is an important feature. Thanks again Amit Kumar Verma On Apr 13, 10:10 pm, David Pollak feeder.of.the.be...@gmail.com wrote: I think it's best to use the built-in JSON creation code and keep the code as JsCmd as long as possible. Here's a snippet that will place the JSON for all the Users in the page. I'm including the whole project as well: class HelloWorld { def json: NodeSeq = Script(jsonForAll(User)) private def jsonForAll[T : Mapper[T]](meta: MetaMapper[T]): JsCmd = JsArray(meta.findAll.map(_.asJs) :_*) } If you have further questions, please update the project and mark the places in the code where you have a question and we'll work from there. Thanks, David On Mon, Apr 13, 2009 at 2:07 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: Hi David, Thanks for ur replies, I just want to make a generic function which will take an object of mapper and returns the json string of the same. I have written only two function 1. This will be called by ajax call : def getJSONString(xhtml: Group):NodeSeq = { getJSONStringGeneric(TestGearLogin) // here 'TestGearLogin' is a mapper object } 2. function to make JSON string i.e def getJSONStringGeneric(anyObject :Any):NodeSeq = { var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = // this line is giving error mm.findAll.map(userdetails = { var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) tempJSON = ret.toString(); // some manipulation for making correct JSON tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON }); } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } I changed the code as you suggested but getting the error as I posted. Please advise. Thanks Amit Kumar Verma On Apr 10, 9:46 pm, David Pollak feeder.of.the.be...@gmail.com wrote: I think the .asJs method on all Mapper instances should give you the object in JavaScript representation. If you can post an entire file, I can work on helping you if the above doesn't work. On Thu, Apr 9, 2009 at 6:53 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: copied the the same code but getting this error type arguments [_] do not conform to trait MetaMapper's type parameter bounds [A : net.liftweb.mapper.Mapper[A]] TravelMerchantTest/src/main/ scala/com/vtech/travelmerchant/snippet default.scala line 142 1239284830763 85593 Actually I am trying to make the JSON object using a mapper object. My function is this : def getJSONStringGeneric(anyObject :Any):NodeSeq = { //Console.println(anyObject.getClass +anyObject.getClass); //Console.println(anyObject.getClass.getName +anyObject.getClass.getName); //var objTemp = anyObject.asInstanceOf[MetaMapper [TestGearLogin]]; var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = mm.findAll.map( (userdetails: Mapper[_]) ={ var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) // ret will be like
[Lift] Re: Object typecast to Mapper
Amit, There is no way that I know of in Java to construct a string at runtime and use that string for casting. If you can show me an example of what you mean in Java I'll show you the equivilent Scala code. In terms of the roadmap, you started a thread on that. If you have follow-up questions, please post them to that thread. Thanks, David On Apr 18, 2009 7:02 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: Hi David, Sorry I used dynamic binding which was not correct. But in the previous thread you said : Scala is a static language, so the class for casting must be known at compile time. It's not poss... But we use this feature in Java for casting the objects. I will write this to scala group. I have also written you about the road map of lift. Thanks for your support Amit Kumar verma On Apr 17, 8:30 pm, David Pollak feeder.of.the.be...@gmail.com wrote: On Fri, Apr 17, 2009 at 2:00 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: Hi David,thanks, this is working fine. Problem is resolved. But dynamic bindi... --~--~-~--~~~---~--~~ You received this message because you are subs... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Object typecast to Mapper
So your talking about reflection right? Take a look at scala Manifests (which aide getting round type erasure) - other than that scala supports all the normal reflection tooling that Java does. Tim On 18/04/2009 06:56, Amit Kumar Verma cdac.a...@gmail.com wrote: Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. But we use this feature in Java for casting the objects. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Object typecast to Mapper
Hi David, thanks, this is working fine. Problem is resolved. But dynamic binding is feature of java then why Scala don't support this. This is an important feature. Thanks again Amit Kumar Verma On Apr 13, 10:10 pm, David Pollak feeder.of.the.be...@gmail.com wrote: I think it's best to use the built-in JSON creation code and keep the code as JsCmd as long as possible. Here's a snippet that will place the JSON for all the Users in the page. I'm including the whole project as well: class HelloWorld { def json: NodeSeq = Script(jsonForAll(User)) private def jsonForAll[T : Mapper[T]](meta: MetaMapper[T]): JsCmd = JsArray(meta.findAll.map(_.asJs) :_*) } If you have further questions, please update the project and mark the places in the code where you have a question and we'll work from there. Thanks, David On Mon, Apr 13, 2009 at 2:07 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: Hi David, Thanks for ur replies, I just want to make a generic function which will take an object of mapper and returns the json string of the same. I have written only two function 1. This will be called by ajax call : def getJSONString(xhtml: Group):NodeSeq = { getJSONStringGeneric(TestGearLogin) // here 'TestGearLogin' is a mapper object } 2. function to make JSON string i.e def getJSONStringGeneric(anyObject :Any):NodeSeq = { var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = // this line is giving error mm.findAll.map(userdetails = { var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) tempJSON = ret.toString(); // some manipulation for making correct JSON tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON }); } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } I changed the code as you suggested but getting the error as I posted. Please advise. Thanks Amit Kumar Verma On Apr 10, 9:46 pm, David Pollak feeder.of.the.be...@gmail.com wrote: I think the .asJs method on all Mapper instances should give you the object in JavaScript representation. If you can post an entire file, I can work on helping you if the above doesn't work. On Thu, Apr 9, 2009 at 6:53 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: copied the the same code but getting this error type arguments [_] do not conform to trait MetaMapper's type parameter bounds [A : net.liftweb.mapper.Mapper[A]] TravelMerchantTest/src/main/ scala/com/vtech/travelmerchant/snippet default.scala line 142 1239284830763 85593 Actually I am trying to make the JSON object using a mapper object. My function is this : def getJSONStringGeneric(anyObject :Any):NodeSeq = { //Console.println(anyObject.getClass +anyObject.getClass); //Console.println(anyObject.getClass.getName +anyObject.getClass.getName); //var objTemp = anyObject.asInstanceOf[MetaMapper [TestGearLogin]]; var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = mm.findAll.map( (userdetails: Mapper[_]) ={ var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) // ret will be like {username=222,password=222,audit_dt=2009-04-06 00:00:00.0,login_pid=26} tempJSON = ret.toString(); tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON } ) } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } but not getting a way of doing this. Please advise. Thanks Amit Kumar Verma On Apr 9, 6:14 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Howdy, Scala is a static language, so the class for casting must be known at compile time.
[Lift] Re: Object typecast to Mapper
On Fri, Apr 17, 2009 at 2:00 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: Hi David, thanks, this is working fine. Problem is resolved. But dynamic binding is feature of java then why Scala don't support this. I'm sorry, I don't understand what dynamic binding is. Scala supports casting just like Java. This is an important feature. Thanks again Amit Kumar Verma On Apr 13, 10:10 pm, David Pollak feeder.of.the.be...@gmail.com wrote: I think it's best to use the built-in JSON creation code and keep the code as JsCmd as long as possible. Here's a snippet that will place the JSON for all the Users in the page. I'm including the whole project as well: class HelloWorld { def json: NodeSeq = Script(jsonForAll(User)) private def jsonForAll[T : Mapper[T]](meta: MetaMapper[T]): JsCmd = JsArray(meta.findAll.map(_.asJs) :_*) } If you have further questions, please update the project and mark the places in the code where you have a question and we'll work from there. Thanks, David On Mon, Apr 13, 2009 at 2:07 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: Hi David, Thanks for ur replies, I just want to make a generic function which will take an object of mapper and returns the json string of the same. I have written only two function 1. This will be called by ajax call : def getJSONString(xhtml: Group):NodeSeq = { getJSONStringGeneric(TestGearLogin) // here 'TestGearLogin' is a mapper object } 2. function to make JSON string i.e def getJSONStringGeneric(anyObject :Any):NodeSeq = { var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = // this line is giving error mm.findAll.map(userdetails = { var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) tempJSON = ret.toString(); // some manipulation for making correct JSON tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON }); } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } I changed the code as you suggested but getting the error as I posted. Please advise. Thanks Amit Kumar Verma On Apr 10, 9:46 pm, David Pollak feeder.of.the.be...@gmail.com wrote: I think the .asJs method on all Mapper instances should give you the object in JavaScript representation. If you can post an entire file, I can work on helping you if the above doesn't work. On Thu, Apr 9, 2009 at 6:53 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: copied the the same code but getting this error type arguments [_] do not conform to trait MetaMapper's type parameter bounds [A : net.liftweb.mapper.Mapper[A]] TravelMerchantTest/src/main/ scala/com/vtech/travelmerchant/snippet default.scala line 142 1239284830763 85593 Actually I am trying to make the JSON object using a mapper object. My function is this : def getJSONStringGeneric(anyObject :Any):NodeSeq = { //Console.println(anyObject.getClass +anyObject.getClass); //Console.println(anyObject.getClass.getName +anyObject.getClass.getName); //var objTemp = anyObject.asInstanceOf[MetaMapper [TestGearLogin]]; var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = mm.findAll.map( (userdetails: Mapper[_]) ={ var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) // ret will be like {username=222,password=222,audit_dt=2009-04-06 00:00:00.0,login_pid=26} tempJSON = ret.toString(); tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON } ) } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last
[Lift] Re: Object typecast to Mapper
Hi David, Thanks for ur replies, I just want to make a generic function which will take an object of mapper and returns the json string of the same. I have written only two function 1. This will be called by ajax call : def getJSONString(xhtml: Group):NodeSeq = { getJSONStringGeneric(TestGearLogin) // here 'TestGearLogin' is a mapper object } 2. function to make JSON string i.e def getJSONStringGeneric(anyObject :Any):NodeSeq = { var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = // this line is giving error mm.findAll.map(userdetails = { var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) tempJSON = ret.toString(); // some manipulation for making correct JSON tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON }); } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } I changed the code as you suggested but getting the error as I posted. Please advise. Thanks Amit Kumar Verma On Apr 10, 9:46 pm, David Pollak feeder.of.the.be...@gmail.com wrote: I think the .asJs method on all Mapper instances should give you the object in JavaScript representation. If you can post an entire file, I can work on helping you if the above doesn't work. On Thu, Apr 9, 2009 at 6:53 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: copied the the same code but getting this error type arguments [_] do not conform to trait MetaMapper's type parameter bounds [A : net.liftweb.mapper.Mapper[A]] TravelMerchantTest/src/main/ scala/com/vtech/travelmerchant/snippet default.scala line 142 1239284830763 85593 Actually I am trying to make the JSON object using a mapper object. My function is this : def getJSONStringGeneric(anyObject :Any):NodeSeq = { //Console.println(anyObject.getClass +anyObject.getClass); //Console.println(anyObject.getClass.getName +anyObject.getClass.getName); //var objTemp = anyObject.asInstanceOf[MetaMapper [TestGearLogin]]; var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = mm.findAll.map( (userdetails: Mapper[_]) ={ var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) // ret will be like {username=222,password=222,audit_dt=2009-04-06 00:00:00.0,login_pid=26} tempJSON = ret.toString(); tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON } ) } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } but not getting a way of doing this. Please advise. Thanks Amit Kumar Verma On Apr 9, 6:14 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Howdy, Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. However, casting to a known class is easy in Scala... and it's done primarily using pattern matching. The following code: def foo(in: Any) = in match { case mm: MetaMapper[_] = mm.findAll.map( (m: Mapper[_]) = m.asJs ) case _ = } Does what I think you want. It takes an incoming instance, in and matches it against being an instance of MetaMapper[_]. This means its some type of MetaMapper (we don't know or care what the type parameter is). If it is a MetaMapper, it's assigned to the mm variable. We can then call findAll on that variable and we have a bunch of Mapper[_] instances. Note that I explicitly called out the type of m in the function, but that line could be re-written mm.findAll.map(m = m.asJs) because the compiler infers the type of m. Does this help? Thanks, David On Thu, Apr 9, 2009 at 3:55 AM, Amit Kumar Verma cdac.a...@gmail.com
[Lift] Re: Object typecast to Mapper
I think it's best to use the built-in JSON creation code and keep the code as JsCmd as long as possible. Here's a snippet that will place the JSON for all the Users in the page. I'm including the whole project as well: class HelloWorld { def json: NodeSeq = Script(jsonForAll(User)) private def jsonForAll[T : Mapper[T]](meta: MetaMapper[T]): JsCmd = JsArray(meta.findAll.map(_.asJs) :_*) } If you have further questions, please update the project and mark the places in the code where you have a question and we'll work from there. Thanks, David On Mon, Apr 13, 2009 at 2:07 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: Hi David, Thanks for ur replies, I just want to make a generic function which will take an object of mapper and returns the json string of the same. I have written only two function 1. This will be called by ajax call : def getJSONString(xhtml: Group):NodeSeq = { getJSONStringGeneric(TestGearLogin) // here 'TestGearLogin' is a mapper object } 2. function to make JSON string i.e def getJSONStringGeneric(anyObject :Any):NodeSeq = { var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = // this line is giving error mm.findAll.map(userdetails = { var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) tempJSON = ret.toString(); // some manipulation for making correct JSON tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON }); } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } I changed the code as you suggested but getting the error as I posted. Please advise. Thanks Amit Kumar Verma On Apr 10, 9:46 pm, David Pollak feeder.of.the.be...@gmail.com wrote: I think the .asJs method on all Mapper instances should give you the object in JavaScript representation. If you can post an entire file, I can work on helping you if the above doesn't work. On Thu, Apr 9, 2009 at 6:53 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: copied the the same code but getting this error type arguments [_] do not conform to trait MetaMapper's type parameter bounds [A : net.liftweb.mapper.Mapper[A]] TravelMerchantTest/src/main/ scala/com/vtech/travelmerchant/snippet default.scala line 142 1239284830763 85593 Actually I am trying to make the JSON object using a mapper object. My function is this : def getJSONStringGeneric(anyObject :Any):NodeSeq = { //Console.println(anyObject.getClass +anyObject.getClass); //Console.println(anyObject.getClass.getName +anyObject.getClass.getName); //var objTemp = anyObject.asInstanceOf[MetaMapper [TestGearLogin]]; var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = mm.findAll.map( (userdetails: Mapper[_]) ={ var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) // ret will be like {username=222,password=222,audit_dt=2009-04-06 00:00:00.0,login_pid=26} tempJSON = ret.toString(); tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON } ) } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } but not getting a way of doing this. Please advise. Thanks Amit Kumar Verma On Apr 9, 6:14 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Howdy, Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. However, casting to a known class is easy in Scala... and it's done primarily using pattern matching. The following code: def foo(in: Any) = in match { case mm: MetaMapper[_] = mm.findAll.map(
[Lift] Re: Object typecast to Mapper
I think the .asJs method on all Mapper instances should give you the object in JavaScript representation. If you can post an entire file, I can work on helping you if the above doesn't work. On Thu, Apr 9, 2009 at 6:53 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: copied the the same code but getting this error type arguments [_] do not conform to trait MetaMapper's type parameter bounds [A : net.liftweb.mapper.Mapper[A]] TravelMerchantTest/src/main/ scala/com/vtech/travelmerchant/snippet default.scala line 142 1239284830763 85593 Actually I am trying to make the JSON object using a mapper object. My function is this : def getJSONStringGeneric(anyObject :Any):NodeSeq = { //Console.println(anyObject.getClass +anyObject.getClass); //Console.println(anyObject.getClass.getName +anyObject.getClass.getName); //var objTemp = anyObject.asInstanceOf[MetaMapper [TestGearLogin]]; var sJSONString = {\bindings\: [; anyObject match { case mm: MetaMapper[_] = mm.findAll.map( (userdetails: Mapper[_]) ={ var tempJSON = ; val ret = new StringBuilder ret.append({) ret.append(userdetails.getSingleton.appendFieldToStrings (userdetails)) ret.append(}) // ret will be like {username=222,password=222,audit_dt=2009-04-06 00:00:00.0,login_pid=26} tempJSON = ret.toString(); tempJSON = tempJSON.replaceAll(\\{,\\{\); tempJSON = tempJSON.replaceAll(\\},\\\}); tempJSON = tempJSON.replaceAll(=,\:\); tempJSON = tempJSON.replaceAll(, , \,\); tempJSON += ,; sJSONString +=tempJSON } ) } sJSONString = sJSONString.substring(0,sJSONString.length-1); // for slicing the last comma sJSONString += ]}; Text(sJSONString); } but not getting a way of doing this. Please advise. Thanks Amit Kumar Verma On Apr 9, 6:14 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Howdy, Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. However, casting to a known class is easy in Scala... and it's done primarily using pattern matching. The following code: def foo(in: Any) = in match { case mm: MetaMapper[_] = mm.findAll.map( (m: Mapper[_]) = m.asJs ) case _ = } Does what I think you want. It takes an incoming instance, in and matches it against being an instance of MetaMapper[_]. This means its some type of MetaMapper (we don't know or care what the type parameter is). If it is a MetaMapper, it's assigned to the mm variable. We can then call findAll on that variable and we have a bunch of Mapper[_] instances. Note that I explicitly called out the type of m in the function, but that line could be re-written mm.findAll.map(m = m.asJs) because the compiler infers the type of m. Does this help? Thanks, David On Thu, Apr 9, 2009 at 3:55 AM, Amit Kumar Verma cdac.a...@gmail.com wrote: Hi All, I am trying to type cast an scala object to its mapper object 1 def getJSONString(anyObject :Object):NodeSeq = { 2 var obj = anyObject.asInstanceOf[anyObject.getClass ().getName()]; 3 obj.findAll.map(userdetails = { // some code will go here } Text(any string) } but i am getting erroe as expected [ but found ( on line 2. please help me to typecast the object to its mapper object. Thanks Amit Kumar Verma -- Lift, the simply functional web frameworkhttp://liftweb.net Beginning Scalahttp://www.apress.com/book/view/1430219890 Follow me:http://twitter.com/dpp Git some:http://github.com/dpp -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Git some: http://github.com/dpp --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Object typecast to Mapper
Howdy, Scala is a static language, so the class for casting must be known at compile time. It's not possible to construct a String at runtime and cast an object into a class represented by that String. However, casting to a known class is easy in Scala... and it's done primarily using pattern matching. The following code: def foo(in: Any) = in match { case mm: MetaMapper[_] = mm.findAll.map( (m: Mapper[_]) = m.asJs ) case _ = } Does what I think you want. It takes an incoming instance, in and matches it against being an instance of MetaMapper[_]. This means its some type of MetaMapper (we don't know or care what the type parameter is). If it is a MetaMapper, it's assigned to the mm variable. We can then call findAll on that variable and we have a bunch of Mapper[_] instances. Note that I explicitly called out the type of m in the function, but that line could be re-written mm.findAll.map(m = m.asJs) because the compiler infers the type of m. Does this help? Thanks, David On Thu, Apr 9, 2009 at 3:55 AM, Amit Kumar Verma cdac.a...@gmail.comwrote: Hi All, I am trying to type cast an scala object to its mapper object 1 def getJSONString(anyObject :Object):NodeSeq = { 2 var obj = anyObject.asInstanceOf[anyObject.getClass ().getName()]; 3 obj.findAll.map(userdetails = { // some code will go here } Text(any string) } but i am getting erroe as expected [ but found ( on line 2. please help me to typecast the object to its mapper object. Thanks Amit Kumar Verma -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Git some: http://github.com/dpp --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---