youxinren / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

How to do ArrayList with custom object serialization #101

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a class name News

public class News{
 public String title ="",description ="",link="";
}

And I need to save and retrieve ArrayList<News> from and to a file
I have done the following as shown in your example
    ArrayList<News> newsList;
    Kryo kryo = new Kryo();
    CollectionSerializer listSerializer = new CollectionSerializer();                               listSerializer.setElementClass(News.class,new Serializer<News>() {
    @Override
    public News read(Kryo kryo, Input input,              Class<News> someClass) {
    News news = new News();
    news.title = input.readString();
    news.link =input.readString();
    news.category =input.readString();
    return news;
     }

     @Override
     public void write(Kryo kryo, Output output, News news) {                                                        
     output.writeString(news.title);                              
     output.writeString(news.link);                         
     output.writeString(news.category);                         
    }
     });
    listSerializer.setElementsCanBeNull(false);
        kryo.register(ArrayList.class, listSerializer);                   

    Output output =new Output(new FileOutputStream("hello.txt"));
    kryo.writeObject(output, newsList);
    output.close();

But I couldn't get the required output when I look upon the file hello.txt and 
further I don't know how to read the objects from same file hello.txt

Thanks

Original issue reported on code.google.com by sant...@braindigit.com on 22 Dec 2012 at 6:43

GoogleCodeExporter commented 9 years ago
Further I am using Kryo for Android app,so how could I efficiently use this 
library to store and retrieve ArrayList with custom objects

Original comment by sant...@braindigit.com on 22 Dec 2012 at 7:20

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Further I continuously get this 

12-22 13:10:55.484: E/dalvikvm(18579): Could not find class 
'com.esotericsoftware.reflectasm.FieldAccess', referenced from method 
com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read
12-22 13:10:55.484: W/dalvikvm(18579): VFY: unable to resolve check-cast 1226 
(Lcom/esotericsoftware/reflectasm/FieldAccess;) in 
Lcom/esotericsoftware/kryo/serializers/FieldSerializer$ObjectField;

Original comment by sant...@braindigit.com on 22 Dec 2012 at 7:27

GoogleCodeExporter commented 9 years ago
You don't need to do anything with serializers. Just give Kryo your 
ArrayList<News> and it will do the right thing. Otherwise, read the docs and 
look at the tests.

Please use the mailing list for help using the library. The issue tracker is 
for bugs.

Original comment by nathan.s...@gmail.com on 22 Dec 2012 at 8:27

GoogleCodeExporter commented 9 years ago
Sorry,
         I didn't know that.I will look upon the mailing list and FOI the method you suggested always returns null while reading the list. 
Anyway I will follow up the mailing list

Thanks again

Original comment by sant...@braindigit.com on 22 Dec 2012 at 9:21