Search in sources :

Example 1 with JsonReader

use of com.serotonin.json.JsonReader in project ma-modules-public by infiniteautomation.

the class JsonEmportV2Controller method uploadConfigurationFile.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Upload 1 configuration json file", notes = "Files should only contain the json object to be imported")
@RequestMapping(method = RequestMethod.POST, value = "/upload-file", consumes = { "multipart/form-data", "multipart/form-data;boundary=-----SWAG_BOUND" })
public ResponseEntity<ImportStatusProvider> uploadConfigurationFile(MultipartHttpServletRequest multipartRequest, UriComponentsBuilder builder, HttpServletRequest request, @ApiParam(value = "timeout for Status Resource to Expire, defaults to 5 minutes", required = false, allowMultiple = false) @RequestParam(value = "timeout", required = false) Long timeout, @AuthenticationPrincipal User user) throws RestValidationFailedException, IOException, JsonException {
    Map<String, MultipartFile> map = multipartRequest.getFileMap();
    if (map.size() != 1)
        throw new BadRequestException(new TranslatableMessage("rest.error.oneFileOnly"));
    Iterator<String> itr = multipartRequest.getFileNames();
    MultipartFile file = multipartRequest.getFile(itr.next());
    if (!file.isEmpty()) {
        JsonReader jr = new JsonReader(Common.JSON_CONTEXT, new String(file.getBytes()));
        JsonObject jo = jr.read(JsonObject.class);
        String resourceId = importStatusResources.generateResourceId();
        ImportStatusProvider statusProvider = new ImportStatusProvider(importStatusResources, resourceId, websocket, timeout, jo, user);
        // Setup the Temporary Resource
        this.importStatusResources.put(resourceId, statusProvider);
        URI location = builder.path("/v2/json-emport/import/{id}").buildAndExpand(resourceId).toUri();
        return getResourceCreated(statusProvider, location);
    } else {
        throw new BadRequestException(new TranslatableMessage("rest.error.noFileProvided"));
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) JsonReader(com.serotonin.json.JsonReader) JsonObject(com.serotonin.json.type.JsonObject) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with JsonReader

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

the class AnnotationsTest method main.

public static void main(String[] args) throws Exception {
    AnnotatedClass ac = new AnnotatedClass();
    ac.setId("qwer");
    ac.setName("asdf");
    System.out.println(JsonWriter.writeToString(null, ac));
    String s = "{\"id\":\"qwer\", \"name\":\"asdf\"}";
    AnnotatedClass ac2 = new JsonReader(s).read(AnnotatedClass.class);
    System.out.println(ac2.getId());
    System.out.println(ac2.getName());
}
Also used : JsonReader(com.serotonin.json.JsonReader)

Example 3 with JsonReader

use of com.serotonin.json.JsonReader 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 4 with JsonReader

use of com.serotonin.json.JsonReader 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 5 with JsonReader

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

the class MangoTestBase method loadConfiguration.

protected void loadConfiguration(File jsonFile) throws JsonException, IOException, URISyntaxException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile), Common.UTF8_CS));
    JsonReader jr = new JsonReader(reader);
    JsonObject jo = jr.read(JsonObject.class);
    ImportTask task = new ImportTask(jo, Common.getTranslations(), null, false);
    task.run(Common.timer.currentTimeMillis());
    if (task.getResponse().getHasMessages()) {
        for (ProcessMessage message : task.getResponse().getMessages()) {
            switch(message.getLevel()) {
                case error:
                case warning:
                    fail(message.toString(Common.getTranslations()));
                case info:
                    LOG.info(message.toString(Common.getTranslations()));
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ImportTask(com.serotonin.m2m2.web.dwr.emport.ImportTask) BufferedReader(java.io.BufferedReader) JsonReader(com.serotonin.json.JsonReader) JsonObject(com.serotonin.json.type.JsonObject) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) FileInputStream(java.io.FileInputStream)

Aggregations

JsonReader (com.serotonin.json.JsonReader)13 JsonObject (com.serotonin.json.type.JsonObject)5 JsonException (com.serotonin.json.JsonException)4 JsonContext (com.serotonin.json.JsonContext)3 JsonValue (com.serotonin.json.type.JsonValue)3 JsonWriter (com.serotonin.json.JsonWriter)2 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)2 TypeDefinition (com.serotonin.json.util.TypeDefinition)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 StringWriter (java.io.StringWriter)2 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 ThrowableSerializingConverter (com.serotonin.json.convert.ThrowableSerializingConverter)1 TypeResolver (com.serotonin.json.spi.TypeResolver)1 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ModelDefinition (com.serotonin.m2m2.module.ModelDefinition)1