view src/main/java/de/mpiwg/gazetteer/utils/JSONUtils.java @ 0:7682c04c63a8

First commit of the source code!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 14:50:41 +0100
parents
children
line wrap: on
line source

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;
	}
}