Search in sources :

Example 6 with JsonTypeWriter

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

the class JsonEmportV2Controller method export.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Export Configuration", notes = "", response = JsonValue.class)
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<JsonValue> export(@ApiParam(value = "Elements To Export", required = true, allowMultiple = true) @RequestParam(name = "exportElements", required = true) String[] exportElements, HttpServletRequest request) throws JsonException {
    // Do the Export
    Map<String, Object> data = ConfigurationExportData.createExportDataMap(exportElements);
    JsonTypeWriter writer = new JsonTypeWriter(Common.JSON_CONTEXT);
    return new ResponseEntity<>(writer.writeObject(data), HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) JsonObject(com.serotonin.json.type.JsonObject) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with JsonTypeWriter

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

the class LazyFieldJsonTest method testLazyPermissionInObject.

@Test
public void testLazyPermissionInObject() {
    RoleService roleService = Common.getBean(RoleService.class);
    PermissionService permissionService = Common.getBean(PermissionService.class);
    Role role1 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-1", "Role 1")).getRole();
    Role role2 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-2", "Role 2")).getRole();
    LazyContainer container = new LazyContainer();
    container.supplyPermission(() -> MangoPermission.builder().minterm(role1, role2).build());
    try (StringWriter stringWriter = new StringWriter()) {
        JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
        JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
        JsonValue value = typeWriter.writeObject(container);
        writer.setPrettyIndent(0);
        writer.setPrettyOutput(true);
        writer.writeObject(value);
        String json = stringWriter.toString();
        JsonTypeReader typeReader = new JsonTypeReader(json);
        JsonValue read = typeReader.read();
        JsonObject root = read.toJsonObject();
        JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
        ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
        LazyContainer readContainer = new LazyContainer();
        context.getReader().readInto(readContainer, root);
        assertEquals(container.getPermission(), readContainer.getPermission());
    } catch (IOException | JsonException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : JsonException(com.serotonin.json.JsonException) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) ImportContext(com.infiniteautomation.mango.emport.ImportContext) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService) StringWriter(java.io.StringWriter) JsonReader(com.serotonin.json.JsonReader) JsonTypeReader(com.serotonin.json.type.JsonTypeReader) Test(org.junit.Test)

Aggregations

JsonTypeWriter (com.serotonin.json.type.JsonTypeWriter)7 JsonException (com.serotonin.json.JsonException)4 JsonWriter (com.serotonin.json.JsonWriter)4 JsonObject (com.serotonin.json.type.JsonObject)4 JsonValue (com.serotonin.json.type.JsonValue)4 IOException (java.io.IOException)4 ImportContext (com.infiniteautomation.mango.emport.ImportContext)3 JsonReader (com.serotonin.json.JsonReader)3 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 StringWriter (java.io.StringWriter)3 Test (org.junit.Test)3 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)2 RoleService (com.infiniteautomation.mango.spring.service.RoleService)2 Role (com.serotonin.m2m2.vo.role.Role)2 RoleVO (com.serotonin.m2m2.vo.role.RoleVO)2 ResponseEntity (org.springframework.http.ResponseEntity)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)1