Search in sources :

Example 6 with JsonWriter

use of com.serotonin.json.JsonWriter in project ma-core-public by infiniteautomation.

the class EscapeTest method main.

public static void main(String[] args) throws Exception {
    JsonContext context = new JsonContext();
    context.setEscapeForwardSlash(true);
    StringWriter out = new StringWriter();
    JsonWriter writer = new JsonWriter(context, out);
    writer.writeObject("stream://asdf");
    String json = out.toString();
    System.out.println(json);
    JsonReader reader = new JsonReader(context, json);
    String s = reader.read(String.class);
    System.out.println(s);
}
Also used : JsonContext(com.serotonin.json.JsonContext) StringWriter(java.io.StringWriter) JsonReader(com.serotonin.json.JsonReader) JsonWriter(com.serotonin.json.JsonWriter)

Example 7 with JsonWriter

use of com.serotonin.json.JsonWriter in project ma-core-public by infiniteautomation.

the class ExceptionTest method main.

public static void main(String[] args) throws Exception {
    JsonContext context = new JsonContext();
    context.addConverter(new ThrowableSerializingConverter(), Throwable.class);
    StringWriter out = new StringWriter();
    JsonWriter writer = new JsonWriter(context, out);
    writer.setPrettyOutput(true);
    writer.writeObject(createException1());
    System.out.println(out);
    JsonReader reader = new JsonReader(context, out.toString());
    Throwable t = reader.read(Throwable.class);
    System.out.println("Great success!");
    System.out.println(t.getMessage());
    t.printStackTrace();
}
Also used : JsonContext(com.serotonin.json.JsonContext) StringWriter(java.io.StringWriter) ThrowableSerializingConverter(com.serotonin.json.convert.ThrowableSerializingConverter) JsonReader(com.serotonin.json.JsonReader) JsonWriter(com.serotonin.json.JsonWriter)

Example 8 with JsonWriter

use of com.serotonin.json.JsonWriter in project ma-core-public by infiniteautomation.

the class StatusServlet method doGet.

/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
    String timeString = request.getParameter("time");
    long time = -1;
    if (timeString != null) {
        try {
            time = Long.parseLong(timeString);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }
    response.setContentType("application/json;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    response.setHeader("Access-Control-Allow-Origin", "*");
    // Get the Info and pack it up
    Map<String, Object> data = new HashMap<String, Object>();
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, sw);
    // Limit to logged in users while running
    if ((lifecycle.getLifecycleState() != IMangoLifecycle.RUNNING) || (Common.getHttpUser() != null)) {
        data.put("messages", LoggingConsoleRT.instance.getMessagesSince(time));
    } else {
        data.put("messages", new ArrayList<String>());
    }
    data.put("startupProgress", lifecycle.getStartupProgress());
    data.put("shutdownProgress", lifecycle.getShutdownProgress());
    data.put("state", getLifecycleStateMessage(lifecycle.getLifecycleState()));
    // Can only get the Startup URI once the database is initalized
    if (lifecycle.getLifecycleState() > IMangoLifecycle.DATABASE_INITIALIZE) {
        Common.envProps.getBoolean("", false);
        Boolean isSsl = Common.envProps.getBoolean("ssl.on", false);
        String uri;
        if (isSsl) {
            int port = Common.envProps.getInt("ssl.port", 443);
            uri = "https://" + request.getServerName() + ":" + port + DefaultPagesDefinition.getLoginUri(request, response);
        } else {
            uri = DefaultPagesDefinition.getLoginUri(request, response);
        }
        data.put("startupUri", uri);
    }
    try {
        writer.writeObject(data);
        response.getWriter().write(sw.toString());
    } catch (JsonException e) {
        LOG.error(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) HashMap(java.util.HashMap) JsonWriter(com.serotonin.json.JsonWriter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException) StringWriter(java.io.StringWriter) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle)

Example 9 with JsonWriter

use of com.serotonin.json.JsonWriter in project ma-core-public by infiniteautomation.

the class EmportDwr method export.

public static String export(Map<String, Object> data, int prettyIndent) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
    writer.setPrettyIndent(prettyIndent);
    writer.setPrettyOutput(true);
    try {
        writer.writeObject(data);
        return stringWriter.toString();
    } catch (JsonException e) {
        throw new ShouldNeverHappenException(e);
    } catch (IOException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) StringWriter(java.io.StringWriter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter)

Example 10 with JsonWriter

use of com.serotonin.json.JsonWriter in project ma-core-public by infiniteautomation.

the class EventDetectorDao method writeValueAsString.

public String writeValueAsString(AbstractEventDetectorVO<?> value) throws JsonException, IOException {
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
    writer.writeObject(value);
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(com.serotonin.json.JsonWriter)

Aggregations

JsonWriter (com.serotonin.json.JsonWriter)29 StringWriter (java.io.StringWriter)26 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)12 HttpPost (org.apache.http.client.methods.HttpPost)12 StringEntity (org.apache.http.entity.StringEntity)12 JsonArray (com.serotonin.json.type.JsonArray)10 JsonValue (com.serotonin.json.type.JsonValue)10 IOException (java.io.IOException)7 JsonException (com.serotonin.json.JsonException)6 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)5 HashMap (java.util.HashMap)4 Version (com.github.zafarkhaja.semver.Version)2 JsonContext (com.serotonin.json.JsonContext)2 JsonReader (com.serotonin.json.JsonReader)2 JsonObject (com.serotonin.json.type.JsonObject)2 JsonString (com.serotonin.json.type.JsonString)2 Module (com.serotonin.m2m2.module.Module)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 OutputStreamWriter (java.io.OutputStreamWriter)2 JsonRawValue (com.fasterxml.jackson.annotation.JsonRawValue)1