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 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 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 -~----------~----~----~----~------~----~------~--~---
json.tgz
Description: GNU Zip compressed data