Nils-o-mat
Nils-o-mat

Reputation: 1256

Gson doesn't read pretty printed JSON

I use gson (Version 2.3.1 in Java 1.7) to serialize an object. Though it does work most of the time, deserializing a pretty printed String fails with the following Exception:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 2 column 1 path $.
    at com.google.gson.Gson.fromJson(Gson.java:825)
    at de.ais.fileexchange.interfaceimplementations.FEProcess.load(FEProcess.java:70)

My Java-Code:

public static IFEProcess load(File file) throws FileNotFoundException, IOException, ClassNotFoundException
{
    Gson gson = new Gson();
    String json = FileHelper.getStringFromFile(file);

    JsonReader reader = new JsonReader(new StringReader(json));
    reader.setLenient(true);

    return gson.fromJson(reader, FEProcess.class);
}

public static void save(File file, IFEProcess process) throws IOException
{
    //Gson gson = new Gson();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(process);

    FileHelper.writeStringToFile(json, file);
}

The JSON-String:

{
  "description": "",
  "altMailAdress": "dfgsfsdg",
  "mailOnError": true,
  "stopOnError": false,
  "operationList": [
    {
      "lConfig": {
        "password": "",
        "username": "",
        "port": 1212,
        "additionalLoginInformations": "",
        "hostname": "",
        "isToRemoteTransfer": false,
        "domain": "",
        "sambaShare": ""
      },
      "operationConfig": {
        "remoteDirectory": "",
        "localDirectory": "321",
        "sourceSemaphoreSuffix": "",
        "sourceSemaphoreType": "NONE",
        "targetSemaphoreSuffix": "",
        "targetSemaphoreType": "NONE",
        "sourceFilePattern": "ztju675",
        "lock": false,
        "delete": false,
        "backup": true,
        "noOverwrite": false,
        "fileRenameRegex": "ztju675",
        "fileRenameContent": ""
      },
      "fileOperationType": "FILESYSTEM",
      "fopConfiguration": {
        "fopPath": "",
        "fopTime": "NONE"
      }
    },
    {
      "lConfig": {
        "password": "",
        "username": "",
        "port": 0,
        "additionalLoginInformations": "",
        "hostname": "",
        "isToRemoteTransfer": false,
        "domain": "",
        "sambaShare": ""
      },
      "operationConfig": {
        "remoteDirectory": "",
        "localDirectory": "",
        "sourceSemaphoreSuffix": "",
        "sourceSemaphoreType": "NONE",
        "targetSemaphoreSuffix": "",
        "targetSemaphoreType": "NONE",
        "sourceFilePattern": "fdsfas",
        "lock": false,
        "delete": true,
        "backup": false,
        "noOverwrite": false,
        "fileRenameRegex": "fdsfas",
        "fileRenameContent": ""
      },
      "fileOperationType": "FILESYSTEM",
      "fopConfiguration": {
        "fopPath": "",
        "fopTime": "NONE"
      }
    }
  ]
}

Even though I use lenient parsing, it does not work...

Upvotes: 0

Views: 1210

Answers (1)

Leonid Glanz
Leonid Glanz

Reputation: 1261

I have tried your code and it works for me with the following code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;

public class Main {

    public static void main(String[] args) throws Exception {
        IFEProcess fe=load(new File("jason.json"));
        save(new File("txt.json"),fe);
    }

    public static IFEProcess load(File file) throws FileNotFoundException,
            IOException, ClassNotFoundException {
        Gson gson = new Gson();
        String json = FileHelper.getStringFromFile(file);

        JsonReader reader = new JsonReader(new StringReader(json));
        reader.setLenient(true);

        return gson.fromJson(reader, FEProcess.class);
    }

    public static void save(File file, IFEProcess process) throws IOException {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(process);

        FileHelper.writeStringToFile(json, file);
    }

}

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileHelper {

    public static String getStringFromFile(File file) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader buff = new BufferedReader(new FileReader(file));
            String line;
            while ((line = buff.readLine()) != null)
                sb.append(line + "\n");
                buff.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return sb.toString();
        }

        public static void writeStringToFile(String json, File file) {
            FileWriter fw;
            try {
                fw = new FileWriter(file);
                fw.write(json);
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


    import java.util.ArrayList;


    public class FEProcess implements IFEProcess {

        String  description;
        String    altMailAdress;
        boolean   mailOnError;
        boolean   stopOnError;
        ArrayList<Operation> operationList;


    }

public class Operation {

    LConfig lConfig;
    OperationConfig operationConfig;
    String fileOperationType;

    class fopConfiguration {
            String fopPath;
            String fopTime;
        }

    }

public class LConfig {

    String password;
    String username;
    int port;
    String additionalLoginInformations;
    String hostname;
    boolean isToRemoteTransfer;
    String domain;
    String sambaShare;

}

public class OperationConfig {

    String remoteDirectory;
    String localDirectory;
    String sourceSemaphoreSuffix;
    String sourceSemaphoreType;
    String targetSemaphoreSuffix;
    String targetSemaphoreType;
    String sourceFilePattern;
    boolean lock;
    boolean delete;
    boolean backup;
    boolean noOverwrite;
    String fileRenameRegex;
    String fileRenameContent;


}

Upvotes: 1

Related Questions