Mercurial > hg > LGServices
diff src/main/java/de/mpiwg/gazetteer/utils/JSONUtils.java @ 0:3e62083dbcbf
First commit. This project comes from LGServer. We removed the framework icefaces. Now, LGServices uses just JSP and jquery.
author | "jurzua <jurzua@mpiwg-berlin.mpg.de>" |
---|---|
date | Thu, 23 Apr 2015 15:46:01 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/gazetteer/utils/JSONUtils.java Thu Apr 23 15:46:01 2015 +0200 @@ -0,0 +1,66 @@ +package de.mpiwg.gazetteer.utils; + +import org.json.JSONObject; + +import de.mpiwg.gazetteer.bo.LGBranch; +import de.mpiwg.gazetteer.bo.LGFile; + +public class JSONUtils { + + public static String getString(JSONObject json, String label){ + try { + return (json.has(label)) ? json.getString(label) : null; + } catch (Exception e) {} + return null; + } + + public static Long getLong(JSONObject json, String label){ + try { + return (json.has(label)) ? json.getLong(label) : null; + } catch (Exception e) {} + return null; + } + + public static JSONObject branch2JSON(LGBranch branch){ + JSONObject json = new JSONObject(); + + try { + + json.put("id", branch.getId()); + json.put("label", branch.getLabel()); + json.put("sectionId", branch.getSectionId()); + json.put("currentLastFileId", branch.getCurrentLastFileId()); + json.put("creatorId", branch.getUserId()); + json.put("creationDate", branch.getFomattedCreation()); + json.put("info", branch.getInfo()); + + } catch (Exception e) { + e.printStackTrace(); + } + + return json; + } + + public static JSONObject file2JSON(LGFile file, boolean includeText){ + JSONObject json = new JSONObject(); + + try { + + json.put("id", file.getId()); + json.put("version", file.getVersion()); + json.put("fileName", file.getFileName()); + json.put("info", file.getInfo()); + json.put("creatorId", file.getUserId()); + json.put("creationDate", file.getFomattedCreation()); + + if(includeText){ + json.put("text", file.getContent()); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + return json; + } +}