loldude
loldude

Reputation: 3

Parsing json to pojo, which consist of lists, Using GSON

I've got a problem when I try to parse a json string into my custom class, which consists of a list of another pojo.

the error I get is:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 226

And the class I try to parse is:

package com.example.client.models;

import java.util.List;

public class Catalog {

private List<Artist> list;

public Catalog() {  }

/**
 * @param list
 */
public Catalog(List<Artist> list) {
    super();
    this.list = list;
}

/**
 * @return the list
 */
public List<Artist> getList() {
    return list;
}

/**
 * @param list the list to set
 */
public void setList(List<Artist> list) {
    this.list = list;
}
}

And the artist class:

package com.example.client.models;

import java.io.File;
import java.util.List;

public class Artist {

private String artistName;
private List<Album> albumList;
private File artistFile;

public Artist() {   }

/**
 * @param artistName
 * @param albumList
 * @param artistFile
 */
public Artist(String artistName, List<Album> albumList, File artistFile) {
    super();
    this.artistName = artistName;
    this.albumList = albumList;
    this.artistFile = artistFile;
}

/**
 * @return the artistName
 */
public String getArtistName() {
    return artistName;
}

/**
 * @param artistName the artistName to set
 */
public void setArtistName(String artistName) {
    this.artistName = artistName;
}

/**
 * @return the albumList
 */
public List<Album> getAlbumList() {
    return albumList;
}

/**
 * @param albumList the albumList to set
 */
public void setAlbumList(List<Album> albumList) {
    this.albumList = albumList;
}

/**
 * @return the artistFile
 */
public File getArtistFile() {
    return artistFile;
}

/**
 * @param artistFile the artistFile to set
 */
public void setArtistFile(File artistFile) {
    this.artistFile = artistFile;
}


 }

The Album class: package com.example.client.models;

import java.io.File;
import java.util.List;

public class Album {

private String albumName;
private List<Song> songList;
private File albumFile;

public Album() {    }

/**
 * @param albumName
 * @param songList
 * @param albumFile
 */
public Album(String albumName, List<Song> songList, File albumFile) {
    super();
    this.albumName = albumName;
    this.songList = songList;
    this.albumFile = albumFile;
}

/**
 * @return the albumName
 */
public String getAlbumName() {
    return albumName;
}

/**
 * @param albumName the albumName to set
 */
public void setAlbumName(String albumName) {
    this.albumName = albumName;
}

/**
 * @return the songList
 */
public List<Song> getSongList() {
    return songList;
}

/**
 * @param songList the songList to set
 */
public void setSongList(List<Song> songList) {
    this.songList = songList;
}

/**
 * @return the albumFile
 */
public File getAlbumFile() {
    return albumFile;
}

/**
 * @param albumFile the albumFile to set
 */
public void setAlbumFile(File albumFile) {
    this.albumFile = albumFile;
}


}

and finally the song class:

package com.example.client.models;

import java.io.File;

public class Song {

private String songName;
private File songFile;

public Song() { }

/**
 * @param songName
 * @param songFile
 */
public Song(String songName, File songFile) {
    super();
    this.songName = songName;
    this.songFile = songFile;
}

/**
 * @return the songName
 */
public String getSongName() {
    return songName;
}

/**
 * @param songName the songName to set
 */
public void setSongName(String songName) {
    this.songName = songName;
}

/**
 * @return the songFile
 */
public File getSongFile() {
    return songFile;
}

/**
 * @param songFile the songFile to set
 */
public void setSongFile(File songFile) {
    this.songFile = songFile;
}


}

I already have validated the json string, therefore I guess my problem lays in the GSON parsing. I hope you can help me with my problem

It would be nice if you could give an example or a hint of what to do. Thanks on beforehand! ;)

EDIT

Hey again I've included a sample of the JSON string i'm using:

{
 "list":[
  {
     "artistName":"tmpArtistName",
     "albumList":[
        {
           "albumName":"tmpAlbumName",
           "songList":[
              {
                 "songName":"tmpSongName",
                 "songFile":"/home/pi/tmpSongFile"
              },
              {
                 "songName":"tmpSongName",
                 "songFile":"/home/pi/tmpSongFile"
              }
           ],
           "albumFile":"/home/pi/tmpAlbumFile"
        },
        {
           "albumName":"tmpAlbumName",
           "songList":[
              {
                 "songName":"tmpSongName",
                 "songFile":"/home/pi/tmpSongFile"
              },
              {
                 "songName":"tmpSongName",
                 "songFile":"/home/pi/tmpSongFile"
              }
           ],
           "albumFile":"/home/pi/tmpAlbumFile"
        }
     ],
     "artistFile":"/home/pi/tmpArtistFile"
  },
  {
     "artistName":"tmpArtistName",
     "albumList":[
        {
           "albumName":"tmpAlbumName",
           "songList":[
              {
                 "songName":"tmpSongName",
                 "songFile":"/home/pi/tmpSongFile"
              },
              {
                 "songName":"tmpSongName",
                 "songFile":"/home/pi/tmpSongFile"
              }
           ],
           "albumFile":"/home/pi/tmpAlbumFile"
        },
        {
           "albumName":"tmpAlbumName",
           "songList":[
              {
                 "songName":"tmpSongName",
                 "songFile":"/home/pi/tmpSongFile"
              },
              {
                 "songName":"tmpSongName",
                 "songFile":"/home/pi/tmpSongFile"
              }
           ],
           "albumFile":"/home/pi/tmpAlbumFile"
        }
     ],
     "artistFile":"/home/pi/tmpArtistFile"
  }
]
}

Upvotes: 0

Views: 747

Answers (1)

Perception
Perception

Reputation: 80623

The Gson parser is apparently unable to deserialize strings directly into File objects. Instead it expects a matching JSON object (surrounded by { and }) in order to proceed with the unmarshalling. In order to get around this, you can simply register a type adapter to handle the file attributes:

public class FileTypeAdapter extends TypeAdapter<File> {

    @Override
    public void write(final JsonWriter out, final File value)
            throws IOException {
        if (value == null) {
            out.nullValue();
        } else {
            out.value(value.getAbsolutePath());
        }
    }

    @Override
    public File read(final JsonReader in) throws IOException {
        if (in.hasNext()) {
            final String name = in.nextString();
            return name != null ? new File(name) : null;
        }

        return null;
    }
}

To use this, the rest of your code would remain the same, but replace the creation of your Gson parser:

final Gson gson = new Gson();

With this:

final Gson gson = new GsonBuilder().registerTypeAdapter(File.class,
    new FileTypeAdapter()).create();

That should fix the issue.


On a side note, the Jackson library can handle these kind of deserializations with no special customization, if you are inclined and able to switch.

Upvotes: 1

Related Questions