Deserialize from json

249 views
Skip to first unread message

boci...@gmail.com

unread,
May 2, 2013, 11:12:37 AM5/2/13
to activate-p...@googlegroups.com
Hi!

How can I deserialize the obejct from json string by hand? Can you show me an example?

Thanks

b0c1

Flavio W. Brasil

unread,
May 2, 2013, 3:14:30 PM5/2/13
to boci...@gmail.com, activate-p...@googlegroups.com
Hi.

It is possible to define custom serializers for serializable entity attributes:


Let me know if this functionality solves your problem.

Regards,

-- 
Flávio W. Brasil

--
You received this message because you are subscribed to the Google Groups "Activate Persistence Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to activate-persist...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

boci

unread,
May 2, 2013, 3:18:31 PM5/2/13
to Flavio W. Brasil, activate-p...@googlegroups.com
Thnx... now Im a little confused.
1. I try to use activate with scalatra but in the test case I put before{ transactional{}} block (delete old data) and I add a servlet (scalatra suite addServlet method) I get initialization exception.

2. I have a simple json with the document (without id) exampe {"name":"test"}. How can I create entiy from this?

Thanks

----------------------------------------------------------------------------------------------------------------------------------
Skype: boci13, GTalk: boci...@gmail.com

Flavio W. Brasil

unread,
May 2, 2013, 3:22:56 PM5/2/13
to boci, activate-p...@googlegroups.com
Hi.

1. Could you send the exception stack trace?

2. Now I understood your question… :) Activate don't have a functionality to transform entities from/to json. It is necessary to do it manually or using a json library.

-- 
Flávio W. Brasil

boci

unread,
May 2, 2013, 3:48:02 PM5/2/13
to Flavio W. Brasil, activate-p...@googlegroups.com

2. Ok here is the problem:
- Scalatra using Json4s for json serialization/deserialization
- Json4s work with case classes
- If I try to using case classes in activate framework, and serialize it, I get Stack overflow
- If I try to serialize not case class, I get Test(id -> fdf4b084-b360-11e2-9b36-5f5259a6b767-26f49600, name -> test) instead of {"id":"fdf4b084-b360-11e2-9b36-5f5259a6b767-26f49600","name":"test"}

- I can't deserialize (It's say "no usable value for name", but my entity only have one property: "name")

my complex entity: class Test(var name: String) extends Entity

Thanks
b0c1

On Thu, May 2, 2013 at 9:22 PM, Flavio W. Brasil <fwbr...@gmail.com> wrote:
Now I understood your question… :) Activate don't have a functionality to transform entities from/to json. It is necessary to do it manually or using a json library.



boci

unread,
May 2, 2013, 7:15:00 PM5/2/13
to Flavio W. Brasil, activate-p...@googlegroups.com
1. Ok I think Im found the problem. I want to create a generic controller, it's have an implicit manifest value, and it's try to do something. If I set it with @transient it's work.

2. The problem still exists. The activate not work with case classes (if I want to deserialize I get this http://pastie.org/private/pfjwj4eajqtz0jhbkpgmaw




----------------------------------------------------------------------------------------------------------------------------------
Skype: boci13, GTalk: boci...@gmail.com


boci

unread,
May 3, 2013, 4:40:49 AM5/3/13
to Flavio W. Brasil, activate-p...@googlegroups.com
Hi, again :)

For the problem 1. I found the root:
I created a small test for reproduce:

If I try to use the Entity manifest in my servlet, the servlet initialization failed.

----------------------------------------------------------------------------------------------------------------------------------
Skype: boci13, GTalk: boci...@gmail.com


Flavio W. Brasil

unread,
May 4, 2013, 11:51:18 PM5/4/13
to boci, activate-p...@googlegroups.com
Hi.

1. The entity class is being loaded before the persistence context by the Manifest. To solve, please add an empty transaction to the startup code ("transactional{}"). I also added a better error message to this case. Gist with the solution:


2. I implemented a new module to serialize entities from/to json using json4s. Could you validate if the available methods solves your necessity? Steps to use the new module:

- add artifact "activate-json4s" version "1.3-SNAPSHOT" to the sbt build. The artifact is in my repository: http://fwbrasil.net/maven.
- extend the persistence context from Json4sContext and implement the jsonMethods value:

object h2Context extends ActivateContext with Json4sContext {

    val storage = new TransientMemoryStorage

    protected val jsonMethods = org.json4s.native.JsonMethods

}

- available methods:

anEntityInstance.toJson
anEntityInstance.updateFromJson(jsonString)
createEntityFromJson[SomeEntityClass](jsonString)
createOrUpdateEntityFromJson[SomeEntityClass](jsonString)

- For the last method, if the json has an ID field, the entity is update. If there isn't an id field, a new entity is created.

-- 
Flávio W. Brasil

boci

unread,
May 5, 2013, 2:22:31 AM5/5/13
to Flavio W. Brasil, activate-p...@googlegroups.com
Hi!

1. Thnx, but I think, I call it at the end of ActivateContext ... (It's have disatvantages? Maybe the ActivateContext can pull up the entities...)

2. I not found these artifact:  "net.fwbrasil" %% "activate-json4s" % "1.3-SNAPSHOT"

Thnx


----------------------------------------------------------------------------------------------------------------------------------
Skype: boci13, GTalk: boci...@gmail.com


Flavio W. Brasil

unread,
May 5, 2013, 6:58:37 AM5/5/13
to boci, activate-p...@googlegroups.com
1. The persistence context class must be loaded before the entities classes to enhance the entities classes. The empty transactional at the start of the test ensures that the context is loaded before the Test class (loaded by the Manifest).

2. Sorry, my mistake while doing the release. Please try again.

-- 
Flávio W. Brasil

Flavio W. Brasil

unread,
May 5, 2013, 7:22:45 AM5/5/13
to boci, activate-p...@googlegroups.com
1. Another option is to move the line "addServlet(new ManifestExample[Test](), "/*")" to after the transactional block inside the "before":


-- 
Flávio W. Brasil

Reply all
Reply to author
Forward
0 new messages