Package org.json

Examples of org.json.JSONWriter


            String serviceUrl = request.getParameter("service");
           
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", "application/json");
           
            JSONWriter writer = new JSONWriter(response.getWriter());
            writer.object();
           
            Column column = project.columnModel.getColumnByName(columnName);
            if (column == null) {
                writer.key("code"); writer.value("error");
                writer.key("message"); writer.value("No such column");
            } else {
                try {
                    writer.key("code"); writer.value("ok");
                    writer.key("types"); writer.array();
                   
                    List<TypeGroup> typeGroups = guessTypes(project, column, serviceUrl);
                    for (TypeGroup tg : typeGroups) {
                        writer.object();
                        writer.key("id"); writer.value(tg.id);
                        writer.key("name"); writer.value(tg.name);
                        writer.key("score"); writer.value(tg.score);
                        writer.key("count"); writer.value(tg.count);
                        writer.endObject();
                    }
                   
                    writer.endArray();
                } catch (Exception e) {
                    writer.key("code"); writer.value("error");
                }
            }
           
            writer.endObject();
        } catch (Exception e) {
            respondException(response, e);
        }
    }
View Full Code Here


            }
        }
       
        StringWriter stringWriter = new StringWriter();
        try {
            JSONWriter jsonWriter = new JSONWriter(stringWriter);
            jsonWriter.object();
            for (int i = 0; i < samples.size(); i++) {
                jsonWriter.key("q" + i);
                jsonWriter.object();
               
                jsonWriter.key("query"); jsonWriter.value(samples.get(i));
                jsonWriter.key("limit"); jsonWriter.value(3);
               
                jsonWriter.endObject();
            }
            jsonWriter.endObject();
        } catch (JSONException e) {
            // ignore
        }
       
        String queriesString = stringWriter.toString();
View Full Code Here

            Set<String> done = new HashSet<String>();
           
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", "application/json");
           
            JSONWriter writer = new JSONWriter(response.getWriter());
            writer.object();
            writer.key("expressions");
                writer.array();
                for (String s : localExpressions) {
                    writer.object();
                    writer.key("code"); writer.value(s);
                    writer.key("global"); writer.value(false);
                    writer.key("starred"); writer.value(starredExpressions.contains(s));
                    writer.endObject();
                    done.add(s);
                }
                for (String s : globalExpressions) {
                    if (!done.contains(s)) {
                        writer.object();
                        writer.key("code"); writer.value(s);
                        writer.key("global"); writer.value(true);
                        writer.key("starred"); writer.value(starredExpressions.contains(s));
                        writer.endObject();
                    }
                }
                writer.endArray();
            writer.endObject();
        } catch (Exception e) {
            respondException(response, e);
        }
    }
View Full Code Here

    static public void writeOldColumnGroups(Writer writer, Properties options,
            List<ColumnGroup> oldColumnGroups) throws IOException {
        writer.write("oldColumnGroupCount=");
        writer.write(Integer.toString(oldColumnGroups.size())); writer.write('\n');
        for (ColumnGroup cg : oldColumnGroups) {
            JSONWriter jsonWriter = new JSONWriter(writer);
            try {
                cg.write(jsonWriter, options);
            } catch (JSONException e) {
                throw new IOException(e);
            }
View Full Code Here

            if (historyEntry != null) {
                /*
                 * If the process is done, write back the cell's data so that the
                 * client side can update its UI right away.
                 */
                JSONWriter writer = new JSONWriter(response.getWriter());

                Pool pool = new Pool();
                Properties options = new Properties();
                options.put("pool", pool);

                writer.object();
                writer.key("code"); writer.value("ok");
                writer.key("historyEntry"); historyEntry.write(writer, options);
                writer.key("cell"); process.newCell.write(writer, options);
                writer.key("pool"); pool.write(writer, options);
                writer.endObject();
            } else {
                respond(response, "{ \"code\" : \"pending\" }");
            }
        } catch (Exception e) {
            respondException(response, e);
View Full Code Here

            response.setHeader("Content-Type", "application/json");
           
            JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString);
            int length = rowIndices.length();
           
            JSONWriter writer = new JSONWriter(response.getWriter());
            writer.object();
           
            try {
                Evaluable eval = MetaParser.parse(expression);
               
                writer.key("code"); writer.value("ok");
                writer.key("results"); writer.array();
               
                Properties bindings = ExpressionUtils.createBindings(project);
                for (int i = 0; i < length; i++) {
                    Object result = null;
                   
                    int rowIndex = rowIndices.getInt(i);
                    if (rowIndex >= 0 && rowIndex < project.rows.size()) {
                        Row row = project.rows.get(rowIndex);
                        Cell cell = row.getCell(cellIndex);
                           
                        try {
                            ExpressionUtils.bind(bindings, row, rowIndex, columnName, cell);
                            result = eval.evaluate(bindings);
                           
                            if (repeat) {
                                for (int r = 0; r < repeatCount && ExpressionUtils.isStorable(result); r++) {
                                    Cell newCell = new Cell((Serializable) result, (cell != null) ? cell.recon : null);
                                    ExpressionUtils.bind(bindings, row, rowIndex, columnName, newCell);
                                   
                                    Object newResult = eval.evaluate(bindings);
                                    if (ExpressionUtils.isError(newResult)) {
                                        break;
                                    } else if (ExpressionUtils.sameValue(result, newResult)) {
                                        break;
                                    } else {
                                        result = newResult;
                                    }
                                }
                            }
                        } catch (Exception e) {
                            // ignore
                        }
                    }
                   
                    if (result == null) {
                        writer.value(null);
                    } else if (ExpressionUtils.isError(result)) {
                        writer.object();
                        writer.key("message"); writer.value(((EvalError) result).message);
                        writer.endObject();
                    } else {
                        StringBuffer sb = new StringBuffer();
                       
                        writeValue(sb, result, false);
                       
                        writer.value(sb.toString());
                    }
                }
                writer.endArray();
            } catch (ParsingException e) {
                writer.key("code"); writer.value("error");
                writer.key("type"); writer.value("parser");
                writer.key("message"); writer.value(e.getMessage());
            } catch (Exception e) {
                writer.key("code"); writer.value("error");
                writer.key("type"); writer.value("other");
                writer.key("message"); writer.value(e.getMessage());
            }
           
            writer.endObject();
        } catch (Exception e) {
            respondException(response, e);
        }
    }
View Full Code Here

       
        StandardReconJob job = new StandardReconJob();

        try {
            StringWriter stringWriter = new StringWriter();
            JSONWriter jsonWriter = new JSONWriter(stringWriter);
           
            jsonWriter.object();
                jsonWriter.key("query"); jsonWriter.value(cell.value.toString());
                if (typeID != null) {
                    jsonWriter.key("type"); jsonWriter.value(typeID);
                    jsonWriter.key("type_strict"); jsonWriter.value("should");
                }
               
                if (columnDetails.size() > 0) {
                    jsonWriter.key("properties");
                    jsonWriter.array();
                   
                    for (ColumnDetail c : columnDetails) {
                        int detailCellIndex = project.columnModel.getColumnByName(c.columnName).getCellIndex();
                       
                        Cell cell2 = row.getCell(detailCellIndex);
                        if (cell2 == null || !ExpressionUtils.isNonBlankData(cell2.value)) {
                            int cellIndex = project.columnModel.getColumnByName(columnName).getCellIndex();
                           
                            RowDependency rd = project.recordModel.getRowDependency(rowIndex);
                            if (rd != null && rd.cellDependencies != null) {
                                int contextRowIndex = rd.cellDependencies[cellIndex].rowIndex;
                                if (contextRowIndex >= 0 && contextRowIndex < project.rows.size()) {
                                    Row row2 = project.rows.get(contextRowIndex);
                                   
                                    cell2 = row2.getCell(detailCellIndex);
                                }
                            }
                        }
                       
                        if (cell2 != null && ExpressionUtils.isNonBlankData(cell2.value)) {
                            jsonWriter.object();
                           
                            jsonWriter.key("pid"); jsonWriter.value(c.propertyID);
                            jsonWriter.key("v");
                            if (cell2.recon != null && cell2.recon.match != null) {
                                jsonWriter.object();
                                jsonWriter.key("id"); jsonWriter.value(cell2.recon.match.id);
                                jsonWriter.key("name"); jsonWriter.value(cell2.recon.match.name);
                                jsonWriter.endObject();
                            } else {
                                jsonWriter.value(cell2.value.toString());
                            }
                           
                            jsonWriter.endObject();
                        }
                    }
                   
                    jsonWriter.endArray();
                }
            jsonWriter.endObject();
           
            job.text = cell.value.toString();
            job.code = stringWriter.toString();
        } catch (JSONException e) {
            //
View Full Code Here

       
        try {
            String query = null;
            {
                StringWriter stringWriter = new StringWriter();
                JSONWriter jsonWriter = new JSONWriter(stringWriter);
               
                jsonWriter.object();
                jsonWriter.key("query");
                    jsonWriter.array();
                    jsonWriter.object();
                   
                        jsonWriter.key("id"); jsonWriter.value(null);
                        jsonWriter.key("name"); jsonWriter.value(null);
                        jsonWriter.key("guid"); jsonWriter.value(null);
                        jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray();
                       
                        jsonWriter.key("id|=");
                            jsonWriter.array();
                            for (ReconJob job : jobs) {
                                jsonWriter.value(((IdBasedReconJob) job).id);
                            }
                            jsonWriter.endArray();
                       
                    jsonWriter.endObject();
                    jsonWriter.endArray();
                jsonWriter.endObject();
               
                query = stringWriter.toString();
            }
           
            StringBuffer sb = new StringBuffer(1024);
View Full Code Here

       
        return connection.getInputStream();
    }
   
    static protected void formulateQuery(Set<String> ids, JSONObject node, Writer writer) throws JSONException {
        JSONWriter jsonWriter = new JSONWriter(writer);
       
        jsonWriter.object();
        jsonWriter.key("query");
            jsonWriter.array();
            jsonWriter.object();
           
                jsonWriter.key("id"); jsonWriter.value(null);
                jsonWriter.key("id|=");
                    jsonWriter.array();
                    for (String id : ids) {
                        if (id != null) {
                            jsonWriter.value(id);
                        }
                    }
                    jsonWriter.endArray();
                   
                formulateQueryNode(node.getJSONArray("properties"), jsonWriter);
           
            jsonWriter.endObject();
            jsonWriter.endArray();
        jsonWriter.endObject();
    }
View Full Code Here

       
        try {
            String query = null;
            {
                StringWriter stringWriter = new StringWriter();
                JSONWriter jsonWriter = new JSONWriter(stringWriter);
               
                jsonWriter.object();
                jsonWriter.key("query");
                    jsonWriter.array();
                    jsonWriter.object();
                   
                        jsonWriter.key("id"); jsonWriter.value(null);
                        jsonWriter.key("name"); jsonWriter.value(null);
                        jsonWriter.key("guid"); jsonWriter.value(null);
                        jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray();
                       
                        jsonWriter.key("key");
                            jsonWriter.array();
                            jsonWriter.object();
                           
                            jsonWriter.key("namespace");
                                jsonWriter.object();
                                jsonWriter.key("id"); jsonWriter.value(namespace.id);
                                jsonWriter.endObject();
                               
                            jsonWriter.key("value"); jsonWriter.value(null);
                            jsonWriter.key("value|=");
                                jsonWriter.array();
                                for (ReconJob job : jobs) {
                                    jsonWriter.value(((KeyBasedReconJob) job).key);
                                }
                                jsonWriter.endArray();
                               
                            jsonWriter.endObject();
                            jsonWriter.endArray();
                       
                    jsonWriter.endObject();
                    jsonWriter.endArray();
                jsonWriter.endObject();
               
                query = stringWriter.toString();
            }
           
            StringBuffer sb = new StringBuffer(1024);
View Full Code Here

TOP

Related Classes of org.json.JSONWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.