Mapper <-> JObject bridge

29 views
Skip to first unread message

David Pollak

unread,
Dec 2, 2009, 4:15:21 PM12/2/09
to liftweb
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 framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

glenn

unread,
Dec 7, 2009, 4:34:17 PM12/7/09
to Lift
David,

I couldn't find encodeAsJSON_! on MetaMapper in the Lift source on
github.
Where is this code checked in?

Glenn
> Beginning Scalahttp://www.apress.com/book/view/1430219890

David Pollak

unread,
Dec 7, 2009, 4:37:15 PM12/7/09
to lif...@googlegroups.com
On the dpp_issue_213 branch

--

You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.





--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

Xuefeng Wu

unread,
Dec 16, 2009, 2:03:56 AM12/16/09
to liftweb
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?

 

--

You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Joni Freeman

unread,
Dec 16, 2009, 3:24:50 AM12/16/09
to Lift
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:
> > 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 lif...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com>
> > .

Xuefeng Wu

unread,
Dec 16, 2009, 3:28:53 AM12/16/09
to liftweb
Thanks, Joni.

To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.





--
Scala中文社区:  http://groups.google.com/group/scalacn
Reply all
Reply to author
Forward
0 new messages