Re: [Simple-support] Custom HashMap serialization/deserialization - Help
Brought to you by:
niallg
|
From: Simon B. <sim...@if...> - 2012-05-28 18:53:40
|
Hi Miguel,
Sorry, I was a little bit to fast. Didn't realize the Map:
So new try:
public class Courses {
@ElementList(entry=Course,type=com.simpleserializer.model.Course.class,
inline=true)
java.util.Collection<Course> getCourses(){
return courses.values();
}
@ElementList(entry=Course,type=com.simpleserializer.model.Course.class,
inline=true)
void setCourses(java.util.Collection<Course> courses){
for(Course course: courses){
this.put(course);
}
}
private HashMap<String, Course> courses = new
HashMap<String, Course>();
public void put(Course course)
{
this.courses.put(course.getId(), course);
}
}
> Hi Simon!
>
> Thank you for your quick answer.
>
> Unfortunately it did not work. When I try this:
>
> // Retrieving courses from ddbb
> Courses courses = CoursesDAO.get(token, connection);
>
> // Serialization to xml
> String xml = Serializer.toXml(courses);
>
> It raises the following exception:
>
> java.util.HashMap cannot be cast to java.util.Collection
>
> The rest of the code below:
>
> Serializer.java:
>
> package com.simpleserializer.util;
>
> import java.io.StringWriter;
>
> import org.simpleframework.xml.core.Persister;
>
> public class Serializer {
> public static String toXml(Object source) {
> StringWriter response = new StringWriter();
>
> try {
> new Persister().write(source, response);
> } catch (Exception e) {
> e1.printStackTrace();
> }
>
> return response.toString();
> }
> }
>
>
> On Mon, May 28, 2012 at 12:12 PM, Simon Brodt <sim...@if...>
> wrote:
>> Hi Miguel,
>>
>>
>>
>> Try the following:
>>
>>
>>
>> Course.java:
>>
>> package com.simpleserializer.model;
>>
>>
>>
>> import org.simpleframework.xml.Attribute;
>>
>>
>>
>> @Root(name=Course)
>>
>> public class Course {
>>
>> @Attribute private String id;
>>
>> @Attribute private String name;
>>
>>
>>
>> public Course(String id, String name) {
>>
>> this.setId(id);
>>
>> this.setName(name);
>>
>> }
>>
>>
>>
>> public String getId() {
>>
>> return this.id;
>>
>> }
>>
>>
>>
>> public void setId(String id) {
>>
>> this.id = id;
>>
>> }
>>
>>
>>
>> public String getName() {
>>
>> return this.name;
>>
>> }
>>
>>
>>
>> public void setName(String name) {
>>
>> this.name = name;
>>
>> }
>>
>> }
>>
>>
>>
>> Course2.java:
>>
>> package com.simpleserializer.model;
>>
>>
>>
>> import java.util.HashMap;
>>
>>
>>
>> @Root(name=Courses)
>>
>> public class Courses {
>>
>>
>>
>> @ElementList(entry=Course,type=
>> com.simpleserializer.model.Course.class, inline=true)
>>
>> private HashMap<String, Course> courses = new
>> HashMap<String, Course>();
>>
>>
>>
>> public void put(String courseId, Course course)
>>
>> {
>>
>> this.courses.put(courseId, course);
>>
>> }
>>
>> }
>>
>>
>>
>> Von: Miguel Ángel Martínez Fernández [mailto:mig...@gm...]
>> Gesendet: Montag, 28. Mai 2012 01:35
>> An: sim...@li...
>> Betreff: [Simple-support] Custom HashMap serialization/deserialization -
>> Help
>>
>>
>>
>> Hi,
>>
>>
>>
>> I am trying to serialize (and deserialize) two Java clases
>> (Courses.java
>> and Couse.Java) in order to get the following XML:
>>
>>
>>
>> <Courses>
>>
>> <Course id="course01" name="My first course"/>
>>
>> <Course id="course02" name="My second course"/>
>>
>> </Courses>
>>
>>
>>
>> Course.java:
>>
>> package com.simpleserializer.model;
>>
>>
>>
>> import org.simpleframework.xml.Attribute;
>>
>>
>>
>> public class Course {
>>
>> @Attribute private String id;
>>
>> @Attribute private String name;
>>
>>
>>
>> public Course(String id, String name) {
>>
>> this.setId(id);
>>
>> this.setName(name);
>>
>> }
>>
>>
>>
>> public String getId() {
>>
>> return this.id;
>>
>> }
>>
>>
>>
>> public void setId(String id) {
>>
>> this.id = id;
>>
>> }
>>
>>
>>
>> public String getName() {
>>
>> return this.name;
>>
>> }
>>
>>
>>
>> public void setName(String name) {
>>
>> this.name = name;
>>
>> }
>>
>> }
>>
>>
>>
>> Course2.java:
>>
>> package com.simpleserializer.model;
>>
>>
>>
>> import java.util.HashMap;
>>
>>
>>
>> public class Courses {
>>
>> private HashMap<String, Course> courses = new
>> HashMap<String,
>> Course>();
>>
>>
>>
>> public void put(String courseId, Course course)
>>
>> {
>>
>> this.courses.put(courseId, course);
>>
>> }
>>
>> }
>>
>>
>>
>> An example about how these clases are used is:
>>
>>
>>
>> Courses courses = new Courses();
>>
>>
>>
>> Course course1 = new Course("course01", "My first course");
>>
>> Course course2 = new Course("course02", "My second course");
>>
>>
>>
>> courses.put(course1.getId(), course1);
>>
>> courses.put(course2.getId(), course2);
>>
>>
>>
>>
>>
>> Could you please help me? I would really appreciate it!
>>
>>
>>
>> Thanks!
>
|