java - Gson doesn't read pretty printed JSON -
i use gson (version 2.3.1 in java 1.7) serialize object. though work of time, deserializing pretty printed string fails following exception:
com.google.gson.jsonsyntaxexception: com.google.gson.stream.malformedjsonexception: expected name @ line 2 column 1 path $. @ com.google.gson.gson.fromjson(gson.java:825) @ 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 use lenient parsing, not work...
i have tried code , works me 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; }
Comments
Post a Comment