Embedded Collections

43 views
Skip to first unread message

Emanuele Ziglioli

unread,
Oct 31, 2012, 11:41:09 PM10/31/12
to siena-...@googlegroups.com
Hi everyone,

one never stops learning... 
I've been using more and more embedded objects and collections with JSON serialization.
Some observations:
1) Arrays of Objects can be serialized but they fail to deserialize. Solution: use List instead of []
2) embedded collections can be persisted but if a generic type is set, they fail to deserialize. Example:
  public Map<String, List> map1; // this works
  public Map<String, List<String> map2; // this fails

The problem is in JsonSerializer, line 323:
map.put(key, deserialize(Util.getGenericClass(f, 1), data.get(key)));

In Util.getGenericClass(), this call here returns an array of types:
   Type[] fieldArgTypes = aType.getActualTypeArguments();

If the generic type is added, the returned type is no longer a Class<?> but instead a ParametrizedTypeImpl.
I'm sure this thing can be fixed, but I can't be bothered for now.

Also, since I then re-serialize to JSON, it'd be nice to do something like Pascal does:
but without using Scala :-)

By the way, I've imported the history of Siena before GitHub from GoogleCode and I've managed to merge it locally.
The problem is that the tree structure doesn't match, so it's a pretty useless excercise.
I've tried to commit a change to the tree structure back in time (before the first GitHub commit) but that looks impossibile (I can change the authoring data but not the commit date). It'd be nice to have a full history of this project. If you have any suggestions, please let me know.


Emanuele Ziglioli

unread,
Nov 1, 2012, 4:56:11 AM11/1/12
to siena-...@googlegroups.com

I was wrong...
 
  public Map<String, List> map1; // this works

the definition above doesn't work either: my map is serialized correctly, but when it gets to deserialize the JSON list, it returns a list of Null instead of a List of Strings.

See in siena.embed.JsonSerializer.deserialize(Class<?> clazz, Json data)
with clazz being a java.util.List and data being a Json arraylist of strings.

The method deserializePlain() that's called at the end doesn't know what to do with data and returns null.
Perhaps I can add a JsonDeserializeAs annotation.

Emanuele Ziglioli

unread,
Nov 1, 2012, 6:32:13 AM11/1/12
to siena-...@googlegroups.com
Hello again,

I've done some little changes to JsonSerializer, just added a case in deserialize(Class<?> clazz, Json data) for handling a collection of Strings.

The interesting bit for me is that it doesn't really matter specifying the generic type because due to type erasure, that won't be available at runtime. Not sure what ParametrizedTypeImpl is what is for.

While iterating over the Json List, the object type could be retrieved but the embedded Object is private so there's no elegant way of doing it.
I'd like to handle arrays too.

I've also noticed that one could also specify the type to be "Json", in that case the json data doesn't get deserialized into a Java object.
That opens some possibilities to due Json to Json transformation, a bit like in Pascal's article.
Food for Java thoughts :-)
JsonSerializer.java

Pascal Voitot Dev

unread,
Nov 1, 2012, 7:11:55 AM11/1/12
to siena-...@googlegroups.com
Hi Emanuele,
I'm glad you liked my last article ;)
Lots of new ideas, new concepts ;)

I agree with you that it would be cool to have the same in Java but it would require lots of runtime manipulation... Don't think I do it in Scala just because I love Scala, it's also for real technical reasons (mainly linked to functional concepts) ;)

About your code modifs, you could create a branch on github with your modifs so that it's easier to see the code differences vs master.

Pascal

--
You received this message because you are subscribed to the Google Groups "Siena" group.
To view this discussion on the web visit https://groups.google.com/d/msg/siena-discuss/-/RN68H2KjIYIJ.

To post to this group, send email to siena-...@googlegroups.com.
To unsubscribe from this group, send email to siena-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/siena-discuss?hl=en.

Emanuele Ziglioli

unread,
Nov 1, 2012, 3:09:34 PM11/1/12
to siena-...@googlegroups.com

About your code modifs, you could create a branch on github with your modifs so that it's easier to see the code differences vs master.


Will do, it was 11.30pm and couldn't be bothered stashing and forking my local branch :-))

Reply all
Reply to author
Forward
0 new messages