Quantcast

Mapper <-> JObject bridge

classic Classic list List threaded Threaded
6 messages Options
dpp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Mapper <-> JObject bridge

dpp
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

--

You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mapper <-> JObject bridge

exmbly
David,

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

Glenn

On Dec 2, 1:15 pm, David Pollak <[hidden email]> 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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.


dpp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Mapper <-> JObject bridge

dpp
On the dpp_issue_213 branch

On Mon, Dec 7, 2009 at 1:34 PM, glenn <[hidden email]> wrote:
David,

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

Glenn

On Dec 2, 1:15 pm, David Pollak <[hidden email]> 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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
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
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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mapper <-> JObject bridge

Xuefeng Wu
In reply to this post by dpp
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 <[hidden email]> 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 framework http://liftweb.net
Beginning Scala http://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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mapper <-> JObject bridge

Joni Freeman-3
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 <[hidden email]> 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
> <[hidden email]>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 [hidden email].
> > To unsubscribe from this group, send email to
> > [hidden email]<liftweb%[hidden email]>
> > .
> > 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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Mapper <-> JObject bridge

Xuefeng Wu
Thanks, Joni.

On Wed, Dec 16, 2009 at 4:24 PM, Joni Freeman <[hidden email]> wrote:
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 <[hidden email]> 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
> <[hidden email]>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 [hidden email].
> > To unsubscribe from this group, send email to
> > [hidden email]<[hidden email]>
> > .
> > 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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
Loading...