List of usage examples for org.json.simple.parser ContainerFactory ContainerFactory
ContainerFactory
From source file:com.textquo.dreamcode.server.JSONHelper.java
public static Map<String, Object> parseJson(String jsonText) { Map<String, Object> json = null; try {/* w ww . j av a 2s . c o m*/ org.json.simple.parser.JSONParser parser = new org.json.simple.parser.JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { public List creatArrayContainer() { return new LinkedList(); } public Map createObjectContainer() { return new LinkedHashMap(); } }; json = (Map<String, Object>) parser.parse(jsonText, containerFactory); Iterator iter = json.entrySet().iterator(); LOG.info("==iterate result=="); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); LOG.info(entry.getKey() + "=>" + entry.getValue()); } LOG.info("==toJSONString()=="); LOG.info(org.json.simple.JSONValue.toJSONString(json)); } catch (ParseException e) { e.printStackTrace(); } return json; }
From source file:com.hunchee.haystack.server.JSONHelper.java
public static Map<String, Object> parseJson(String jsonText) throws ParseException { Map<String, Object> json = null; org.json.simple.parser.JSONParser parser = new org.json.simple.parser.JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { public List creatArrayContainer() { return new LinkedList(); }/*from w ww . ja va 2 s . c o m*/ public Map createObjectContainer() { return new LinkedHashMap(); } }; json = (Map<String, Object>) parser.parse(jsonText, containerFactory); Iterator iter = json.entrySet().iterator(); LOG.info("==iterate result=="); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); LOG.info(entry.getKey() + "=>" + entry.getValue()); } LOG.info("==toJSONString()=="); LOG.info(org.json.simple.JSONValue.toJSONString(json)); return json; }
From source file:com.turt2live.xmail.api.Attachments.java
/** * Attempts to get an attachment from a JSON string. This will throw an IllegalArgumentException if * the json string is null or invalid./*w w w . j a v a 2s. co m*/ * * @param json the JSON string * * @return the attachment, or an UnknownAttachment if no match */ @SuppressWarnings("unchecked") public static Attachment getAttachment(String json) { if (json == null) { throw new IllegalArgumentException(); } JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { @Override public List<Object> creatArrayContainer() { return new LinkedList<Object>(); } @Override public Map<String, Object> createObjectContainer() { return new LinkedHashMap<String, Object>(); } }; Map<String, Object> map = new HashMap<String, Object>(); try { Map<?, ?> json1 = (Map<?, ?>) parser.parse(json, containerFactory); Iterator<?> iter = json1.entrySet().iterator(); // Type check while (iter.hasNext()) { Entry<?, ?> entry = (Entry<?, ?>) iter.next(); if (!(entry.getKey() instanceof String)) { throw new IllegalArgumentException("Not in <String, Object> format"); } } map = (Map<String, Object>) json1; } catch (ParseException e) { e.printStackTrace(); return null; } catch (IllegalArgumentException e) { e.printStackTrace(); return null; } XMail plugin = XMail.getInstance(); Attachment a = plugin.getMailServer().attemptAttachmentDecode(map); if (a != null) { return a; } a = plugin.getAttachmentHandlerRegistry().getAttachmentFromHandler(json); return a == null ? new UnknownAttachment(json) : a; }
From source file:org.exoplatform.social.client.core.util.SocialJSONDecodingSupport.java
/** * Parse JSON text into java Model object from the input source. * and then it's base on the class type. * /*from w w w .j a v a2s . co m*/ * @param <T> Generic type must extend from Model. * @param clazz Class type. * @param jsonContent Json content which you need to create the Model * @throws ParseException Throw this exception if any * */ public static <T extends Model> T parser(final Class<T> clazz, String jsonContent) throws ParseException { JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { public List<T> creatArrayContainer() { return new LinkedList<T>(); } public T createObjectContainer() { try { return clazz.newInstance(); } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } } }; return (T) parser.parse(jsonContent, containerFactory); }
From source file:ch.unifr.pai.twice.comm.serverPush.server.ServerRemoteEvent.java
/** * update the object with the value of the JSON message */// ww w . j a va2 s.c o m private void extract() { JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { @Override public List<?> creatArrayContainer() { return new LinkedList(); } @Override public Map createObjectContainer() { return new HashMap(); } }; try { Map json = (Map) parser.parse(message != null ? message.toString() : null, containerFactory); timeStamp = (String) json.get(RemoteEvent.TIMESTAMP); context = (String) json.get(RemoteEvent.CONTEXT); blockingEvent = (String) json.get(RemoteEvent.BLOCKINGEVENT); type = (String) json.get(RemoteEvent.EVENTTYPEKEY); originUUID = (String) json.get(RemoteEvent.ORIGINATINGDEVICE); receipients = (List<String>) json.get(RemoteEvent.RECEIPIENTS); } catch (ParseException pe) { System.out.println(pe); } }
From source file:com.turt2live.xmail.api.mail.MailWaiter.java
@SuppressWarnings("unchecked") @Override//from ww w.ja va 2s . co m public void done(Request request, ServerResponse response) { for (String line : response.getLines()) { JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { @Override public List<Object> creatArrayContainer() { return new LinkedList<Object>(); } @Override public Map<String, Object> createObjectContainer() { return new LinkedHashMap<String, Object>(); } }; Map<String, Object> map = new HashMap<String, Object>(); try { Map<?, ?> json = (Map<?, ?>) parser.parse(line, containerFactory); Iterator<?> iter = json.entrySet().iterator(); // Type check while (iter.hasNext()) { Entry<?, ?> entry = (Entry<?, ?>) iter.next(); if (!(entry.getKey() instanceof String)) { throw new IllegalArgumentException("Not in <String, Object> format"); } } map = (Map<String, Object>) json; } catch (ParseException e) { e.printStackTrace(); return; } catch (IllegalArgumentException e) { e.printStackTrace(); return; } Object mails = map.get("mail"); if (mails != null && mails instanceof List) { List<?> list = (List<?>) mails; List<Map<?, ?>> maps = new ArrayList<Map<?, ?>>(); for (Object o : list) { if (o instanceof Map) { maps.add((Map<?, ?>) o); } } List<Map<String, Object>> revisedMaps = new ArrayList<Map<String, Object>>(); for (Map<?, ?> o : maps) { Map<String, Object> m2 = new HashMap<String, Object>(); for (Object k : o.keySet()) { if (k instanceof String) { Object obj = o.get(k); m2.put((String) k, obj); } } revisedMaps.add(m2); } for (Map<String, Object> map2 : revisedMaps) { Mail m3 = Mail.fromJSON(map2); if (m3 != null) { this.mail.add(m3); } } } } hasMail = true; }
From source file:de.cgarbs.lib.json.JSONAdapter.java
/** * Returns a JSON {@link ContainerFactors} that keeps the ordering * from the JSON String for Maps and Lists by using {@link LinkedHashMap} * and {@link LinkedList}.// www .j a v a 2 s.c o m * * @return a ContainerFactory providing ordered results */ @SuppressWarnings("rawtypes") protected static ContainerFactory getOrderedContainerFactory() { if (orderedContainerFactory == null) { orderedContainerFactory = new ContainerFactory() { @Override public Map createObjectContainer() { return new LinkedHashMap(); } @Override public List creatArrayContainer() { return new LinkedList(); } }; } return orderedContainerFactory; }
From source file:com.intuit.tank.http.json.JsonResponse.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private void initialize() { try {//from w w w . j a va2 s .com JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { public List creatArrayContainer() { return new LinkedList(); } public Map createObjectContainer() { return new LinkedHashMap(); } }; if (!StringUtils.isEmpty(this.response)) { Object parse = parser.parse(this.response, containerFactory); if (parse instanceof List) { this.jsonMap = new LinkedHashMap(); } else { this.jsonMap = (Map) parse; } this.jsonMap.put("root", parse); } else { this.jsonMap = new HashMap(); } } catch (Exception ex) { logger.warn("Unable to parse the response string as a JSON object: " + this.response, ex); } }
From source file:org.exoplatform.social.client.core.util.SocialJSONDecodingSupport.java
/** * Parse JSON text into java Map object from the input source. * //w ww . ja va2 s .c om * @param jsonContent Json content which you need to create the Model * @throws ParseException Throw this exception if any */ public static Map parser(String jsonContent) throws ParseException { JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { public List creatArrayContainer() { return new LinkedList(); } public Map createObjectContainer() { return new LinkedHashMap(); } }; return (Map) parser.parse(jsonContent, containerFactory); }
From source file:com.appdynamics.monitors.pingdom.communicator.PingdomCommunicator.java
@SuppressWarnings("rawtypes") private void getCredits(Map<String, Integer> metrics) { try {/*from www . ja va 2s .c o m*/ HttpClient httpclient = new DefaultHttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); HttpGet httpget = new HttpGet(baseAddress + "/api/2.0/credits"); httpget.addHeader(BasicScheme.authenticate(creds, "US-ASCII", false)); httpget.addHeader("App-Key", appkey); HttpResponse response; response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); // reading in the JSON response String result = ""; if (entity != null) { InputStream instream = entity.getContent(); int b; try { while ((b = instream.read()) != -1) { result += Character.toString((char) b); } } finally { instream.close(); } } // parsing the JSON response try { JSONParser parser = new JSONParser(); ContainerFactory containerFactory = new ContainerFactory() { public List creatArrayContainer() { return new LinkedList(); } public Map createObjectContainer() { return new LinkedHashMap(); } }; // retrieving the metrics and populating HashMap JSONObject obj = (JSONObject) parser.parse(result); if (obj.get("credits") == null) { logger.error("Error retrieving data. " + obj); return; } Map json = (Map) parser.parse(obj.get("credits").toString(), containerFactory); if (json.containsKey("autofillsms")) { if (json.get("autofillsms").toString().equals("false")) { metrics.put("Credits|autofillsms", 0); } else if (json.get("autofillsms").toString().equals("true")) { metrics.put("Credits|autofillsms", 1); } else { logger.error("can't determine whether Credits|autofillsms is true or false!"); } } if (json.containsKey("availablechecks")) { try { metrics.put("Credits|availablechecks", Integer.parseInt(json.get("availablechecks").toString())); } catch (NumberFormatException e) { logger.error("Error parsing metric value for Credits|availablechecks!"); } } if (json.containsKey("availablesms")) { try { metrics.put("Credits|availablesms", Integer.parseInt(json.get("availablesms").toString())); } catch (NumberFormatException e) { logger.error("Error parsing metric value for Credits|availablesms!"); } } if (json.containsKey("availablesmstests")) { try { metrics.put("Credits|availablesmstests", Integer.parseInt(json.get("availablesmstests").toString())); } catch (NumberFormatException e) { logger.error("Error parsing metric value for Credits|availablesmstests!"); } } if (json.containsKey("checklimit")) { try { metrics.put("Credits|checklimit", Integer.parseInt(json.get("checklimit").toString())); } catch (NumberFormatException e) { logger.error("Error parsing metric value for Credits|checklimit!"); } } } catch (ParseException e) { logger.error("JSON Parsing error: " + e.getMessage()); } catch (Throwable e) { logger.error(e.getMessage()); } } catch (IOException e1) { logger.error(e1.getMessage()); } catch (Throwable t) { logger.error(t.getMessage()); } }