comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:3e62083dbcbf
1 package de.mpiwg.gazetteer.utils;
2
3 import org.json.JSONObject;
4
5 import de.mpiwg.gazetteer.bo.LGBranch;
6 import de.mpiwg.gazetteer.bo.LGFile;
7
8 public class JSONUtils {
9
10 public static String getString(JSONObject json, String label){
11 try {
12 return (json.has(label)) ? json.getString(label) : null;
13 } catch (Exception e) {}
14 return null;
15 }
16
17 public static Long getLong(JSONObject json, String label){
18 try {
19 return (json.has(label)) ? json.getLong(label) : null;
20 } catch (Exception e) {}
21 return null;
22 }
23
24 public static JSONObject branch2JSON(LGBranch branch){
25 JSONObject json = new JSONObject();
26
27 try {
28
29 json.put("id", branch.getId());
30 json.put("label", branch.getLabel());
31 json.put("sectionId", branch.getSectionId());
32 json.put("currentLastFileId", branch.getCurrentLastFileId());
33 json.put("creatorId", branch.getUserId());
34 json.put("creationDate", branch.getFomattedCreation());
35 json.put("info", branch.getInfo());
36
37 } catch (Exception e) {
38 e.printStackTrace();
39 }
40
41 return json;
42 }
43
44 public static JSONObject file2JSON(LGFile file, boolean includeText){
45 JSONObject json = new JSONObject();
46
47 try {
48
49 json.put("id", file.getId());
50 json.put("version", file.getVersion());
51 json.put("fileName", file.getFileName());
52 json.put("info", file.getInfo());
53 json.put("creatorId", file.getUserId());
54 json.put("creationDate", file.getFomattedCreation());
55
56 if(includeText){
57 json.put("text", file.getContent());
58 }
59
60 } catch (Exception e) {
61 e.printStackTrace();
62 }
63
64 return json;
65 }
66 }