Example usage for com.google.gson.stream JsonWriter endArray

List of usage examples for com.google.gson.stream JsonWriter endArray

Introduction

In this page you can find the example usage for com.google.gson.stream JsonWriter endArray.

Prototype

public JsonWriter endArray() throws IOException 

Source Link

Document

Ends encoding the current array.

Usage

From source file:logInAuthentication.java

void sendRequestToDataBase(HttpServletRequest request, HttpServletResponse response) throws IOException {
    PrintWriter pw = response.getWriter();
    response.setContentType("text/json;charset=UTF-8");

    try {/*w  w w .j a v a  2  s. co  m*/
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/uhms", "root", "");
        java.sql.Statement st = con.createStatement();
        StaticData.phone = request.getParameter("phone");
        StaticData.password = request.getParameter("password");
        ResultSet rs = st
                .executeQuery("SELECT gardian_password from gardian_info where id=" + StaticData.phone);
        if (rs.next()) {
            String pss = rs.getString(1);
            if (!StaticData.password.equals(pss)) {
                throw new Exception("Password did not match");
            }
        } else
            throw new Exception("No User Found by this Phone No");
        rs = st.executeQuery("SELECT *from student_info a,gardian_info b,student_gardian_relation c"
                + " WHERE a.id=c.student_id AND b.id = c.gardian_id" + " AND b.id=" + StaticData.phone);
        StaticData.resultSet = rs;
        if (!rs.next())
            throw new Exception("User Not Found On DATABASE");

        JsonWriter gsonWriter = new JsonWriter(pw);
        ResultSetMetaData metaData = rs.getMetaData();
        gsonWriter.beginObject();
        gsonWriter.name("info");
        gsonWriter.beginArray();
        rs.previous();
        while (rs.next()) {
            gsonWriter.beginObject();
            for (int indx = 1; indx <= metaData.getColumnCount(); indx++) {
                gsonWriter.name(metaData.getColumnLabel(indx));
                gsonWriter.value(rs.getString(indx));
            }
            gsonWriter.endObject();
        }
        gsonWriter.endArray();
        gsonWriter.name("status");
        gsonWriter.value("200");

        gsonWriter.endObject();
        gsonWriter.flush();
        gsonWriter.close();

    } catch (Exception ex) {
        JsonWriter gsonWriter = new JsonWriter(pw);
        gsonWriter.beginObject();
        gsonWriter.name("status");
        gsonWriter.value("400");
        gsonWriter.name("error_desc");
        gsonWriter.value(ex.getMessage());
        gsonWriter.endObject();
        gsonWriter.flush();
        gsonWriter.close();

        //  pw.println(ex);
    }
}

From source file:at.univie.sensorium.logging.JSONLogger.java

License:Open Source License

private void writeObject(AbstractSensor sensor) {
    if (externalMediaWriteable()) {
        JsonWriter jw = getWriterForName(sensor.getClass().getName());
        List<SensorValue> valuelist = sensor.getSensorValues();
        if (jw != null) {
            try {
                jw.beginObject();//from   w  w w  .  j ava  2 s .c o m
                jw.name("privacy-level").value(sensor.getPrivacylevel().name());
                for (SensorValue value : valuelist) {
                    if (value.isNested()) {
                        jw.name(value.getType().getName());
                        jw.beginArray();
                        List<NestedSensorValue> nested = (List<NestedSensorValue>) value.getValue();
                        for (NestedSensorValue nsv : nested) {
                            List<SensorValue> values = nsv.getInnerSensorValues();
                            jw.beginObject();
                            for (SensorValue nestedvalue : values) {
                                SensorValue privatized = Privacy.anonymize(nestedvalue,
                                        sensor.getPrivacylevel());
                                jw.name(privatized.getType().getName())
                                        .value(privatized.getValueRepresentation());
                            }
                            jw.endObject();

                        }
                        jw.endArray();
                    } else {
                        SensorValue privatized = Privacy.anonymize(value, sensor.getPrivacylevel());
                        jw.name(privatized.getType().getName()).value(privatized.getValueRepresentation());
                    }
                }
                jw.endObject();
                writerMap.get(sensor.getClass().getName()).flush();
            } catch (IOException e) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                Log.d(SensorRegistry.TAG, sw.toString());
            }
        } else {
            Log.d(SensorRegistry.TAG, "Can't get write access to log file, skipping");
        }
    }
}

From source file:at.univie.sensorium.logging.JSONLogger.java

License:Open Source License

public void finalizeLog() {
    //      String id = UUID.randomUUID().toString();
    for (AbstractSensor s : SensorRegistry.getInstance().getSensors()) {
        s.removeListener(this);
    }/*  www.ja  v  a 2s .  c om*/
    for (JsonWriter js : jsonMap.values()) {
        try {
            //            js.beginObject();
            //            js.name("id").value(id);
            //            js.endObject();
            js.endArray();
            js.flush(); // added on 2013-04-09; does this help in fixing the missing ]?
            js.close(); // added on 2013-04-09; does this help in fixing the missing ]?
        } catch (IOException e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            Log.d(SensorRegistry.TAG, sw.toString());
        }
    }
    // JSONWriter.close() already flushes/closes underlying writer,
    // so explicity closing the FileWriter would always throw an exception
}

From source file:bammerbom.ultimatecore.bukkit.resources.utils.JsonRepresentedObject.java

License:MIT License

@Override
public void writeJson(JsonWriter writer) throws IOException {
    if (messageParts.size() == 1) {
        latest().writeJson(writer);//from ww  w  .  j a v a 2s  .  c  o  m
    } else {
        writer.beginObject().name("text").value("").name("extra").beginArray();
        for (final MessagePart part : this) {
            part.writeJson(writer);
        }
        writer.endArray().endObject();
    }
}

From source file:bammerbom.ultimatecore.bukkit.resources.utils.JsonRepresentedObject.java

License:MIT License

public void writeJson(JsonWriter json) {
    try {//  w  w w.  j  a  v a 2 s.  c  o  m
        json.beginObject();
        text.writeJson(json);
        json.name("color").value(color.name().toLowerCase());
        for (final ChatColor style : styles) {
            json.name(stylesToNames.get(style)).value(true);
        }
        if (clickActionName != null && clickActionData != null) {
            json.name("clickEvent").beginObject().name("action").value(clickActionName).name("value")
                    .value(clickActionData).endObject();
        }
        if (hoverActionName != null && hoverActionData != null) {
            json.name("hoverEvent").beginObject().name("action").value(hoverActionName).name("value");
            hoverActionData.writeJson(json);
            json.endObject();
        }
        if (insertionData != null) {
            json.name("insertion").value(insertionData);
        }
        if (translationReplacements.size() > 0 && text != null && TextualComponent.isTranslatableText(text)) {
            json.name("with").beginArray();
            for (JsonRepresentedObject obj : translationReplacements) {
                obj.writeJson(json);
            }
            json.endArray();
        }
        json.endObject();
    } catch (IOException e) {
        Bukkit.getLogger().log(Level.WARNING, "A problem occured during writing of JSON string", e);
    }
}

From source file:be.iminds.iot.dianne.coordinator.util.DianneCoordinatorWriter.java

License:Open Source License

public static void writeObject(JsonWriter writer, Object o) throws Exception {
    if (o == null) {
        writer.value("null");
    } else if (o instanceof LearnResult) {
        writeLearnResult(writer, (LearnResult) o, 0);
    } else if (o instanceof EvaluationResult) {
        writeEvaluationResult(writer, (EvaluationResult) o);
    } else if (o instanceof AgentResult) {
        writeAgentResult(writer, (AgentResult) o, 0);
    } else if (o.getClass().equals(String.class) || o.getClass().isEnum() || o.getClass().equals(UUID.class)) {
        writer.value(o.toString());// w  w w . j  a v a 2s  .c  om
    } else if (o instanceof List) {
        List<?> l = (List<?>) o;
        writer.beginArray();
        for (Object ll : l) {
            writeObject(writer, ll);
        }
        writer.endArray();
    } else if (o instanceof Map) {
        Map<?, ?> m = (Map<?, ?>) o;
        writer.beginObject();
        for (Object k : m.keySet()) {
            writer.name(k.toString());
            writeObject(writer, m.get(k));
        }
        writer.endObject();
    } else if (o.getClass().isArray()) {
        int length = Array.getLength(o);
        writer.beginArray();
        for (int i = 0; i < length; i++) {
            writeObject(writer, Array.get(o, i));
        }
        writer.endArray();
    } else {
        writer.beginObject();
        writeFields(writer, o);
        writer.endObject();
    }
}

From source file:be.iminds.iot.dianne.coordinator.util.DianneCoordinatorWriter.java

License:Open Source License

public static void writeLearnResult(JsonWriter writer, LearnResult result, int limit) throws Exception {
    writer.beginArray();/*w  w  w  .  ja  v  a2s .  c  o  m*/
    // merge progress and validation in single object
    // TODO for now select one learners minibatch loss as progress?
    if (result.progress.size() > 0) {
        List<LearnProgress> select = result.progress.values().iterator().next();
        int step = 1000;
        if (limit > 0 && select.size() / step > limit) {
            step = select.size() / step * limit + 1;
        }
        for (int i = 0; i < select.size(); i += step) {
            LearnProgress p = select.get(i);
            Evaluation val = result.validations.get(p.iteration);

            writer.beginObject();
            writeFields(writer, p);
            if (val != null) {
                writer.name("validationLoss");
                writer.value(val.metric());
            }
            writer.endObject();
        }
    }
    writer.endArray();
}

From source file:be.iminds.iot.dianne.coordinator.util.DianneCoordinatorWriter.java

License:Open Source License

public static void writeEvaluationResult(JsonWriter writer, EvaluationResult result) throws Exception {
    writer.beginArray();//w w  w  . j  a v a 2  s  .c om
    for (Evaluation eval : result.evaluations.values()) {
        writer.beginObject();
        if (eval == null) {
            // write nothing?
        } else if (eval instanceof EvaluationProgress) {
            writer.name("processed");
            writer.value(((EvaluationProgress) eval).processed());
            writer.name("total");
            writer.value(((EvaluationProgress) eval).size());
            writer.name("metric");
            writer.value(((EvaluationProgress) eval).metric());
        } else {
            writer.name("evaluationTime");
            writer.value(eval.time());

            if (eval instanceof ErrorEvaluation) {
                ErrorEvaluation eeval = (ErrorEvaluation) eval;

                writer.name("error");
                writer.value(new Float(eeval.error()));

                writer.name("forwardTime");
                writer.value(new Float(eeval.forwardTime()));

                // write all outputs
                if (eeval.outputs() != null) {
                    writer.name("outputs");
                    writer.beginArray();
                    for (Tensor t : eeval.outputs()) {
                        writer.beginArray();
                        for (float f : t.get()) {
                            writer.value(new Float(f));
                        }
                        writer.endArray();
                    }
                    writer.endArray();
                }
            }

            if (eval instanceof ClassificationEvaluation) {
                ClassificationEvaluation ceval = (ClassificationEvaluation) eval;
                // write accuracy
                writer.name("accuracy");
                writer.value(new Float(ceval.accuracy()));

                writer.name("top3");
                writer.value(new Float(ceval.topNaccuracy(3)));

                writer.name("top5");
                writer.value(new Float(ceval.topNaccuracy(5)));

                // write confusion matrix
                writer.name("confusionMatrix");
                writer.beginArray();
                Tensor confusionMatrix = ceval.confusionMatrix();
                for (int i = 0; i < confusionMatrix.size(0); i++) {
                    writer.beginArray();
                    for (int j = 0; j < confusionMatrix.size(1); j++) {
                        writer.value(new Float(confusionMatrix.get(i, j)));
                    }
                    writer.endArray();
                }
                writer.endArray();
            }
        }
        writer.endObject();
    }
    writer.endArray();
}

From source file:be.iminds.iot.dianne.coordinator.util.DianneCoordinatorWriter.java

License:Open Source License

public static void writeAgentResult(JsonWriter writer, AgentResult result, int limit) throws Exception {
    writer.beginArray();/*from   w  w  w. j a v a 2 s.  c  o m*/
    for (List<AgentProgress> pp : result.progress.values()) {
        writer.beginArray();
        int step = 1;
        if (limit > 0 && pp.size() > limit) {
            step = pp.size() / limit + 1;
        }
        for (int i = 0; i < pp.size(); i += step) {
            AgentProgress progress = pp.get(i);
            writer.beginObject();
            writeFields(writer, progress);
            writer.endObject();
        }
        writer.endArray();
    }
    writer.endArray();
}

From source file:bufferings.ktr.wjr.server.service.KtrWjrServiceServlet.java

License:Apache License

/**
 * Write store json.//from w  w w.j a v a 2  s  .co  m
 * 
 * @param writer
 *          the writer
 * @param store
 *          the store.
 * @throws IOException
 *           when fail to write
 */
void writeStore(JsonWriter writer, WjrStore store) throws IOException {
    WjrStoreMeta m = WjrStoreMeta.meta();

    writer.beginObject().name(m.classItems).beginArray();
    for (WjrClassItem classItem : store.getClassItems()) {
        writeClassItem(writer, classItem);
    }
    writer.endArray();

    writer.name(m.methodItems).beginArray();
    for (WjrMethodItem methodItem : store.getMethodItems()) {
        writeMethodItem(writer, methodItem);
    }
    writer.endArray().endObject();
}