Search in sources :

Example 1 with JsonTypeWriter

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

the class JsonEmportController 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(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with JsonTypeWriter

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

the class JsonSerializableUtility method convertMapToJsonObject.

/**
 */
public static JsonObject convertMapToJsonObject(Map<String, Object> map) throws JsonException {
    JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
    ObjectTypeWriter writer = new ObjectTypeWriter(typeWriter);
    Iterator<String> it = map.keySet().iterator();
    while (it.hasNext()) {
        String name = it.next();
        writer.writeEntry(name, map.get(name));
    }
    return writer.getJsonObject();
}
Also used : ObjectTypeWriter(com.serotonin.json.type.ObjectTypeWriter) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter)

Example 3 with JsonTypeWriter

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

the class EmportService method export.

/**
 * Export JSON to a Writer
 */
public void export(Map<String, Object> data, Writer writer, int prettyIndent) throws PermissionException {
    permissionService.ensurePermission(Common.getUser(), exportPermissionDefinition.getPermission());
    JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
    JsonWriter jsonWriter = new JsonWriter(Common.JSON_CONTEXT, writer);
    jsonWriter.setPrettyIndent(prettyIndent);
    jsonWriter.setPrettyOutput(prettyIndent > 0);
    try {
        JsonValue export = typeWriter.writeObject(data);
        jsonWriter.writeObject(export);
    } catch (JsonException | IOException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) JsonValue(com.serotonin.json.type.JsonValue) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter)

Example 4 with JsonTypeWriter

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

the class LazyFieldJsonTest method testLazyPermissionFromJsonObject.

@Test
public void testLazyPermissionFromJsonObject() {
    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();
    LazyField<MangoPermission> permission = new LazyField<>(() -> 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(permission);
        writer.setPrettyIndent(0);
        writer.setPrettyOutput(true);
        writer.writeObject(value);
        String json = stringWriter.toString();
        JsonTypeReader typeReader = new JsonTypeReader(json);
        JsonValue read = typeReader.read();
        JsonArray root = read.toJsonArray();
        JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
        ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
        LazyField<MangoPermission> readPermission = new LazyField<>();
        TypeDefinition lazyType = new TypeDefinition(LazyField.class, MangoPermission.class);
        context.getReader().readInto(lazyType, readPermission, root);
        assertEquals(permission.get(), readPermission.get());
    } catch (IOException | JsonException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : JsonException(com.serotonin.json.JsonException) LazyField(com.infiniteautomation.mango.util.LazyField) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter) TypeDefinition(com.serotonin.json.util.TypeDefinition) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) JsonArray(com.serotonin.json.type.JsonArray) 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) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) Test(org.junit.Test)

Example 5 with JsonTypeWriter

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

the class UserTest method testUser.

@Test
public void testUser() {
    User user = new User();
    Date created = new Date();
    user.setCreated(created);
    String userJson;
    try (StringWriter stringWriter = new StringWriter()) {
        JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
        JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
        JsonValue value = typeWriter.writeObject(user);
        writer.setPrettyIndent(0);
        writer.setPrettyOutput(true);
        writer.writeObject(value);
        userJson = stringWriter.toString();
        // System.out.println(userJson);
        JsonTypeReader typeReader = new JsonTypeReader(userJson);
        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());
        User readUser = new User();
        context.getReader().readInto(readUser, root);
        // Assert the dates are the same
        assertEquals(created, readUser.getCreated());
    } catch (IOException | JsonException e) {
        fail(e.getMessage());
    }
}
Also used : JsonException(com.serotonin.json.JsonException) User(com.serotonin.m2m2.vo.User) 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) Date(java.util.Date) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter) ImportContext(com.infiniteautomation.mango.emport.ImportContext) 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