bendroid
bendroid

Reputation: 1

Android Jsonwriter writes 0 instead of my value into the file

I have a Jsonwriter which is writing into a file. I want to save all of my Book objects which have id, title, author, releasedate, type, description and link. I still get 0 at the releaseDate name, but in the array there is the right value.

JsonWriter writer = new JsonWriter(new OutputStreamWriter
                (this.openFileOutput("books.json",Context.MODE_PRIVATE),"ISO-8859-2"));
        writer.beginArray();
        for(Book b : MainActivity.books){
            writer.beginObject();
            writer.name("id").value(b.getId());
            writer.name("title").value(b.getTitle());
            writer.name("author").value(b.getAuthor());
            writer.name("releaseDate").value(b.getReleaseDate());
            writer.name("type").value(b.getType());
            writer.name("description").value(b.getDescription());
            writer.name("link").value(b.getLink());
            writer.endObject();
        }
        writer.endArray();
        writer.close();

public class Book implements Parcelable, Comparable{

public int id;
public String title;
public String author;
public String type;
public int releaseDate;
public String description;
public String link;
public int isfav;

public static final int OBTITLE = 1;
public static final int OBAUTHOR = 2;
public static final int OBRDATE = 3;

public static int orderby = OBTITLE;

public static int getOrderby() {
    return orderby;
}

public static void setOrderby(int orderby) {
    Book.orderby = orderby;
}

public Book(int id, String title, String author, String type, int releaseDate, String description, String link) {
    this.id = id;
    this.title = title;
    this.author = author;
    this.type = type;
    this.releaseDate = releaseDate;
    this.description = description;
    this.link = link;
    this.isfav = 0;
}


public Book(Parcel in) {
    this.id = Integer.parseInt(in.readString());
    this.title = in.readString();
    this.author = in.readString();
    this.type = in.readString();
    this.releaseDate = Integer.parseInt(in.readString());
    this.description = in.readString();
    this.link = in.readString();
}


public String getTitle() {
    return title;
}

public String getAuthor() {
    return author;
}

public int getId() {
    return id;
}

public int getIsfav() {
    return isfav;
}

public String getType() {
    return type;
}

public int getReleaseDate() {
    return releaseDate;
}

public String getDescription() {
    return description;
}

public String getLink() {
    return link;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(String.valueOf(id));
    dest.writeString(title);
    dest.writeString(author);
    dest.writeString(type);
    dest.writeString(String.valueOf(releaseDate));
    dest.writeString(description);
    dest.writeString(link);
}

public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
     public Book createFromParcel(Parcel in) {
         return new Book(in);
     }

     public Book[] newArray(int size) {
         return new Book[size];
     }
};

@Override
public int compareTo(Book another) {
    if(orderby == 2){
        return this.getAuthor().compareTo(another.getAuthor());
    } else if(orderby == 3){
        return this.getReleaseDate() - another.getReleaseDate();
    } else{
        return this.getTitle().compareTo(another.getTitle());
    }
}

}

public ArrayList<Book> beolvas(JsonReader reader) {
    try {
        ArrayList<Book> books = new ArrayList<Book>();
        Book book;
        String name;
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();
            book = new Book(0, null, null, null, 0, null, null);
            while (reader.hasNext()) {
                name = reader.nextName();
                if (name.equals("id")) {
                    book.id = reader.nextInt();
                } else if (name.equals("title")) {
                    book.title = reader.nextString();
                } else if (name.equals("author")) {
                    book.author = reader.nextString();
                } else if (name.equals("releasedate")) {
                    book.releaseDate = Integer.parseInt(reader.nextString());
                } else if (name.equals("type")) {
                    book.type = reader.nextString();
                } else if (name.equals("description")) {
                    book.description = reader.nextString();
                } else {
                    book.link = reader.nextString();
                }
            }
            reader.endObject();
            books.add(book);
        }
        reader.endArray();
        reader.close();
        return books;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

Upvotes: 0

Views: 193

Answers (3)

bendroid
bendroid

Reputation: 1

I missed a little mistake in my write and read method. The releasedate has D in the write method and that is why the reader reads 0 from the file.

Upvotes: 0

nikis
nikis

Reputation: 11244

Field releaseDate at the Book class is a type of int. Please try to wrap it's value with String.valueOf():

writer.name("releaseDate").value(String.valueOf(b.getReleaseDate()));

Upvotes: 0

andMarkus
andMarkus

Reputation: 1020

Change to this:

writer.name("releaseDate").value(Integer.toString(b.getReleaseDate()));

Upvotes: 1

Related Questions