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"));
}
}
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());
}
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);
}
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();
}
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()));
}
}
}
}
Aggregations