Bidirectional Record - JSON conversion

30 views
Skip to first unread message

Marius

unread,
Mar 29, 2009, 8:07:02 AM3/29/09
to Lift
Folks,

I just committed the Record/JSON conversion support.

Here is a simple example:

import net.liftweb.record.field._
import java.util._

object Main {
def main(args : Array[String]) : Unit = {

val rec = MyRecordMeta.createRecord.firstName("McLoud")

// Change an existing record with the values from a given json
val json = "{'firstName': 'Johnson', 'birthDay': 'Sun, 29 Mar 2009
11:41:53 UTC', 'cars': 3, 'lastName': 'Doe'}"
rec.fromJSON(json)
println(rec.birthDay.value.getTime)
println(rec.firstName)
println(rec.cars)


// Construct a record out of the json(opne_! is used only for
exemplification :)...)
val rec2 = MyRecordMeta.createRecord(json).open_!
println(rec2.firstName)
println(rec2.birthDay.value.getTime)
}
}

class MyRecord extends Record[MyRecord] {
def meta = MyRecordMeta

object firstName extends StringField(this, "Smith")
object lastName extends StringField(this, "John")
object birthDay extends DateTimeField(this, Calendar.getInstance);
object cars extends IntField(this, 1);
}

object MyRecordMeta extends MyRecord with MetaRecord[MyRecord] {
override def mutable_? = false
}



Br's,
Marius

Derek Chen-Becker

unread,
Mar 29, 2009, 8:59:35 AM3/29/09
to lif...@googlegroups.com
Very nice :)

Timothy Perrett

unread,
Mar 29, 2009, 4:29:00 PM3/29/09
to Lift
@marius, this is slick.

However, its causing my build to error now... I get the following
stack trace when building the entire lift project:

[INFO] Compiling 22 source files to /Users/timperrett/repositories/
lift/lift-framework/lift-record/target/classes
[WARNING] /Users/timperrett/repositories/lift/lift-framework/lift-
record/src/main/scala/net/liftweb/record/MetaRecord.scala:238: error:
type mismatch;
[WARNING] found : net.liftweb.util.Box[net.liftweb.util.Box
[net.liftweb.record.OwnedField[BaseRecord]#MyType]]
[WARNING] required: Iterable[?]
[WARNING] Note that implicit conversions are not applicable because
they are ambiguous:
[WARNING] both method box2Iterable in object Box of type [T]
(net.liftweb.util.Box[T])Iterable[T]
[WARNING] and method box2Iterable in object Box of type [T]
(net.liftweb.util.Box[T])Iterable[T]
[WARNING] are possible conversion functions from net.liftweb.util.Box
[net.liftweb.util.Box[net.liftweb.record.OwnedField[BaseRecord]
#MyType]] to Iterable[?]
[WARNING] field <- inst.fieldByName(k.toString)) yield
field.setFromAny(v)
[WARNING] ^
[WARNING] one error found

On Mar 29, 1:59 pm, Derek Chen-Becker <dchenbec...@gmail.com> wrote:
> Very nice :)
>

Timothy Perrett

unread,
Mar 29, 2009, 4:34:03 PM3/29/09
to Lift
FYI - the job is also failing on Hudson

http://scala-tools.org/hudson/job/lift/926/

marius d.

unread,
Mar 29, 2009, 4:45:18 PM3/29/09
to Lift
Apologies ... fixing it now.

marius d.

unread,
Mar 29, 2009, 4:56:37 PM3/29/09
to Lift
Done ...

The Box's implicits were not visible. Funny it was working on my
Eclipse plugin 2.8.0 ...

Sorry again and thanks for pointing it out !

Br's,
Marius

Timothy Perrett

unread,
Mar 29, 2009, 5:16:29 PM3/29/09
to Lift Google Group
That's strange about the eclipse plugin though for sure - I guess
that's IDE voodoo for you!

Cheers Marius

Tim

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages