Hi,

Yes, that's a way to convert JSON AST to string and vice versa. All
functions in lift-json operate on AST instances. This is lets us post-
and pre-process JSON in many ways before converting it to string. See
for instance functions map, merge, diff, \, etc. defined in JsonAST
(scaladocs still missing but will be provided before Lift 2.0 is
released). Typical conversions might for instance remove some data
from JSON, convert data types, change structure of resulting JSON and
so on.

I usually import all needed objects and their members. After that the
code becomes a bit more concise (the cast is unfortunately needed
since JSON comes from the wild and can be anything):

def encodeAsJsonString(in: A) = compact(render(encodeAsJson(in)))
def buildFromJsonString(json: String): A = buildFromJson(parse
(json).asInstanceOf[JObject])

Cheers Joni


On 16 joulu, 09:03, Xuefeng Wu <ben...@gmail.com> wrote:
> Hi,
> I add two method to en/decode JObject/String
>
> def encodeAsJsonString(in: A): String =
> Printer.compact(JsonAST.render(encodeAsJson(in)))
> def buildFromJsonString(json: String): A =
> buildFromJson(JsonParser.parse(json).asInstanceOf[JsonAST.JObject])
>
> Do there have better way?
>
> On Thu, Dec 3, 2009 at 5:15 AM, David Pollak
> <feeder.of.the.be...@gmail.com>wrote:
>
>
>
> > Folks (HarryH -- this means you),
>
> > I've just checked in code on the dpp_issue_213 that does Mapper <-> JObject
> > bridging using the awesome lift-json library.
>
> > The methods on MetaMapper:
> > protected def encodeAsJSON_! (toEncode: A): JsonAST.JObject
> > protected def decodeFromJSON_!(json: JsonAST.JObject): A
>
> > Implement the bridge.  They are protected and have a _! in their name
> > because they are *dangerous* in that data can be exposed on the JSON object
> > that you might not want exposed and these methods should be used with
> > extreme caution.
>
> > An example of usage can be found in the MapperSpecs:
>
> > object SampleModel extends SampleModel with KeyedMetaMapper[Long,
> > SampleModel] {
> >   def encodeAsJson(in: SampleModel): JsonAST.JObject = encodeAsJSON_!(in)
> >   def buildFromJson(json: JsonAST.JObject): SampleModel =
> > decodeFromJSON_!(json)
> > }
>
> > class SampleModel extends KeyedMapper[Long, SampleModel] {
> >   def getSingleton = SampleModel // what's the "meta" server
> >   def primaryKeyField = id
>
> >   object id extends MappedLongIndex(this)
> >   object firstName extends MappedString(this, 32)
> >   object moose extends MappedNullableLong(this)
> >   object notNull extends MappedString(this, 32) {
> >     override def dbNotNull_? = true
> >   }
>
> >   def encodeAsJson(): JsonAST.JObject = SampleModel.encodeAsJson(this)
> > }
>
> > So, you can use this mechanism to serialize a Mapper object to JSON, shovel
> > the object into memcached and then pull it out, mutate a field and save the
> > object back to the database (although connection identifier is lost, so if
> > you are sharding your database, this will not work).
>
> > Please give it a try, give me feedback.  I'll put it on review board
> > tomorrow after any feedback and get it into Lift.
>
> > Thanks,
>
> > David
>
> > --
> > Lift, the simply functional web frameworkhttp://liftweb.net
> > Beginning Scalahttp://www.apress.com/book/view/1430219890
> > Follow me:http://twitter.com/dpp
> > Surf the harmonics
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Lift" group.
> > To post to this group, send email to lift...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > liftweb+unsubscr...@googlegroups.com<liftweb%2bunsubscr...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/liftweb?hl=en.
>
> --
> Scala中文社区:  http://groups.google.com/group/scalacn

--

You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to lift...@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.


Reply via email to